-
Notifications
You must be signed in to change notification settings - Fork 5.3k
/
json_flags.go
185 lines (167 loc) · 5.03 KB
/
json_flags.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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
package cmdutil
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"io"
"reflect"
"sort"
"strings"
"github.com/cli/cli/pkg/export"
"github.com/cli/cli/pkg/jsoncolor"
"github.com/cli/cli/pkg/set"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
)
type JSONFlagError struct {
error
}
func AddJSONFlags(cmd *cobra.Command, exportTarget *Exporter, fields []string) {
f := cmd.Flags()
f.StringSlice("json", nil, "Output JSON with the specified `fields`")
f.StringP("jq", "q", "", "Filter JSON output using a jq `expression`")
f.StringP("template", "t", "", "Format JSON output using a Go template")
_ = cmd.RegisterFlagCompletionFunc("json", func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
var results []string
var prefix string
if idx := strings.LastIndexByte(toComplete, ','); idx >= 0 {
prefix = toComplete[:idx+1]
toComplete = toComplete[idx+1:]
}
toComplete = strings.ToLower(toComplete)
for _, f := range fields {
if strings.HasPrefix(strings.ToLower(f), toComplete) {
results = append(results, prefix+f)
}
}
sort.Strings(results)
return results, cobra.ShellCompDirectiveNoSpace
})
oldPreRun := cmd.PreRunE
cmd.PreRunE = func(c *cobra.Command, args []string) error {
if oldPreRun != nil {
if err := oldPreRun(c, args); err != nil {
return err
}
}
if export, err := checkJSONFlags(c); err == nil {
if export == nil {
*exportTarget = nil
} else {
allowedFields := set.NewStringSet()
allowedFields.AddValues(fields)
for _, f := range export.fields {
if !allowedFields.Contains(f) {
sort.Strings(fields)
return JSONFlagError{fmt.Errorf("Unknown JSON field: %q\nAvailable fields:\n %s", f, strings.Join(fields, "\n "))}
}
}
*exportTarget = export
}
} else {
return err
}
return nil
}
cmd.SetFlagErrorFunc(func(c *cobra.Command, e error) error {
if e.Error() == "flag needs an argument: --json" {
sort.Strings(fields)
return JSONFlagError{fmt.Errorf("Specify one or more comma-separated fields for `--json`:\n %s", strings.Join(fields, "\n "))}
}
return c.Parent().FlagErrorFunc()(c, e)
})
}
func checkJSONFlags(cmd *cobra.Command) (*exportFormat, error) {
f := cmd.Flags()
jsonFlag := f.Lookup("json")
jqFlag := f.Lookup("jq")
tplFlag := f.Lookup("template")
webFlag := f.Lookup("web")
if jsonFlag.Changed {
if webFlag != nil && webFlag.Changed {
return nil, errors.New("cannot use `--web` with `--json`")
}
jv := jsonFlag.Value.(pflag.SliceValue)
return &exportFormat{
fields: jv.GetSlice(),
filter: jqFlag.Value.String(),
template: tplFlag.Value.String(),
}, nil
} else if jqFlag.Changed {
return nil, errors.New("cannot use `--jq` without specifying `--json`")
} else if tplFlag.Changed {
return nil, errors.New("cannot use `--template` without specifying `--json`")
}
return nil, nil
}
type Exporter interface {
Fields() []string
Write(w io.Writer, data interface{}, colorEnabled bool) error
}
type exportFormat struct {
fields []string
filter string
template string
}
func (e *exportFormat) Fields() []string {
return e.fields
}
// Write serializes data into JSON output written to w. If the object passed as data implements exportable,
// or if data is a map or slice of exportable object, ExportData() will be called on each object to obtain
// raw data for serialization.
func (e *exportFormat) Write(w io.Writer, data interface{}, colorEnabled bool) error {
buf := bytes.Buffer{}
encoder := json.NewEncoder(&buf)
encoder.SetEscapeHTML(false)
if err := encoder.Encode(e.exportData(reflect.ValueOf(data))); err != nil {
return err
}
if e.filter != "" {
return export.FilterJSON(w, &buf, e.filter)
} else if e.template != "" {
return export.ExecuteTemplate(w, &buf, e.template, colorEnabled)
} else if colorEnabled {
return jsoncolor.Write(w, &buf, " ")
}
_, err := io.Copy(w, &buf)
return err
}
func (e *exportFormat) exportData(v reflect.Value) interface{} {
switch v.Kind() {
case reflect.Ptr, reflect.Interface:
if !v.IsNil() {
return e.exportData(v.Elem())
}
case reflect.Slice:
a := make([]interface{}, v.Len())
for i := 0; i < v.Len(); i++ {
a[i] = e.exportData(v.Index(i))
}
return a
case reflect.Map:
t := reflect.MapOf(v.Type().Key(), emptyInterfaceType)
m := reflect.MakeMapWithSize(t, v.Len())
iter := v.MapRange()
for iter.Next() {
ve := reflect.ValueOf(e.exportData(iter.Value()))
m.SetMapIndex(iter.Key(), ve)
}
return m.Interface()
case reflect.Struct:
if v.CanAddr() && reflect.PtrTo(v.Type()).Implements(exportableType) {
ve := v.Addr().Interface().(exportable)
return ve.ExportData(e.fields)
} else if v.Type().Implements(exportableType) {
ve := v.Interface().(exportable)
return ve.ExportData(e.fields)
}
}
return v.Interface()
}
type exportable interface {
ExportData([]string) *map[string]interface{}
}
var exportableType = reflect.TypeOf((*exportable)(nil)).Elem()
var sliceOfEmptyInterface []interface{}
var emptyInterfaceType = reflect.TypeOf(sliceOfEmptyInterface).Elem()