@@ -19,7 +19,7 @@ type JSONFlagError struct {
1919 error
2020}
2121
22- func AddJSONFlags (cmd * cobra.Command , exportTarget * * ExportFormat , fields []string ) {
22+ func AddJSONFlags (cmd * cobra.Command , exportTarget * Exporter , fields []string ) {
2323 f := cmd .Flags ()
2424 f .StringSlice ("json" , nil , "Output JSON with the specified `fields`" )
2525 f .StringP ("jq" , "q" , "" , "Filter JSON output using a jq `expression`" )
@@ -62,9 +62,9 @@ func checkJSONFlags(cmd *cobra.Command) (*ExportFormat, error) {
6262 }
6363 jv := jsonFlag .Value .(pflag.SliceValue )
6464 return & ExportFormat {
65- Fields : jv .GetSlice (),
66- Filter : jqFlag .Value .String (),
67- Template : tplFlag .Value .String (),
65+ fields : jv .GetSlice (),
66+ filter : jqFlag .Value .String (),
67+ template : tplFlag .Value .String (),
6868 }, nil
6969 } else if jqFlag .Changed {
7070 return nil , errors .New ("cannot use `--jq` without specifying `--json`" )
@@ -74,10 +74,19 @@ func checkJSONFlags(cmd *cobra.Command) (*ExportFormat, error) {
7474 return nil , nil
7575}
7676
77+ type Exporter interface {
78+ Fields () []string
79+ Write (w io.Writer , data interface {}, colorEnabled bool ) error
80+ }
81+
7782type ExportFormat struct {
78- Fields []string
79- Filter string
80- Template string
83+ fields []string
84+ filter string
85+ template string
86+ }
87+
88+ func (e * ExportFormat ) Fields () []string {
89+ return e .fields
8190}
8291
8392func (e * ExportFormat ) Write (w io.Writer , data interface {}, colorEnabled bool ) error {
@@ -88,10 +97,10 @@ func (e *ExportFormat) Write(w io.Writer, data interface{}, colorEnabled bool) e
8897 return err
8998 }
9099
91- if e .Filter != "" {
92- return export .FilterJSON (w , & buf , e .Filter )
93- } else if e .Template != "" {
94- return export .ExecuteTemplate (w , & buf , e .Template , colorEnabled )
100+ if e .filter != "" {
101+ return export .FilterJSON (w , & buf , e .filter )
102+ } else if e .template != "" {
103+ return export .ExecuteTemplate (w , & buf , e .template , colorEnabled )
95104 } else if colorEnabled {
96105 return jsoncolor .Write (w , & buf , " " )
97106 }
0 commit comments