-
Notifications
You must be signed in to change notification settings - Fork 23
/
list.go
181 lines (159 loc) · 4.98 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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
package cmd
import (
"os"
"strconv"
"strings"
"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()
doJSON, _ := cmd.Flags().GetBool("json")
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)
}
printListDirResult(doJSON, ref)
} 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)
lookuphash, err = at.GetLookupHash()
if err != nil {
PrintError("Error getting the lookuphash from authticket", err)
os.Exit(1)
}
ref, err := allocationObj.ListDirFromAuthTicket(authticket, lookuphash)
if err != nil {
PrintError(err.Error())
os.Exit(1)
}
printListDirResult(doJSON, ref)
}
return
},
}
var listAllCmd = &cobra.Command{
Use: "list-all",
Short: "list all files from blobbers",
Long: `list all 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("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 return
}
allocationID := cmd.Flag("allocation").Value.String()
allocationObj, err := sdk.GetAllocation(allocationID)
if err != nil {
PrintError("Error fetching the allocation", err)
os.Exit(1)
}
ref, err := allocationObj.GetRemoteFileMap(nil)
if err != nil {
PrintError(err.Error())
os.Exit(1)
}
type fileResp struct {
Name string `json:"name"`
Path string `json:"path"`
Type string `json:"type"`
Size int64 `json:"size"`
Hash string `json:"hash"`
}
fileResps := make([]fileResp, 0)
for path, data := range ref {
paths := strings.SplitAfter(path, "/")
var resp = fileResp{
Name: paths[len(paths)-1],
Path: path,
Type: data.Type,
Size: data.Size,
Hash: data.Hash,
}
fileResps = append(fileResps, resp)
}
util.PrintJSON(fileResps)
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.Flags().Bool("json", false, "pass this option to print response as json data")
listCmd.MarkFlagRequired("allocation")
rootCmd.AddCommand(listAllCmd)
listAllCmd.PersistentFlags().String("allocation", "", "Allocation ID")
listAllCmd.MarkFlagRequired("allocation")
}
func printListDirResult(outJson bool, ref *sdk.ListResult) {
if outJson {
util.PrintJSON(ref.Children)
return
}
header := []string{"Type", "Name", "Path", "Size", "Num Blocks", "Lookup Hash", "Is Encrypted"}
data := make([][]string, len(ref.Children))
for idx, child := range ref.Children {
size := strconv.FormatInt(child.Size, 10)
if child.Type == fileref.DIRECTORY {
size = ""
}
isEncrypted := ""
if child.Type == fileref.FILE {
if len(child.EncryptionKey) > 0 {
isEncrypted = "YES"
} else {
isEncrypted = "NO"
}
}
data[idx] = []string{
child.Type,
child.Name,
child.Path,
size,
strconv.FormatInt(child.NumBlocks, 10),
child.LookupHash,
isEncrypted,
}
}
util.WriteTable(os.Stdout, header, []string{}, data)
}