Skip to content

Commit

Permalink
Implement the -q or --quiet flag to add with minimal output
Browse files Browse the repository at this point in the history
This implements issue ipfs#304.

It was done as much as possible in the same way as the
-r or --recursive flag.

License: MIT
Signed-off-by: Christian Couder <[email protected]>
  • Loading branch information
chriscool committed Nov 22, 2014
1 parent 34da5f0 commit cfab643
Show file tree
Hide file tree
Showing 7 changed files with 51 additions and 15 deletions.
14 changes: 13 additions & 1 deletion commands/cli/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func Parse(input []string, stdin *os.File, root *cmds.Command) (cmds.Request, *c
}
}

req, err := cmds.NewRequest(path, opts, nil, nil, cmd, optDefs)
req, err := cmds.NewRequest(path, false, opts, nil, nil, cmd, optDefs)
if err != nil {
return nil, cmd, path, err
}
Expand All @@ -58,6 +58,18 @@ func Parse(input []string, stdin *os.File, root *cmds.Command) (cmds.Request, *c
}
}

// if -q is provided and it is associated with the package builtin
// quiet option, set the quiet field in the request
quietOpt := req.Option(cmds.QuietShort)
quiet := false
if quietOpt != nil && quietOpt.Definition() == cmds.OptionQuietOutput {
quiet, _, err = quietOpt.Bool()
if err != nil {
return nil, nil, nil, u.ErrCast()
}
req.SetQuiet(quiet)
}

