diff --git a/cmd/podman/farm/list.go b/cmd/podman/farm/list.go new file mode 100644 index 0000000000..eb13b6d48a --- /dev/null +++ b/cmd/podman/farm/list.go @@ -0,0 +1,119 @@ +package farm + +import ( + "fmt" + "os" + "sort" + + "github.com/containers/common/pkg/completion" + "github.com/containers/common/pkg/config" + "github.com/containers/common/pkg/report" + "github.com/containers/podman/v4/cmd/podman/common" + "github.com/containers/podman/v4/cmd/podman/registry" + "github.com/containers/podman/v4/cmd/podman/validate" + "github.com/spf13/cobra" +) + +var ( + farmLsDescription = `podman farm ls + +List all available farms. The output of the farms can be filtered +and the output format can be changed to JSON or a user specified Go template.` + lsCommand = &cobra.Command{ + Use: "list [options]", + Aliases: []string{"ls"}, + Args: validate.NoArgs, + Short: "List all existing farms", + Long: farmLsDescription, + RunE: list, + ValidArgsFunction: completion.AutocompleteNone, + } + + // Temporary struct to hold cli values. + lsOpts = struct { + Format string + }{} +) + +func init() { + registry.Commands = append(registry.Commands, registry.CliCommand{ + Command: lsCommand, + Parent: farmCmd, + }) + flags := lsCommand.Flags() + + formatFlagName := "format" + flags.StringVar(&lsOpts.Format, formatFlagName, "", "Format farm output using Go template") + _ = lsCommand.RegisterFlagCompletionFunc(formatFlagName, common.AutocompleteFormat(&farmOut{})) +} + +type farmOut struct { + Name string + Connections []string + Default bool +} + +func list(cmd *cobra.Command, args []string) error { + cfg, err := config.ReadCustomConfig() + if err != nil { + return err + } + + format := lsOpts.Format + if format == "" && len(args) > 0 { + format = "json" + } + + rows := make([]farmOut, 0) + for k, v := range cfg.Farms.List { + defaultFarm := false + if k == cfg.Farms.Default { + defaultFarm = true + } + + r := farmOut{ + Name: k, + Connections: v, + Default: defaultFarm, + } + rows = append(rows, r) + } + + sort.Slice(rows, func(i, j int) bool { + return rows[i].Name < rows[j].Name + }) + + rpt := report.New(os.Stdout, cmd.Name()) + defer rpt.Flush() + + if report.IsJSON(format) { + buf, err := registry.JSONLibrary().MarshalIndent(rows, "", " ") + if err == nil { + fmt.Println(string(buf)) + } + return err + } + + if format != "" { + rpt, err = rpt.Parse(report.OriginUser, format) + } else { + rpt, err = rpt.Parse(report.OriginPodman, + "{{range .}}{{.Name}}\t{{.Connections}}\t{{.Default}}\n{{end -}}") + } + if err != nil { + return err + } + + if rpt.RenderHeaders { + err = rpt.Execute([]map[string]string{{ + "Default": "Default", + "Connections": "Connections", + "Name": "Name", + }}) + if err != nil { + return err + } + } + + return rpt.Execute(rows) +} diff --git a/docs/source/markdown/podman-farm-list.1.md b/docs/source/markdown/podman-farm-list.1.md new file mode 100644 index 0000000000..c30a9ec545 --- /dev/null +++ b/docs/source/markdown/podman-farm-list.1.md @@ -0,0 +1,38 @@ +% podman-farm-list 1 + +## NAME +podman\-farm\-list - List the existing farms + +## SYNOPSIS +**podman farm list** [*options*] + +**podman farm ls** [*options*] + +## DESCRIPTION +List all the existing farms. + +## OPTIONS + +#### **--format**, **-f**=*format* + +Change the default output format. This can be of a supported type like 'json' or a Go template. +Valid placeholders for the Go template listed below: + +| **Placeholder** | **Description** | +| --------------- | ------------------------------------------ | +| .Connections | List of all system connections in the farm | +| .Default | Indicates whether farm is the default | +| .Name | Farm name | + +## EXAMPLE +``` +$ podman farm list +Name Connections Default +farm1 [f38 f37] false +farm2 [f37] true +``` +## SEE ALSO +**[podman(1)](podman.1.md)**, **[podman-farm(1)](podman-farm.1.md)** + +## HISTORY +July 2023, Originally compiled by Urvashi Mohnani (umohnani at redhat dot com)