commands: Allow overriding marshaller for any encoding type
Matt Bell committed
Nov 3, 2014 at 23:17 UTC
f95476c19aa6bcfe240c1c3bf6fc6fec3a54135b
7 files changed
+54
-42
cmd/ipfs2/main.go
+1
-1
@@ -58,7 +58,7 @@ func createRequest(args []string) (cmds.Request, *cmds.Command) {
58
ctx.Config = conf
59
60
if _, found := options.Option("encoding"); !found {
61
- if req.Command().Format != nil {
61
+ if req.Command().Marshallers != nil && req.Command().Marshallers[cmds.Text] != nil {
62
req.SetOption("encoding", cmds.Text)
63
} else {
64
req.SetOption("encoding", cmds.JSON)
core/commands2/add.go
+13
-11
@@ -55,18 +55,20 @@ var addCmd = &cmds.Command{
55
56
res.SetOutput(&AddOutput{added})
57
},
58
- Format: func(res cmds.Response) ([]byte, error) {
59
- v := res.Output().(*AddOutput).Added
60
- if len(v) == 1 {
61
- s := fmt.Sprintf("Added object: %s\n", v[0].Hash)
62
- return []byte(s), nil
63
- }
58
+ Marshallers: map[cmds.EncodingType]cmds.Marshaller{
59
+ cmds.Text: func(res cmds.Response) ([]byte, error) {
60
+ v := res.Output().(*AddOutput).Added
61
+ if len(v) == 1 {
62
+ s := fmt.Sprintf("Added object: %s\n", v[0].Hash)
63
+ return []byte(s), nil
64
+ }
65
65
- s := fmt.Sprintf("Added %v objects:\n", len(v))
66
- for _, obj := range v {
67
- s += fmt.Sprintf("- %s\n", obj.Hash)
68
- }
69
- return []byte(s), nil
66
+ s := fmt.Sprintf("Added %v objects:\n", len(v))
67
+ for _, obj := range v {
68
+ s += fmt.Sprintf("- %s\n", obj.Hash)
69
+ }
70
+ return []byte(s), nil
71
+ },
72
},
73
Type: &AddOutput{},
74
}
core/commands2/commands.go
+6
-4
@@ -18,10 +18,12 @@ var commandsCmd = &cmds.Command{
18
root := outputCommand("ipfs", Root)
19
res.SetOutput(&root)
20
},
21
- Format: func(res cmds.Response) ([]byte, error) {
22
- v := res.Output().(*Command)
23
- s := formatCommand(v, 0)
24
- return []byte(s), nil
21
+ Marshallers: map[cmds.EncodingType]cmds.Marshaller{
22
+ cmds.Text: func(res cmds.Response) ([]byte, error) {
23
+ v := res.Output().(*Command)
24
+ s := formatCommand(v, 0)
25
+ return []byte(s), nil
26
+ },
27
},
28
Type: &Command{},
29
}
core/commands2/log.go
+4
-2
@@ -23,6 +23,8 @@ var logCmd = &cmds.Command{
23
s := fmt.Sprintf("Changed log level of '%s' to '%s'", args[0], args[1])
24
res.SetOutput(&MessageOutput{s})
25
},
26
- Format: MessageMarshaller,
27
- Type: &MessageOutput{},
26
+ Marshallers: map[cmds.EncodingType]cmds.Marshaller{
27
+ cmds.Text: MessageTextMarshaller,
28
+ },
29
+ Type: &MessageOutput{},
30
}
core/commands2/ls.go
+16
-14
@@ -52,25 +52,27 @@ var lsCmd = &cmds.Command{
52
53
res.SetOutput(&LsOutput{output})
54
},
55
- Format: func(res cmds.Response) ([]byte, error) {
56
- s := ""
57
- output := res.Output().(*LsOutput).Objects
55
+ Marshallers: map[cmds.EncodingType]cmds.Marshaller{
56
+ cmds.Text: func(res cmds.Response) ([]byte, error) {
57
+ s := ""
58
+ output := res.Output().(*LsOutput).Objects
59
59
- for _, object := range output {
60
- if len(output) > 1 {
61
- s += fmt.Sprintf("%s:\n", object.Hash)
62
- }
60
+ for _, object := range output {
61
+ if len(output) > 1 {
62
+ s += fmt.Sprintf("%s:\n", object.Hash)
63
+ }
64
64
- for _, link := range object.Links {
65
- s += fmt.Sprintf("-> %s %s (%v bytes)\n", link.Name, link.Hash, link.Size)
66
- }
65
+ for _, link := range object.Links {
66
+ s += fmt.Sprintf("-> %s %s (%v bytes)\n", link.Name, link.Hash, link.Size)
67
+ }
68
68
- if len(output) > 1 {
69
- s += "\n"
69
+ if len(output) > 1 {
70
+ s += "\n"
71
+ }
72
}
71
- }
73
73
- return []byte(s), nil
74
+ return []byte(s), nil
75
+ },
76
},
77
Type: &LsOutput{},
78
}
core/commands2/publish.go
+6
-4
@@ -61,10 +61,12 @@ var publishCmd = &cmds.Command{
61
Value: ref,
62
})
63
},
64
- Format: func(res cmds.Response) ([]byte, error) {
65
- v := res.Output().(*PublishOutput)
66
- s := fmt.Sprintf("Published name %s to %s\n", v.Name, v.Value)
67
- return []byte(s), nil
64
+ Marshallers: map[cmds.EncodingType]cmds.Marshaller{
65
+ cmds.Text: func(res cmds.Response) ([]byte, error) {
66
+ v := res.Output().(*PublishOutput)
67
+ s := fmt.Sprintf("Published name %s to %s\n", v.Name, v.Value)
68
+ return []byte(s), nil
69
+ },
70
},
71
Type: &PublishOutput{},
72
}
core/commands2/root.go
+8
-6
@@ -71,11 +71,13 @@ var rootSubcommands = map[string]*cmds.Command{
71
log.Info("beep")
72
res.SetOutput(v)
73
},
74
- Format: func(res cmds.Response) ([]byte, error) {
75
- v := res.Output().(*TestOutput)
76
- s := fmt.Sprintf("Foo: %s\n", v.Foo)
77
- s += fmt.Sprintf("Bar: %v\n", v.Bar)
78
- return []byte(s), nil
74
+ Marshallers: map[cmds.EncodingType]cmds.Marshaller{
75
+ cmds.Text: func(res cmds.Response) ([]byte, error) {
76
+ v := res.Output().(*TestOutput)
77
+ s := fmt.Sprintf("Foo: %s\n", v.Foo)
78
+ s += fmt.Sprintf("Bar: %v\n", v.Bar)
79
+ return []byte(s), nil
80
+ },
81
},
82
Type: &TestOutput{},
83
},
@@ -120,6 +122,6 @@ type MessageOutput struct {
122
Message string
123
}
124
123
-func MessageMarshaller(res cmds.Response) ([]byte, error) {
125
+func MessageTextMarshaller(res cmds.Response) ([]byte, error) {
126
return []byte(res.Output().(*MessageOutput).Message), nil
127
}