@cryptotaxi247 / kubo / commits / 38fc8131b

feat: add quieter option to add

It disables all outputs apart from the last hash. Useful for adding directories as you won't have to do `| tail -1` License: MIT Signed-off-by: Jakub Sztandera <kubuxu@protonmail.ch> DRY up License: MIT Signed-off-by: Jakub Sztandera <kubuxu@protonmail.ch>

Jakub Sztandera committed Mar 9, 2017 at 15:59 UTC 38fc8131bf46ff1dd3affbae40c5f95cf9fe578b
1 file changed +18 -11
core/commands/add.go
+18 -11
@@ -26,6 +26,7 @@ var ErrDepthLimitExceeded = fmt.Errorf("depth limit exceeded")
26
27 const (
28 quietOptionName = "quiet"
29 + quieterOptionName = "quieter"
30 silentOptionName = "silent"
31 progressOptionName = "progress"
32 trickleOptionName = "trickle"
@@ -73,6 +74,7 @@ You can now refer to the added file in a gateway, like so:
74 Options: []cmds.Option{
75 cmds.OptionRecursivePath, // a builtin option that allows recursive paths (-r, --recursive)
76 cmds.BoolOption(quietOptionName, "q", "Write minimal output."),
77 + cmds.BoolOption(quieterOptionName, "Q", "Write only final hash."),
78 cmds.BoolOption(silentOptionName, "Write no output."),
79 cmds.BoolOption(progressOptionName, "p", "Stream progress data."),
80 cmds.BoolOption(trickleOptionName, "t", "Use trickle-dag format for dag generation."),
@@ -87,6 +89,9 @@ You can now refer to the added file in a gateway, like so:
89 },
90 PreRun: func(req cmds.Request) error {
91 quiet, _, _ := req.Option(quietOptionName).Bool()
92 + quieter, _, _ := req.Option(quieterOptionName).Bool()
93 + quiet = quiet || quieter
94 +
95 silent, _, _ := req.Option(silentOptionName).Bool()
96
97 if quiet || silent {
@@ -278,17 +283,11 @@ You can now refer to the added file in a gateway, like so:
283 }
284 res.SetOutput(nil)
285
281 - quiet, _, err := req.Option("quiet").Bool()
282 - if err != nil {
283 - res.SetError(u.ErrCast(), cmds.ErrNormal)
284 - return
285 - }
286 + quiet, _, _ := req.Option(quietOptionName).Bool()
287 + quieter, _, _ := req.Option(quieterOptionName).Bool()
288 + quiet = quiet || quieter
289
287 - progress, _, err := req.Option(progressOptionName).Bool()
288 - if err != nil {
289 - res.SetError(u.ErrCast(), cmds.ErrNormal)
290 - return
291 - }
290 + progress, _, _ := req.Option(progressOptionName).Bool()
291
292 var bar *pb.ProgressBar
293 if progress {
@@ -307,6 +306,7 @@ You can now refer to the added file in a gateway, like so:
306 }
307
308 lastFile := ""
309 + lastHash := ""
310 var totalProgress, prevFiles, lastBytes int64
311
312 LOOP:
@@ -314,10 +314,18 @@ You can now refer to the added file in a gateway, like so:
314 select {
315 case out, ok := <-outChan:
316 if !ok {
317 + if quieter {
318 + fmt.Fprintln(res.Stdout(), lastHash)
319 + }
320 break LOOP
321 }
322 output := out.(*coreunix.AddedObject)
323 if len(output.Hash) > 0 {
324 + lastHash = output.Hash
325 + if quieter {
326 + continue
327 + }
328 +
329 if progress {
330 // clear progress bar line before we print "added x" output
331 fmt.Fprintf(res.Stderr(), "\033[2K\r")
@@ -327,7 +335,6 @@ You can now refer to the added file in a gateway, like so:
335 } else {
336 fmt.Fprintf(res.Stdout(), "added %s %s\n", output.Hash, output.Name)
337 }
330 -
338 } else {
339 log.Debugf("add progress: %v %v\n", output.Name, output.Bytes)
340