-
Notifications
You must be signed in to change notification settings - Fork 7
/
onlymarshaled.go
38 lines (33 loc) · 1.19 KB
/
onlymarshaled.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
package output
import (
"encoding/json"
"errors"
"fmt"
)
// OnlyMarshaled implements output.Command for a return value that is only displayed as raw marshaled in JSON and YAML
// eg. most 'state change' commands
type OnlyMarshaled struct {
Value interface{}
}
// MarshalJSON implements json.Marshaler and output.Output
func (d OnlyMarshaled) MarshalJSON() ([]byte, error) {
if errValue, ok := d.Value.(error); ok {
return json.MarshalIndent(map[string]interface{}{
"error": errValue.Error(),
}, "", " ")
}
return json.MarshalIndent(d.Value, "", " ")
}
// MarshalHuman implements output.Output
// For OnlyMarshaled outputs, we dont return anything in humanized output as it's assumed the log output is what the user
// wants and it is down to the command itself to provide that.
func (d OnlyMarshaled) MarshalHuman() ([]byte, error) {
if errValue, ok := d.Value.(error); ok {
return []byte(fmt.Sprintf("ERROR: %v", errValue)), nil
}
return []byte{}, nil
}
// MarshalRawMap implements output.Output
func (d OnlyMarshaled) MarshalRawMap() (map[string]interface{}, error) {
return nil, errors.New("onlymarshaled output should not be used as part of multiple output, raw output is undefined")
}