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: Richard Littauer <richard.littauer@gmail.com>
  • Loading branch information
RichardLitt authored and Kubuxu committed May 17, 2016
1 parent d183e34 commit f3bd304
Showing 1 changed file with 22 additions and 31 deletions.
53 changes: 22 additions & 31 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,8 +345,8 @@ 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("n", "count", "Maximum number of bytes to read."),
cmds.IntOption("o", "offset", "Byte offset to begin reading from.").Default(0),
cmds.IntOption("n", "count", "Maximum number of bytes to read.").Default(0),
},
Run: func(req cmds.Request, res cmds.Response) {
n, err := req.InvocContext().GetNode()
Expand Down Expand Up @@ -412,16 +409,16 @@ Examples:
}

var r io.Reader = &contextReaderWrapper{R: rfd, ctx: req.Context()}
count, found, err := req.Option("count").Int()
count, _, err := req.Option("count").Int()
if err != nil {
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 count > 0 {
r = io.LimitReader(r, int64(count))
}

Expand Down Expand Up @@ -516,10 +513,10 @@ 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("n", "count", "Maximum number of bytes to read."),
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.").Default(0),
},
Run: func(req cmds.Request, res cmds.Response) {
path, err := checkPath(req.Arguments()[0])
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, _, err := req.Option("count").Int()
if err != nil {
res.SetError(err, cmds.ErrNormal)
return
}
if countfound && count < 0 {
if 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

0 comments on commit f3bd304

Please sign in to comment.