@@ -4,14 +4,27 @@ import (
4
4
"encoding/json"
5
5
"errors"
6
6
"fmt"
7
+ "reflect"
8
+ "strings"
7
9
"text/tabwriter"
10
+ "text/template"
8
11
9
12
"github.com/docker/go-units"
10
13
"github.com/lima-vm/lima/pkg/store"
11
14
"github.com/sirupsen/logrus"
12
15
"github.com/spf13/cobra"
13
16
)
14
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
+
15
28
func newListCommand () * cobra.Command {
16
29
listCommand := & cobra.Command {
17
30
Use : "list [flags] [INSTANCE]..." ,
@@ -22,6 +35,8 @@ func newListCommand() *cobra.Command {
22
35
ValidArgsFunction : listBashComplete ,
23
36
}
24
37
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" )
25
40
listCommand .Flags ().Bool ("json" , false , "JSONify output" )
26
41
listCommand .Flags ().BoolP ("quiet" , "q" , false , "Only show names" )
27
42
@@ -43,14 +58,35 @@ func listAction(cmd *cobra.Command, args []string) error {
43
58
if err != nil {
44
59
return err
45
60
}
61
+ goFormat , err := cmd .Flags ().GetString ("format" )
62
+ if err != nil {
63
+ return err
64
+ }
65
+ listFields , err := cmd .Flags ().GetBool ("list-fields" )
66
+ if err != nil {
67
+ return err
68
+ }
46
69
jsonFormat , err := cmd .Flags ().GetBool ("json" )
47
70
if err != nil {
48
71
return err
49
72
}
50
73
74
+ if goFormat != "" && listFields {
75
+ return errors .New ("option --format conflicts with --list-fields" )
76
+ }
77
+ if jsonFormat && listFields {
78
+ return errors .New ("option --json conflicts with --list-fields" )
79
+ }
80
+ if listFields {
81
+ fmt .Println (strings .Join (instanceFields (), "\n " ))
82
+ return nil
83
+ }
51
84
if quiet && jsonFormat {
52
85
return errors .New ("option --quiet conflicts with --json" )
53
86
}
87
+ if goFormat != "" && jsonFormat {
88
+ return errors .New ("option --format conflicts with --json" )
89
+ }
54
90
55
91
allinstances , err := store .Instances ()
56
92
if err != nil {
@@ -78,6 +114,25 @@ func listAction(cmd *cobra.Command, args []string) error {
78
114
return nil
79
115
}
80
116
117
+ if goFormat != "" {
118
+ tmpl , err := template .New ("format" ).Parse (goFormat )
119
+ if err != nil {
120
+ return err
121
+ }
122
+ for _ , instName := range instances {
123
+ inst , err := store .Inspect (instName )
124
+ if err != nil {
125
+ logrus .WithError (err ).Errorf ("instance %q does not exist?" , instName )
126
+ continue
127
+ }
128
+ err = tmpl .Execute (cmd .OutOrStdout (), inst )
129
+ if err != nil {
130
+ return err
131
+ }
132
+ fmt .Fprintln (cmd .OutOrStdout ())
133
+ }
134
+ return nil
135
+ }
81
136
if jsonFormat {
82
137
for _ , instName := range instances {
83
138
inst , err := store .Inspect (instName )
0 commit comments