-
Notifications
You must be signed in to change notification settings - Fork 23
/
list.go
106 lines (98 loc) · 3.55 KB
/
list.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
package cmd
import (
"os"
"strconv"
"github.com/0chain/gosdk/zboxcore/fileref"
"github.com/0chain/gosdk/zboxcore/sdk"
"github.com/0chain/zboxcli/util"
"github.com/spf13/cobra"
)
// listCmd represents list command
var listCmd = &cobra.Command{
Use: "list",
Short: "list files from blobbers",
Long: `list files from blobbers`,
Args: cobra.MinimumNArgs(0),
Run: func(cmd *cobra.Command, args []string) {
fflags := cmd.Flags() // fflags is a *flag.FlagSet
if fflags.Changed("remotepath") == false && fflags.Changed("authticket") == false {
PrintError("Error: remotepath / authticket flag is missing")
os.Exit(1)
}
remotepath := cmd.Flag("remotepath").Value.String()
authticket := cmd.Flag("authticket").Value.String()
lookuphash := cmd.Flag("lookuphash").Value.String()
if len(remotepath) == 0 && (len(authticket) == 0) {
PrintError("Error: remotepath / authticket / lookuphash flag is missing")
os.Exit(1)
}
if len(remotepath) > 0 {
if fflags.Changed("allocation") == false { // check if the flag "path" is set
PrintError("Error: allocation flag is missing") // If not, we'll let the user know
os.Exit(1) // and os.Exit(1)
}
allocationID := cmd.Flag("allocation").Value.String()
allocationObj, err := sdk.GetAllocation(allocationID)
if err != nil {
PrintError("Error fetching the allocation", err)
os.Exit(1)
}
remotepath := cmd.Flag("remotepath").Value.String()
ref, err := allocationObj.ListDir(remotepath)
if err != nil {
PrintError(err.Error())
os.Exit(1)
}
header := []string{"Type", "Name", "Path", "Size", "Num Blocks", "Lookup Hash"}
data := make([][]string, len(ref.Children))
for idx, child := range ref.Children {
size := strconv.FormatInt(child.Size, 10)
if child.Type == fileref.DIRECTORY {
size = ""
}
data[idx] = []string{child.Type, child.Name, child.Path, size, strconv.FormatInt(child.NumBlocks, 10), child.LookupHash}
}
util.WriteTable(os.Stdout, header, []string{}, data)
} else if len(authticket) > 0 {
allocationObj, err := sdk.GetAllocationFromAuthTicket(authticket)
if err != nil {
PrintError("Error fetching the allocation", err)
os.Exit(1)
}
at := sdk.InitAuthTicket(authticket)
isDir, err := at.IsDir()
if isDir && len(lookuphash) == 0 {
lookuphash, err = at.GetLookupHash()
if err != nil {
PrintError("Error getting the lookuphash from authticket", err)
os.Exit(1)
}
}
if !isDir {
PrintError("Invalid operation. Auth ticket is not for a directory")
os.Exit(1)
}
ref, err := allocationObj.ListDirFromAuthTicket(authticket, lookuphash)
if err != nil {
PrintError(err.Error())
os.Exit(1)
}
header := []string{"Type", "Name", "Size", "Num Blocks", "Lookup Hash"}
data := make([][]string, len(ref.Children))
for idx, child := range ref.Children {
size := strconv.FormatInt(child.Size, 10)
data[idx] = []string{child.Type, child.Name, size, strconv.FormatInt(child.NumBlocks, 10), child.LookupHash}
}
util.WriteTable(os.Stdout, header, []string{}, data)
}
return
},
}
func init() {
rootCmd.AddCommand(listCmd)
listCmd.PersistentFlags().String("allocation", "", "Allocation ID")
listCmd.PersistentFlags().String("remotepath", "", "Remote path to list from")
listCmd.PersistentFlags().String("authticket", "", "Auth ticket fot the file to download if you dont own it")
listCmd.PersistentFlags().String("lookuphash", "", "The remote lookuphash of the object retrieved from the list")
listCmd.MarkFlagRequired("allocation")
}