4
4
"encoding/json"
5
5
"errors"
6
6
"fmt"
7
+ "reflect"
8
+ "strings"
7
9
"text/tabwriter"
8
10
"text/template"
9
11
@@ -13,6 +15,16 @@ import (
13
15
"github.com/spf13/cobra"
14
16
)
15
17
18
+ func instanceFields () []string {
19
+ fields := []string {}
20
+ var instance store.Instance
21
+ t := reflect .TypeOf (instance )
22
+ for i := 0 ; i < t .NumField (); i ++ {
23
+ fields = append (fields , t .Field (i ).Name )
24
+ }
25
+ return fields
26
+ }
27
+
16
28
func newListCommand () * cobra.Command {
17
29
listCommand := & cobra.Command {
18
30
Use : "list" ,
@@ -24,6 +36,7 @@ func newListCommand() *cobra.Command {
24
36
}
25
37
26
38
listCommand .Flags ().StringP ("format" , "f" , "" , "Format the output using the given Go template" )
39
+ listCommand .Flags ().Bool ("list-fields" , false , "List fields available for format" )
27
40
listCommand .Flags ().Bool ("json" , false , "JSONify output" )
28
41
listCommand .Flags ().BoolP ("quiet" , "q" , false , "Only show names" )
29
42
@@ -39,11 +52,25 @@ func listAction(cmd *cobra.Command, args []string) error {
39
52
if err != nil {
40
53
return err
41
54
}
55
+ listFields , err := cmd .Flags ().GetBool ("list-fields" )
56
+ if err != nil {
57
+ return err
58
+ }
42
59
jsonFormat , err := cmd .Flags ().GetBool ("json" )
43
60
if err != nil {
44
61
return err
45
62
}
46
63
64
+ if goFormat != "" && listFields {
65
+ return errors .New ("option --format conflicts with --list-fields" )
66
+ }
67
+ if jsonFormat && listFields {
68
+ return errors .New ("option --json conflicts with --list-fields" )
69
+ }
70
+ if listFields {
71
+ fmt .Println (strings .Join (instanceFields (), "\n " ))
72
+ return nil
73
+ }
47
74
if quiet && jsonFormat {
48
75
return errors .New ("option --quiet conflicts with --json" )
49
76
}
0 commit comments