Commit f95476c1 authored by Matt Bell's avatar Matt Bell Committed by Juan Batiz-Benet

commands: Allow overriding marshaller for any encoding type

parent 0afd3391
......@@ -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)
......
......@@ -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{},
}
......
......@@ -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{},
}
......
......@@ -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{},
}
......@@ -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{},
}
......@@ -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{},
}
......@@ -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{},
},
......@@ -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
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment