提交 86b08f4e 作者: Juan Batiz-Benet

cmds2 config outputs nicely

上级 a2d06b5f
......@@ -53,10 +53,25 @@ func WriteFile(filename string, buf []byte) error {
return err
}
// HumanOutput gets a config value ready for printing
func HumanOutput(value interface{}) ([]byte, error) {
s, ok := value.(string)
if ok {
return []byte(strings.Trim(s, "\n")), nil
}
return Marshal(value)
}
// Marshal configuration with JSON
func Marshal(value interface{}) ([]byte, error) {
// need to prettyprint, hence MarshalIndent, instead of Encoder
return json.MarshalIndent(value, "", " ")
}
// Encode configuration with JSON
func Encode(w io.Writer, value interface{}) error {
// need to prettyprint, hence MarshalIndent, instead of Encoder
buf, err := json.MarshalIndent(value, "", " ")
buf, err := Marshal(value)
if err != nil {
return err
}
......
......@@ -2,7 +2,6 @@ package commands
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"io"
......@@ -87,20 +86,17 @@ Set the value of the 'datastore.path' key:
},
Marshallers: map[cmds.EncodingType]cmds.Marshaller{
cmds.Text: func(res cmds.Response) ([]byte, error) {
v := res.Output().(*ConfigField)
s := ""
if len(res.Request().Arguments()) == 2 {
s += fmt.Sprintf("'%s' set to: ", v.Key)
return nil, nil // dont output anything
}
marshalled, err := json.Marshal(v.Value)
v := res.Output().(*ConfigField)
buf, err := config.HumanOutput(v.Value)
if err != nil {
return nil, err
}
s += fmt.Sprintf("%s\n", marshalled)
return []byte(s), nil
buf = append(buf, byte('\n'))
return buf, nil
},
},
Type: &ConfigField{},
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论