diff --git a/cmd/limactl/list.go b/cmd/limactl/list.go index be44a2e1396..1b58f13ef2f 100644 --- a/cmd/limactl/list.go +++ b/cmd/limactl/list.go @@ -4,6 +4,8 @@ import ( "encoding/json" "errors" "fmt" + "reflect" + "strings" "text/tabwriter" "text/template" @@ -13,6 +15,16 @@ import ( "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", @@ -24,6 +36,7 @@ func newListCommand() *cobra.Command { } 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") @@ -39,11 +52,25 @@ func listAction(cmd *cobra.Command, args []string) error { 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") }