-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
cmd_simplefs_list.go
333 lines (300 loc) · 7.91 KB
/
cmd_simplefs_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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
// Copyright 2015 Keybase, Inc. All rights reserved. Use of
// this source code is governed by the included BSD license.
package client
import (
"bytes"
"errors"
"path/filepath"
"strings"
"golang.org/x/net/context"
"github.com/keybase/cli"
"github.com/keybase/client/go/libcmdline"
"github.com/keybase/client/go/libkb"
keybase1 "github.com/keybase/client/go/protocol/keybase1"
)
// ListOptions is for the Linux style
type ListOptions struct {
all bool
long bool
human bool
one bool
dir bool
color bool
sortReverse bool
sortTime bool
sortSize bool
dirsFirst bool
}
// CmdSimpleFSList is the 'fs ls' command.
type CmdSimpleFSList struct {
libkb.Contextified
paths []keybase1.Path
recurse bool
winStyle bool
options ListOptions
}
// NewCmdSimpleFSList creates a new cli.Command.
func NewCmdSimpleFSList(cl *libcmdline.CommandLine, g *libkb.GlobalContext) cli.Command {
return cli.Command{
Name: "ls",
ArgumentHelp: "<path>",
Usage: "list directory contents",
Action: func(c *cli.Context) {
cl.ChooseCommand(&CmdSimpleFSList{Contextified: libkb.NewContextified(g)}, "ls", c)
cl.SetNoStandalone()
},
Flags: []cli.Flag{
cli.BoolFlag{
Name: "rec, recursive",
Usage: "recurse into subdirectories",
},
/* TODO: currently this option does nothing.
cli.BoolFlag{
Name: "dirs-first",
Usage: "list directories first",
},
*/
cli.BoolFlag{
Name: "nocolor",
Usage: "remove color formatting",
},
cli.BoolFlag{
Name: "1, one",
Usage: "one entry per line",
},
cli.BoolFlag{
Name: "a, all",
Usage: "include entries starting with '.'",
},
cli.BoolFlag{
Name: "l, long",
Usage: "long listing",
},
cli.BoolFlag{
Name: "r, sort-reverse",
Usage: "reverse any sorting",
},
cli.BoolFlag{
Name: "t, sort-time",
Usage: "sort entries by modify time",
},
cli.BoolFlag{
Name: "s, sort-size",
Usage: "sort entries by size",
},
cli.BoolFlag{
Name: "w, windows",
Usage: "windows style dir",
},
cli.IntFlag{
Name: "rev",
Usage: "a revision number for the KBFS folder",
},
cli.StringFlag{
Name: "time",
Usage: "a time for the KBFS folder (eg \"2018-07-27 22:05\")",
},
cli.StringFlag{
Name: "reltime, relative-time",
Usage: "a relative time for the KBFS folder (eg \"5m\")",
},
},
}
}
// HandleTopLevelKeybaseList - See if this is either /keybase/public or /keybase/private,
// and request favorites accordingly.
func (c *CmdSimpleFSList) HandleTopLevelKeybaseList(path keybase1.Path) (bool, error) {
private := false
pathType, err := path.PathType()
if err != nil {
return false, err
}
if pathType != keybase1.PathType_KBFS {
return false, nil
}
acc := filepath.Clean(strings.ToLower(path.Kbfs().Path))
acc = filepath.ToSlash(acc)
c.G().Log.Debug("fs ls HandleTopLevelKeybaseList: %s -> %s", path.Kbfs(), acc)
if acc == "/private" {
private = true
} else if acc != "/public" {
return false, nil
}
arg := keybase1.GetFavoritesArg{}
tlfs, err := list(c.G(), arg)
if err != nil {
return true, err
}
result := keybase1.SimpleFSListResult{}
// copy the list result into a SimpleFS result
// to use the same output function
for _, f := range tlfs.FavoriteFolders {
if f.Private == private {
result.Entries = append(result.Entries, keybase1.Dirent{
Name: f.Name,
DirentType: keybase1.DirentType_DIR,
})
}
}
err = c.output(result)
return true, err
}
// Run runs the command in client/server mode.
func (c *CmdSimpleFSList) Run() error {
cli, err := GetSimpleFSClient(c.G())
if err != nil {
return err
}
ctx := context.TODO()
paths, err := doSimpleFSGlob(ctx, c.G(), cli, c.paths)
if err != nil {
return err
}
// If the argument was globbed, we really just want a stat of each item
if len(paths) > 1 {
var listResult keybase1.SimpleFSListResult
for _, path := range paths {
e, err := cli.SimpleFSStat(context.TODO(), keybase1.SimpleFSStatArg{Path: path})
if err != nil {
return err
}
// TODO: should stat include the path in the result?
e.Name = path.String()
listResult.Entries = append(listResult.Entries, e)
}
err := c.output(listResult)
if err != nil {
return err
}
} else if len(paths) == 1 {
path := paths[0]
c.G().Log.Debug("SimpleFSList %s", path)
opid, err2 := cli.SimpleFSMakeOpid(ctx)
if err2 != nil {
return err2
}
defer cli.SimpleFSClose(ctx, opid)
filter := keybase1.ListFilter_FILTER_ALL_HIDDEN
if c.options.all {
filter = keybase1.ListFilter_NO_FILTER
}
if c.recurse {
err = cli.SimpleFSListRecursive(ctx, keybase1.SimpleFSListRecursiveArg{
OpID: opid,
Path: path,
Filter: filter,
})
} else {
err = cli.SimpleFSList(ctx, keybase1.SimpleFSListArg{
OpID: opid,
Path: path,
Filter: filter,
})
}
if err != nil {
return err
}
err = cli.SimpleFSWait(ctx, opid)
if err != nil {
return err
}
gotList := false
for {
listResult, err := cli.SimpleFSReadList(ctx, opid)
// Eat the error here because it may just mean the results
// are complete. TODO: should KBFS return non-error here
// until the opid is closed?
if err != nil || len(listResult.Entries) == 0 {
if gotList {
err = nil
}
return err
}
gotList = true
err = c.output(listResult)
if err != nil {
return err
}
}
}
return err
}
// like keybase1.FormatTime(), except no time zone
func formatListTime(t keybase1.Time) string {
layout := "2006-01-02 15:04:05"
return keybase1.FromTime(t).Format(layout)
}
func (c *CmdSimpleFSList) output(listResult keybase1.SimpleFSListResult) error {
ui := c.G().UI.GetTerminalUI()
if c.winStyle {
for _, e := range listResult.Entries {
if e.DirentType == keybase1.DirentType_DIR || e.DirentType == keybase1.DirentType_SYM {
_, _ = ui.Printf("%s\t<%s>\t\t%s\n", formatListTime(e.Time), keybase1.DirentTypeRevMap[e.DirentType], e.Name)
} else {
_, _ = ui.Printf("%s\t%9d\t%s\n", formatListTime(e.Time), e.Size, e.Name)
}
}
} else {
// capture the current terminal dimensions
terminalWidth, _ := ui.TerminalSize()
var outputBuffer bytes.Buffer
err := c.ls(&outputBuffer, listResult, terminalWidth)
if err != nil {
return err
}
if outputBuffer.String() != "" {
_, _ = ui.PrintfUnescaped("%s", outputBuffer.String())
}
}
return nil
}
// ParseArgv gets the required path argument for this command.
func (c *CmdSimpleFSList) ParseArgv(ctx *cli.Context) error {
nargs := len(ctx.Args())
var err error
c.recurse = ctx.Bool("rec") || ctx.Bool("recursive")
c.winStyle = ctx.Bool("windows")
c.options.all = ctx.Bool("all")
c.options.long = ctx.Bool("long")
c.options.one = ctx.Bool("one")
c.options.dir = true // treat dirs as regular entries
c.options.color = !ctx.Bool("nocolor")
c.options.sortReverse = ctx.Bool("sort-reverse")
c.options.sortTime = ctx.Bool("sort-time")
c.options.sortSize = ctx.Bool("sort-size")
c.options.dirsFirst = ctx.Bool("dirs-first")
if nargs < 1 {
return errors.New("ls requires at least one KBFS path argument")
}
// TODO: "rev" should be a real int64, need to update the `cli`
// library for that.
rev := int64(ctx.Int("rev"))
time := ctx.String("time")
relTime := getRelTime(ctx)
for _, src := range ctx.Args() {
// Use the same revision number for each path.
argPath, err := makeSimpleFSPathWithArchiveParams(
src, rev, time, relTime)
if err != nil {
return err
}
pathType, err := argPath.PathType()
if err != nil {
return err
}
if pathType != keybase1.PathType_KBFS &&
pathType != keybase1.PathType_KBFS_ARCHIVED {
return errors.New("ls requires KBFS path arguments")
}
c.paths = append(c.paths, argPath)
}
return err
}
// GetUsage says what this command needs to operate.
func (c *CmdSimpleFSList) GetUsage() libkb.Usage {
return libkb.Usage{
Config: true,
KbKeyring: true,
API: true,
}
}