Skip to content

Commit

Permalink
commands: Allow overriding marshaller for any encoding type
Browse files Browse the repository at this point in the history
  • Loading branch information
mappum authored and jbenet committed Nov 4, 2014
1 parent 9f241fe commit 22a826a
Show file tree
Hide file tree
Showing 7 changed files with 54 additions and 42 deletions.
2 changes: 1 addition & 1 deletion cmd/ipfs2/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ func createRequest(args []string) (cmds.Request, *cmds.Command) {
ctx.Config = conf

if _, found := options.Option("encoding"); !found {
if req.Command().Format != nil {
if req.Command().Marshallers != nil && req.Command().Marshallers[cmds.Text] != nil {
req.SetOption("encoding", cmds.Text)
} else {
req.SetOption("encoding", cmds.JSON)
Expand Down
24 changes: 13 additions & 11 deletions core/commands2/add.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,18 +55,20 @@ var addCmd = &cmds.Command{

res.SetOutput(&AddOutput{added})
},
Format: func(res cmds.Response) ([]byte, error) {
v := res.Output().(*AddOutput).Added
if len(v) == 1 {
s := fmt.Sprintf("Added object: %s\n", v[0].Hash)
return []byte(s), nil
}
Marshallers: map[cmds.EncodingType]cmds.Marshaller{
cmds.Text: func(res cmds.Response) ([]byte, error) {
v := res.Output().(*AddOutput).Added
if len(v) == 1 {
s := fmt.Sprintf("Added object: %s\n", v[0].Hash)
return []byte(s), nil
}

s := fmt.Sprintf("Added %v objects:\n", len(v))
for _, obj := range v {
s += fmt.Sprintf("- %s\n", obj.Hash)
}
return []byte(s), nil
s := fmt.Sprintf("Added %v objects:\n", len(v))
for _, obj := range v {
s += fmt.Sprintf("- %s\n", obj.Hash)
}
return []byte(s), nil
},
},
Type: &AddOutput{},
}
Expand Down
10 changes: 6 additions & 4 deletions core/commands2/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,12 @@ var commandsCmd = &cmds.Command{
root := outputCommand("ipfs", Root)
res.SetOutput(&root)
},
Format: func(res cmds.Response) ([]byte, error) {
v := res.Output().(*Command)
s := formatCommand(v, 0)
return []byte(s), nil
Marshallers: map[cmds.EncodingType]cmds.Marshaller{
cmds.Text: func(res cmds.Response) ([]byte, error) {
v := res.Output().(*Command)
s := formatCommand(v, 0)
return []byte(s), nil
},
},
Type: &Command{},
}
Expand Down
6 changes: 4 additions & 2 deletions core/commands2/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ var logCmd = &cmds.Command{
s := fmt.Sprintf("Changed log level of '%s' to '%s'", args[0], args[1])
res.SetOutput(&MessageOutput{s})
},
Format: MessageMarshaller,
Type: &MessageOutput{},
Marshallers: map[cmds.EncodingType]cmds.Marshaller{
cmds.Text: MessageTextMarshaller,
},
Type: &MessageOutput{},
}
30 changes: 16 additions & 14 deletions core/commands2/ls.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,25 +52,27 @@ var lsCmd = &cmds.Command{

res.SetOutput(&LsOutput{output})
},
Format: func(res cmds.Response) ([]byte, error) {
s := ""
output := res.Output().(*LsOutput).Objects
Marshallers: map[cmds.EncodingType]cmds.Marshaller{
cmds.Text: func(res cmds.Response) ([]byte, error) {
s := ""
output := res.Output().(*LsOutput).Objects

for _, object := range output {
if len(output) > 1 {
s += fmt.Sprintf("%s:\n", object.Hash)
}
for _, object := range output {
if len(output) > 1 {
s += fmt.Sprintf("%s:\n", object.Hash)
}

for _, link := range object.Links {
s += fmt.Sprintf("-> %s %s (%v bytes)\n", link.Name, link.Hash, link.Size)
}
for _, link := range object.Links {
s += fmt.Sprintf("-> %s %s (%v bytes)\n", link.Name, link.Hash, link.Size)
}

if len(output) > 1 {
s += "\n"
if len(output) > 1 {
s += "\n"
}
}
}

return []byte(s), nil
return []byte(s), nil
},
},
Type: &LsOutput{},
}
10 changes: 6 additions & 4 deletions core/commands2/publish.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,12 @@ var publishCmd = &cmds.Command{
Value: ref,
})
},
Format: func(res cmds.Response) ([]byte, error) {
v := res.Output().(*PublishOutput)
s := fmt.Sprintf("Published name %s to %s\n", v.Name, v.Value)
return []byte(s), nil
Marshallers: map[cmds.EncodingType]cmds.Marshaller{
cmds.Text: func(res cmds.Response) ([]byte, error) {
v := res.Output().(*PublishOutput)
s := fmt.Sprintf("Published name %s to %s\n", v.Name, v.Value)
return []byte(s), nil
},
},
Type: &PublishOutput{},
}
14 changes: 8 additions & 6 deletions core/commands2/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,11 +71,13 @@ var rootSubcommands = map[string]*cmds.Command{
log.Info("beep")
res.SetOutput(v)
},
Format: func(res cmds.Response) ([]byte, error) {
v := res.Output().(*TestOutput)
s := fmt.Sprintf("Foo: %s\n", v.Foo)
s += fmt.Sprintf("Bar: %v\n", v.Bar)
return []byte(s), nil
Marshallers: map[cmds.EncodingType]cmds.Marshaller{
cmds.Text: func(res cmds.Response) ([]byte, error) {
v := res.Output().(*TestOutput)
s := fmt.Sprintf("Foo: %s\n", v.Foo)
s += fmt.Sprintf("Bar: %v\n", v.Bar)
return []byte(s), nil
},
},
Type: &TestOutput{},
},
Expand Down Expand Up @@ -120,6 +122,6 @@ type MessageOutput struct {
Message string
}

func MessageMarshaller(res cmds.Response) ([]byte, error) {
func MessageTextMarshaller(res cmds.Response) ([]byte, error) {
return []byte(res.Output().(*MessageOutput).Message), nil
}

0 comments on commit 22a826a

Please sign in to comment.