Skip to content

Commit f426069

Browse files
authored
Merge pull request #324 from afbjorklund/lima-format
Implement custom go format for list command
2 parents f2dc27f + 4f042c4 commit f426069

File tree

1 file changed

+55
-0
lines changed

1 file changed

+55
-0
lines changed

cmd/limactl/list.go

+55
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,27 @@ import (
44
"encoding/json"
55
"errors"
66
"fmt"
7+
"reflect"
8+
"strings"
79
"text/tabwriter"
10+
"text/template"
811

912
"github.com/docker/go-units"
1013
"github.com/lima-vm/lima/pkg/store"
1114
"github.com/sirupsen/logrus"
1215
"github.com/spf13/cobra"
1316
)
1417

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+
1528
func newListCommand() *cobra.Command {
1629
listCommand := &cobra.Command{
1730
Use: "list [flags] [INSTANCE]...",
@@ -22,6 +35,8 @@ func newListCommand() *cobra.Command {
2235
ValidArgsFunction: listBashComplete,
2336
}
2437

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")
2540
listCommand.Flags().Bool("json", false, "JSONify output")
2641
listCommand.Flags().BoolP("quiet", "q", false, "Only show names")
2742

@@ -43,14 +58,35 @@ func listAction(cmd *cobra.Command, args []string) error {
4358
if err != nil {
4459
return err
4560
}
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+
}
4669
jsonFormat, err := cmd.Flags().GetBool("json")
4770
if err != nil {
4871
return err
4972
}
5073

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+
}
5184
if quiet && jsonFormat {
5285
return errors.New("option --quiet conflicts with --json")
5386
}
87+
if goFormat != "" && jsonFormat {
88+
return errors.New("option --format conflicts with --json")
89+
}
5490

5591
allinstances, err := store.Instances()
5692
if err != nil {
@@ -78,6 +114,25 @@ func listAction(cmd *cobra.Command, args []string) error {
78114
return nil
79115
}
80116

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

0 commit comments

Comments
 (0)