5
5
"errors"
6
6
"fmt"
7
7
"text/tabwriter"
8
+ "text/template"
8
9
9
10
"github.com/docker/go-units"
10
11
"github.com/lima-vm/lima/pkg/store"
@@ -22,6 +23,7 @@ func newListCommand() *cobra.Command {
22
23
ValidArgsFunction : cobra .NoFileCompletions ,
23
24
}
24
25
26
+ listCommand .Flags ().StringP ("format" , "f" , "" , "Format the output using the given Go template" )
25
27
listCommand .Flags ().Bool ("json" , false , "JSONify output" )
26
28
listCommand .Flags ().BoolP ("quiet" , "q" , false , "Only show names" )
27
29
@@ -33,6 +35,10 @@ func listAction(cmd *cobra.Command, args []string) error {
33
35
if err != nil {
34
36
return err
35
37
}
38
+ goFormat , err := cmd .Flags ().GetString ("format" )
39
+ if err != nil {
40
+ return err
41
+ }
36
42
jsonFormat , err := cmd .Flags ().GetBool ("json" )
37
43
if err != nil {
38
44
return err
@@ -41,6 +47,9 @@ func listAction(cmd *cobra.Command, args []string) error {
41
47
if quiet && jsonFormat {
42
48
return errors .New ("option --quiet conflicts with --json" )
43
49
}
50
+ if goFormat != "" && jsonFormat {
51
+ return errors .New ("option --format conflicts with --json" )
52
+ }
44
53
45
54
instances , err := store .Instances ()
46
55
if err != nil {
@@ -54,6 +63,25 @@ func listAction(cmd *cobra.Command, args []string) error {
54
63
return nil
55
64
}
56
65
66
+ if goFormat != "" {
67
+ tmpl , err := template .New ("format" ).Parse (goFormat )
68
+ if err != nil {
69
+ return err
70
+ }
71
+ for _ , instName := range instances {
72
+ inst , err := store .Inspect (instName )
73
+ if err != nil {
74
+ logrus .WithError (err ).Errorf ("instance %q does not exist?" , instName )
75
+ continue
76
+ }
77
+ err = tmpl .Execute (cmd .OutOrStdout (), inst )
78
+ if err != nil {
79
+ return err
80
+ }
81
+ fmt .Fprintln (cmd .OutOrStdout ())
82
+ }
83
+ return nil
84
+ }
57
85
if jsonFormat {
58
86
for _ , instName := range instances {
59
87
inst , err := store .Inspect (instName )
0 commit comments