Skip to content

Commit da4a4ac

Browse files
committed
Merge pull request #2657 from ipfs/feature/add-defaults-to-add
Add Defaults to `ipfs add`
2 parents 518f7e0 + 03dd669 commit da4a4ac

File tree

2 files changed

+26
-37
lines changed

2 files changed

+26
-37
lines changed

core/commands/add.go

Lines changed: 21 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,11 @@ const (
3030

3131
var AddCmd = &cmds.Command{
3232
Helptext: cmds.HelpText{
33-
Tagline: "Add a file or directory to ipfs.",
33+
Tagline: "Add a file to ipfs.",
3434
ShortDescription: `
35-
Adds contents of <path> to ipfs. Use -r to add directories (recursively).
35+
Adds contents of <path> to ipfs. Use -r to add directories.
36+
Note that directories are added recursively, to form the ipfs
37+
MerkleDAG.
3638
`,
3739
LongDescription: `
3840
Adds contents of <path> to ipfs. Use -r to add directories.
@@ -61,29 +63,21 @@ You can now refer to the added file in a gateway, like so:
6163
},
6264
Options: []cmds.Option{
6365
cmds.OptionRecursivePath, // a builtin option that allows recursive paths (-r, --recursive)
64-
cmds.BoolOption(quietOptionName, "q", "Write minimal output."),
65-
cmds.BoolOption(silentOptionName, "Write no output."),
66-
cmds.BoolOption(progressOptionName, "p", "Stream progress data."),
67-
cmds.BoolOption(trickleOptionName, "t", "Use trickle-dag format for dag generation."),
68-
cmds.BoolOption(onlyHashOptionName, "n", "Only chunk and hash - do not write to disk."),
69-
cmds.BoolOption(wrapOptionName, "w", "Wrap files with a directory object."),
70-
cmds.BoolOption(hiddenOptionName, "H", "Include files that are hidden. Only takes effect on recursive add."),
66+
cmds.BoolOption(quietOptionName, "q", "Write minimal output.").Default(false),
67+
cmds.BoolOption(silentOptionName, "Write no output.").Default(false),
68+
cmds.BoolOption(progressOptionName, "p", "Stream progress data.").Default(true),
69+
cmds.BoolOption(trickleOptionName, "t", "Use trickle-dag format for dag generation.").Default(false),
70+
cmds.BoolOption(onlyHashOptionName, "n", "Only chunk and hash - do not write to disk.").Default(false),
71+
cmds.BoolOption(wrapOptionName, "w", "Wrap files with a directory object.").Default(false),
72+
cmds.BoolOption(hiddenOptionName, "H", "Include files that are hidden. Only takes effect on recursive add.").Default(false),
7173
cmds.StringOption(chunkerOptionName, "s", "Chunking algorithm to use."),
72-
cmds.BoolOption(pinOptionName, "Pin this object when adding. Default: true."),
74+
cmds.BoolOption(pinOptionName, "Pin this object when adding.").Default(true),
7375
},
7476
PreRun: func(req cmds.Request) error {
7577
if quiet, _, _ := req.Option(quietOptionName).Bool(); quiet {
7678
return nil
7779
}
7880

79-
// ipfs cli progress bar defaults to true
80-
progress, found, _ := req.Option(progressOptionName).Bool()
81-
if !found {
82-
progress = true
83-
}
84-
85-
req.SetOption(progressOptionName, progress)
86-
8781
sizeFile, ok := req.Files().(files.SizeFile)
8882
if !ok {
8983
// we don't need to error, the progress bar just won't know how big the files are
@@ -129,11 +123,7 @@ You can now refer to the added file in a gateway, like so:
129123
hidden, _, _ := req.Option(hiddenOptionName).Bool()
130124
silent, _, _ := req.Option(silentOptionName).Bool()
131125
chunker, _, _ := req.Option(chunkerOptionName).String()
132-
dopin, pin_found, _ := req.Option(pinOptionName).Bool()
133-
134-
if !pin_found { // default
135-
dopin = true
136-
}
126+
dopin, _, _ := req.Option(pinOptionName).Bool()
137127

138128
if hash {
139129
nilnode, err := core.NewNode(n.Context(), &core.BuildCfg{
@@ -220,7 +210,7 @@ You can now refer to the added file in a gateway, like so:
220210
return
221211
}
222212

223-
progress, prgFound, err := req.Option(progressOptionName).Bool()
213+
progress, _, err := req.Option(progressOptionName).Bool()
224214
if err != nil {
225215
res.SetError(u.ErrCast(), cmds.ErrNormal)
226216
return
@@ -232,16 +222,13 @@ You can now refer to the added file in a gateway, like so:
232222
return
233223
}
234224

235-
var showProgressBar bool
236-
if prgFound {
237-
showProgressBar = progress
238-
} else if !quiet && !silent {
239-
showProgressBar = true
225+
if !quiet && !silent {
226+
progress = true
240227
}
241228

242229
var bar *pb.ProgressBar
243230
var terminalWidth int
244-
if showProgressBar {
231+
if progress {
245232
bar = pb.New64(0).SetUnits(pb.U_BYTES)
246233
bar.ManualUpdate = true
247234
bar.Start()
@@ -276,7 +263,7 @@ You can now refer to the added file in a gateway, like so:
276263
}
277264
output := out.(*coreunix.AddedObject)
278265
if len(output.Hash) > 0 {
279-
if showProgressBar {
266+
if progress {
280267
// clear progress bar line before we print "added x" output
281268
fmt.Fprintf(res.Stderr(), "\033[2K\r")
282269
}
@@ -289,7 +276,7 @@ You can now refer to the added file in a gateway, like so:
289276
} else {
290277
log.Debugf("add progress: %v %v\n", output.Name, output.Bytes)
291278

292-
if !showProgressBar {
279+
if !progress {
293280
continue
294281
}
295282

@@ -305,11 +292,11 @@ You can now refer to the added file in a gateway, like so:
305292
totalProgress = bar.Add64(delta)
306293
}
307294

308-
if showProgressBar {
295+
if progress {
309296
bar.Update()
310297
}
311298
case size := <-sizeChan:
312-
if showProgressBar {
299+
if progress {
313300
bar.Total = size
314301
bar.ShowPercent = true
315302
bar.ShowBar = true

test/sharness/t0040-add-and-cat.sh

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,11 @@ test_description="Test add and cat commands"
1111
client_err_add() {
1212
printf "$@\n\n"
1313
echo 'USAGE
14-
ipfs add <path>... - Add a file or directory to ipfs.
14+
ipfs add <path>... - Add a file to ipfs.
1515
16-
Adds contents of <path> to ipfs. Use -r to add directories (recursively).
16+
Adds contents of <path> to ipfs. Use -r to add directories.
17+
Note that directories are added recursively, to form the ipfs
18+
MerkleDAG.
1719
1820
Use '"'"'ipfs add --help'"'"' for more information about this command.
1921
'
@@ -360,7 +362,7 @@ test_add_cat_5MB
360362

361363
test_add_cat_expensive
362364

363-
test_add_named_pipe " Post http://$API_ADDR/api/v0/add?encoding=json&progress=true&r=true&stream-channels=true:"
365+
test_add_named_pipe " Post http://$API_ADDR/api/v0/add?encoding=json&r=true&stream-channels=true:"
364366

365367
test_kill_ipfs_daemon
366368

0 commit comments

Comments
 (0)