stringArgs, fileArgs, err := parseArgs(stringVals, stdin, cmd.Arguments, recursive)
if err != nil {
return nil, cmd, path, err
Expand Down
16 changes: 8 additions & 8 deletions commands/command_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,58 +17,58 @@ func TestOptionValidation(t *testing.T) {

opts, _ := cmd.GetOptions(nil)

req, _ := NewRequest(nil, nil, nil, nil, nil, opts)
req, _ := NewRequest(nil, false, nil, nil, nil, nil, opts)
req.SetOption("beep", true)
res := cmd.Call(req)
if res.Error() == nil {
t.Error("Should have failed (incorrect type)")
}

req, _ = NewRequest(nil, nil, nil, nil, nil, opts)
req, _ = NewRequest(nil, false, nil, nil, nil, nil, opts)
req.SetOption("beep", 5)
res = cmd.Call(req)
if res.Error() != nil {
t.Error(res.Error(), "Should have passed")
}

req, _ = NewRequest(nil, nil, nil, nil, nil, opts)
req, _ = NewRequest(nil, false, nil, nil, nil, nil, opts)
req.SetOption("beep", 5)
req.SetOption("boop", "test")
res = cmd.Call(req)
if res.Error() != nil {
t.Error("Should have passed")
}

req, _ = NewRequest(nil, nil, nil, nil, nil, opts)
req, _ = NewRequest(nil, false, nil, nil, nil, nil, opts)
req.SetOption("b", 5)
req.SetOption("B", "test")
res = cmd.Call(req)
if res.Error() != nil {
t.Error("Should have passed")
}

req, _ = NewRequest(nil, nil, nil, nil, nil, opts)
req, _ = NewRequest(nil, false, nil, nil, nil, nil, opts)
req.SetOption("foo", 5)
res = cmd.Call(req)
if res.Error() != nil {
t.Error("Should have passed")
}

req, _ = NewRequest(nil, nil, nil, nil, nil, opts)
req, _ = NewRequest(nil, false, nil, nil, nil, nil, opts)
req.SetOption(EncShort, "json")
res = cmd.Call(req)
if res.Error() != nil {
t.Error("Should have passed")
}

req, _ = NewRequest(nil, nil, nil, nil, nil, opts)
req, _ = NewRequest(nil, false, nil, nil, nil, nil, opts)
req.SetOption("b", "100")
res = cmd.Call(req)
if res.Error() != nil {
t.Error("Should have passed")
}

req, _ = NewRequest(nil, nil, nil, nil, &cmd, opts)
req, _ = NewRequest(nil, false, nil, nil, nil, &cmd, opts)
req.SetOption("b", ":)")
res = cmd.Call(req)
if res.Error() == nil {
Expand Down
2 changes: 1 addition & 1 deletion commands/http/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ func Parse(r *http.Request, root *cmds.Command) (cmds.Request, error) {
return nil, fmt.Errorf("File argument '%s' is required", requiredFile)
}

req, err := cmds.NewRequest(path, opts, args, f, cmd, optDefs)
req, err := cmds.NewRequest(path, false, opts, args, f, cmd, optDefs)
if err != nil {
return nil, err
}
Expand Down
3 changes: 3 additions & 0 deletions commands/option.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,11 +158,14 @@ const (
EncLong = "encoding"
RecShort = "r"
RecLong = "recursive"
QuietShort = "q"
QuietLong = "quiet"
)

// options that are used by this package
var OptionEncodingType = StringOption(EncShort, EncLong, "The encoding type the output should be encoded with (json, xml, or text)")
var OptionRecursivePath = BoolOption(RecShort, RecLong, "Add directory paths recursively")
var OptionQuietOutput = BoolOption(QuietShort, QuietLong, "Display minimal output")

// global options, added to every command
var globalOptions = []Option{
Expand Down
19 changes: 16 additions & 3 deletions commands/request.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ func (c *Context) NodeWithoutConstructing() *core.IpfsNode {
// Request represents a call to a command from a consumer
type Request interface {
Path() []string
Quiet() bool
SetQuiet(bool)
Option(name string) *OptionValue
Options() optMap
SetOption(name string, val interface{})
Expand All @@ -77,6 +79,7 @@ type Request interface {

type request struct {
path []string
quiet bool
options optMap
arguments []string
files File
Expand All @@ -90,6 +93,16 @@ func (r *request) Path() []string {
return r.path
}

// Quiet returns the quiet status of this request
func (r *request) Quiet() bool {
return r.quiet
}

// SetQuiet sets the quiet status of this request
func (r *request) SetQuiet(q bool) {
r.quiet = q
}

// Option returns the value of the option for given name.
func (r *request) Option(name string) *OptionValue {
// find the option with the specified name
Expand Down Expand Up @@ -254,12 +267,12 @@ func (r *request) ConvertOptions() error {

// NewEmptyRequest initializes an empty request
func NewEmptyRequest() (Request, error) {
return NewRequest(nil, nil, nil, nil, nil, nil)
return NewRequest(nil, false, nil, nil, nil, nil, nil)
}

// NewRequest returns a request initialized with given arguments
// An non-nil error will be returned if the provided option values are invalid
func NewRequest(path []string, opts optMap, args []string, file File, cmd *Command, optDefs map[string]Option) (Request, error) {
func NewRequest(path []string, quiet bool, opts optMap, args []string, file File, cmd *Command, optDefs map[string]Option) (Request, error) {
if path == nil {
path = make([]string, 0)
}
Expand All @@ -273,7 +286,7 @@ func NewRequest(path []string, opts optMap, args []string, file File, cmd *Comma
optDefs = make(map[string]Option)
}

req := &request{path, opts, args, file, cmd, Context{}, optDefs}
req := &request{path, quiet, opts, args, file, cmd, Context{}, optDefs}
err := req.ConvertOptions()
if err != nil {
return nil, err
Expand Down
2 changes: 1 addition & 1 deletion commands/response_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ func TestMarshalling(t *testing.T) {
cmd := &Command{}
opts, _ := cmd.GetOptions(nil)

req, _ := NewRequest(nil, nil, nil, nil, nil, opts)
req, _ := NewRequest(nil, false, nil, nil, nil, nil, opts)

res := NewResponse(req)
res.SetOutput(TestOutput{"beep", "boop", 1337})
Expand Down
10 changes: 9 additions & 1 deletion core/commands/add.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ var ErrDepthLimitExceeded = fmt.Errorf("depth limit exceeded")
type AddOutput struct {
Objects []*Object
Names []string
Quiet bool
}

var addCmd = &cmds.Command{
Expand All @@ -41,6 +42,7 @@ remains to be implemented.
},
Options: []cmds.Option{
cmds.OptionRecursivePath, // a builtin option that allows recursive paths (-r, --recursive)
cmds.OptionQuietOutput, // a builtin option to have minimal output (-q, --quiet)
},
Run: func(req cmds.Request) (interface{}, error) {
added := &AddOutput{}
Expand All @@ -64,6 +66,8 @@ remains to be implemented.
}
}

added.Quiet = req.Quiet()

return added, nil
},
Marshalers: cmds.MarshalerMap{
Expand All @@ -75,7 +79,11 @@ remains to be implemented.

var buf bytes.Buffer
for i, obj := range val.Objects {
buf.Write([]byte(fmt.Sprintf("added %s %s\n", obj.Hash, val.Names[i])))
if val.Quiet {
buf.Write([]byte(fmt.Sprintf("%s\n", obj.Hash)))
} else {
buf.Write([]byte(fmt.Sprintf("added %s %s\n", obj.Hash, val.Names[i])))
}
}
return buf.Bytes(), nil
},
Expand Down

0 comments on commit cfab643

Please sign in to comment.