Skip to content

Commit

Permalink
Added Default logic to files cmd
Browse files Browse the repository at this point in the history
Part of #2484

License: MIT
Signed-off-by: Jakub Sztandera <kubuxu@protonmail.ch>
  • Loading branch information
Kubuxu committed May 19, 2016
1 parent abeea4f commit 791b2cc
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 29 deletions.
47 changes: 19 additions & 28 deletions core/commands/files/files.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ on the files in question, then data may be lost. This also applies to running
`,
},
Options: []cmds.Option{
cmds.BoolOption("f", "flush", "Flush target and ancestors after write. Default: true."),
cmds.BoolOption("f", "flush", "Flush target and ancestors after write.").Default(true),
},
Subcommands: map[string]*cmds.Command{
"read": FilesReadCmd,
Expand Down Expand Up @@ -158,10 +158,7 @@ var FilesCpCmd = &cmds.Command{
return
}

flush, found, _ := req.Option("flush").Bool()
if !found {
flush = true
}
flush, _, _ := req.Option("flush").Bool()

src, err := checkPath(req.Arguments()[0])
if err != nil {
Expand Down Expand Up @@ -252,7 +249,7 @@ Examples:
cmds.StringArg("path", false, false, "Path to show listing for. Defaults to '/'."),
},
Options: []cmds.Option{
cmds.BoolOption("l", "Use long listing format."),
cmds.BoolOption("l", "Use long listing format.").Default(false),
},
Run: func(req cmds.Request, res cmds.Response) {
var arg string
Expand Down Expand Up @@ -348,7 +345,7 @@ Examples:
cmds.StringArg("path", true, false, "Path to file to be read."),
},
Options: []cmds.Option{
cmds.IntOption("o", "offset", "Byte offset to begin reading from."),
cmds.IntOption("o", "offset", "Byte offset to begin reading from.").Default(0),
cmds.IntOption("n", "count", "Maximum number of bytes to read."),
},
Run: func(req cmds.Request, res cmds.Response) {
Expand Down Expand Up @@ -417,11 +414,11 @@ Examples:
res.SetError(err, cmds.ErrNormal)
return
}
if found {
if count < 0 {
res.SetError(fmt.Errorf("Cannot specify negative 'count'."), cmds.ErrNormal)
return
}
if count < 0 {
res.SetError(fmt.Errorf("Cannot specify negative 'count'."), cmds.ErrNormal)
return
}
if found && count > 0 {
r = io.LimitReader(r, int64(count))
}

Expand Down Expand Up @@ -516,9 +513,9 @@ WARNING:
cmds.FileArg("data", true, false, "Data to write.").EnableStdin(),
},
Options: []cmds.Option{
cmds.IntOption("o", "offset", "Byte offset to begin writing at."),
cmds.BoolOption("e", "create", "Create the file if it does not exist."),
cmds.BoolOption("t", "truncate", "Truncate the file to size zero before writing."),
cmds.IntOption("o", "offset", "Byte offset to begin writing at.").Default(0),
cmds.BoolOption("e", "create", "Create the file if it does not exist.").Default(false),
cmds.BoolOption("t", "truncate", "Truncate the file to size zero before writing.").Default(false),
cmds.IntOption("n", "count", "Maximum number of bytes to read."),
},
Run: func(req cmds.Request, res cmds.Response) {
Expand All @@ -530,10 +527,7 @@ WARNING:

create, _, _ := req.Option("create").Bool()
trunc, _, _ := req.Option("truncate").Bool()
flush, fset, _ := req.Option("flush").Bool()
if !fset {
flush = true
}
flush, _, _ := req.Option("flush").Bool()

nd, err := req.InvocContext().GetNode()
if err != nil {
Expand Down Expand Up @@ -572,12 +566,12 @@ WARNING:
}
}

count, countfound, err := req.Option("count").Int()
count, found, err := req.Option("count").Int()
if err != nil {
res.SetError(err, cmds.ErrNormal)
return
}
if countfound && count < 0 {
if found && count < 0 {
res.SetError(fmt.Errorf("cannot have negative byte count"), cmds.ErrNormal)
return
}
Expand All @@ -596,7 +590,7 @@ WARNING:
}

var r io.Reader = input
if countfound {
if count > 0 {
r = io.LimitReader(r, int64(count))
}

Expand Down Expand Up @@ -629,7 +623,7 @@ Examples:
cmds.StringArg("path", true, false, "Path to dir to make."),
},
Options: []cmds.Option{
cmds.BoolOption("p", "parents", "No error if existing, make parent directories as needed."),
cmds.BoolOption("p", "parents", "No error if existing, make parent directories as needed.").Default(false),
},
Run: func(req cmds.Request, res cmds.Response) {
n, err := req.InvocContext().GetNode()
Expand All @@ -645,10 +639,7 @@ Examples:
return
}

flush, found, _ := req.Option("flush").Bool()
if !found {
flush = true
}
flush, _, _ := req.Option("flush").Bool()

err = mfs.Mkdir(n.FilesRoot, dirtomake, dashp, flush)
if err != nil {
Expand Down Expand Up @@ -709,7 +700,7 @@ Remove files or directories.
cmds.StringArg("path", true, true, "File to remove."),
},
Options: []cmds.Option{
cmds.BoolOption("r", "recursive", "Recursively remove directories."),
cmds.BoolOption("r", "recursive", "Recursively remove directories.").Default(false),
},
Run: func(req cmds.Request, res cmds.Response) {
nd, err := req.InvocContext().GetNode()
Expand Down
2 changes: 1 addition & 1 deletion test/sharness/t0250-files-api.sh
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ test_files_api() {
'

test_expect_success "cannot read negative count bytes" '
test_expect_code 1 ipfs read --count -1 /cats/file1
test_expect_code 1 ipfs files read --count -1 /cats/file1
'

test_expect_success "reading zero bytes prints nothing" '
Expand Down

0 comments on commit 791b2cc

Please sign in to comment.