Skip to content

Commit

Permalink
Merge pull request #324 from afbjorklund/lima-format
Browse files Browse the repository at this point in the history
Implement custom go format for list command
  • Loading branch information
AkihiroSuda authored Nov 18, 2021
2 parents f2dc27f + 4f042c4 commit f426069
Showing 1 changed file with 55 additions and 0 deletions.
55 changes: 55 additions & 0 deletions cmd/limactl/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,27 @@ import (
"encoding/json"
"errors"
"fmt"
"reflect"
"strings"
"text/tabwriter"
"text/template"

"github.com/docker/go-units"
"github.com/lima-vm/lima/pkg/store"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
)

func instanceFields() []string {
fields := []string{}
var instance store.Instance
t := reflect.TypeOf(instance)
for i := 0; i < t.NumField(); i++ {
fields = append(fields, t.Field(i).Name)
}
return fields
}

func newListCommand() *cobra.Command {
listCommand := &cobra.Command{
Use: "list [flags] [INSTANCE]...",
Expand All @@ -22,6 +35,8 @@ func newListCommand() *cobra.Command {
ValidArgsFunction: listBashComplete,
}

listCommand.Flags().StringP("format", "f", "", "Format the output using the given Go template")
listCommand.Flags().Bool("list-fields", false, "List fields available for format")
listCommand.Flags().Bool("json", false, "JSONify output")
listCommand.Flags().BoolP("quiet", "q", false, "Only show names")

Expand All @@ -43,14 +58,35 @@ func listAction(cmd *cobra.Command, args []string) error {
if err != nil {
return err
}
goFormat, err := cmd.Flags().GetString("format")
if err != nil {
return err
}
listFields, err := cmd.Flags().GetBool("list-fields")
if err != nil {
return err
}
jsonFormat, err := cmd.Flags().GetBool("json")
if err != nil {
return err
}

if goFormat != "" && listFields {
return errors.New("option --format conflicts with --list-fields")
}
if jsonFormat && listFields {
return errors.New("option --json conflicts with --list-fields")
}
if listFields {
fmt.Println(strings.Join(instanceFields(), "\n"))
return nil
}
if quiet && jsonFormat {
return errors.New("option --quiet conflicts with --json")
}
if goFormat != "" && jsonFormat {
return errors.New("option --format conflicts with --json")
}

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

if goFormat != "" {
tmpl, err := template.New("format").Parse(goFormat)
if err != nil {
return err
}
for _, instName := range instances {
inst, err := store.Inspect(instName)
if err != nil {
logrus.WithError(err).Errorf("instance %q does not exist?", instName)
continue
}
err = tmpl.Execute(cmd.OutOrStdout(), inst)
if err != nil {
return err
}
fmt.Fprintln(cmd.OutOrStdout())
}
return nil
}
if jsonFormat {
for _, instName := range instances {
inst, err := store.Inspect(instName)
Expand Down

0 comments on commit f426069

Please sign in to comment.