/
stat.go
69 lines (53 loc) · 1.56 KB
/
stat.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
package main
import (
"fmt"
pb "github.com/clawio/clawio/proto/metadata"
"github.com/spf13/cobra"
"golang.org/x/net/context"
"google.golang.org/grpc"
"os"
"text/tabwriter"
)
var statCmd = &cobra.Command{
Use: "stat <path>",
Short: "Stat a resource",
Run: stat,
}
func stat(cmd *cobra.Command, args []string) {
if len(args) != 1 {
fmt.Println("You have to provide a path")
os.Exit(1)
}
token, err := getToken()
if err != nil {
fmt.Println("Authentication required")
os.Exit(1)
}
addr := os.Getenv("CLAWIO_CLI_META_ADDR")
con, err := grpc.Dial(addr, grpc.WithInsecure())
if err != nil {
fmt.Println("Cannot connect to server " + addr)
os.Exit(1)
}
defer con.Close()
c := pb.NewMetaClient(con)
in := &pb.StatReq{}
in.AccessToken = token
in.Path = args[0]
in.Children = childrenFlag
ctx := context.Background()
res, err := c.Stat(ctx, in)
if err != nil {
fmt.Println("Cannot stat resource: " + err.Error())
os.Exit(1)
}
tabWriter := tabwriter.NewWriter(os.Stdout, 0, 4, 2, ' ', 0)
defer tabWriter.Flush()
fmt.Fprintln(tabWriter, "ID\tPath\tContainer\tSize\tModified\tPermissions\tETag\tMime\tChecksum")
fmt.Fprintf(tabWriter, "%s\t%s\t%t\t%d\t%d\t%d\t%s\t%s\t%s\n",
res.Id, res.Path, res.IsContainer, res.Size, res.Modified, res.Permissions, res.Etag, res.MimeType, res.Checksum)
for _, child := range res.GetChildren() {
fmt.Fprintf(tabWriter, "%s\t%s\t%t\t%d\t%d\t%d\t%s\t%s\t%s\n",
child.Id, child.Path, child.IsContainer, child.Size, child.Modified, child.Permissions, child.Etag, child.MimeType, child.Checksum)
}
}