@cryptotaxi247 / kubo / commits / 86b08f4e4

cmds2 config outputs nicely

Juan Batiz-Benet committed Nov 12, 2014 at 04:25 UTC 86b08f4e4be649479d5215792376a55130d57d53
2 files changed +21 -10
config/serialize.go
+16 -1
@@ -53,10 +53,25 @@ func WriteFile(filename string, buf []byte) error {
53 return err
54 }
55
56 +// HumanOutput gets a config value ready for printing
57 +func HumanOutput(value interface{}) ([]byte, error) {
58 + s, ok := value.(string)
59 + if ok {
60 + return []byte(strings.Trim(s, "\n")), nil
61 + }
62 + return Marshal(value)
63 +}
64 +
65 +// Marshal configuration with JSON
66 +func Marshal(value interface{}) ([]byte, error) {
67 + // need to prettyprint, hence MarshalIndent, instead of Encoder
68 + return json.MarshalIndent(value, "", " ")
69 +}
70 +
71 // Encode configuration with JSON
72 func Encode(w io.Writer, value interface{}) error {
73 // need to prettyprint, hence MarshalIndent, instead of Encoder
59 - buf, err := json.MarshalIndent(value, "", " ")
74 + buf, err := Marshal(value)
75 if err != nil {
76 return err
77 }
core/commands2/config.go
+5 -9
@@ -2,7 +2,6 @@ package commands
2
3 import (
4 "bytes"
5 - "encoding/json"
5 "errors"
6 "fmt"
7 "io"
@@ -87,20 +86,17 @@ Set the value of the 'datastore.path' key:
86 },
87 Marshallers: map[cmds.EncodingType]cmds.Marshaller{
88 cmds.Text: func(res cmds.Response) ([]byte, error) {
90 - v := res.Output().(*ConfigField)
91 -
92 - s := ""
89 if len(res.Request().Arguments()) == 2 {
94 - s += fmt.Sprintf("'%s' set to: ", v.Key)
90 + return nil, nil // dont output anything
91 }
92
97 - marshalled, err := json.Marshal(v.Value)
93 + v := res.Output().(*ConfigField)
94 + buf, err := config.HumanOutput(v.Value)
95 if err != nil {
96 return nil, err
97 }
101 - s += fmt.Sprintf("%s\n", marshalled)
102 -
103 - return []byte(s), nil
98 + buf = append(buf, byte('\n'))
99 + return buf, nil
100 },
101 },
102 Type: &ConfigField{},