Skip to content

Commit 090eea7

Browse files
committed
Implement custom go format for list command
Signed-off-by: Anders F Björklund <[email protected]>
1 parent 2d0572d commit 090eea7

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

cmd/limactl/list.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"errors"
66
"fmt"
77
"text/tabwriter"
8+
"text/template"
89

910
"github.com/docker/go-units"
1011
"github.com/lima-vm/lima/pkg/store"
@@ -22,6 +23,7 @@ func newListCommand() *cobra.Command {
2223
ValidArgsFunction: cobra.NoFileCompletions,
2324
}
2425

26+
listCommand.Flags().StringP("format", "f", "", "Format the output using the given Go template")
2527
listCommand.Flags().Bool("json", false, "JSONify output")
2628
listCommand.Flags().BoolP("quiet", "q", false, "Only show names")
2729

@@ -33,6 +35,10 @@ func listAction(cmd *cobra.Command, args []string) error {
3335
if err != nil {
3436
return err
3537
}
38+
goFormat, err := cmd.Flags().GetString("format")
39+
if err != nil {
40+
return err
41+
}
3642
jsonFormat, err := cmd.Flags().GetBool("json")
3743
if err != nil {
3844
return err
@@ -41,6 +47,9 @@ func listAction(cmd *cobra.Command, args []string) error {
4147
if quiet && jsonFormat {
4248
return errors.New("option --quiet conflicts with --json")
4349
}
50+
if goFormat != "" && jsonFormat {
51+
return errors.New("option --format conflicts with --json")
52+
}
4453

4554
instances, err := store.Instances()
4655
if err != nil {
@@ -54,6 +63,25 @@ func listAction(cmd *cobra.Command, args []string) error {
5463
return nil
5564
}
5665

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+
}
5785
if jsonFormat {
5886
for _, instName := range instances {
5987
inst, err := store.Inspect(instName)

0 commit comments

Comments
 (0)