diff --git a/cmd/podman/farm/list.go b/cmd/podman/farm/list.go index ec5a68b7c37..480181c9721 100644 --- a/cmd/podman/farm/list.go +++ b/cmd/podman/farm/list.go @@ -1,6 +1,7 @@ package farm import ( + "errors" "fmt" "os" "sort" @@ -34,6 +35,7 @@ and the output format can be changed to JSON or a user specified Go template.` // Temporary struct to hold cli values. lsOpts = struct { Format string + Quiet bool }{} ) @@ -47,9 +49,15 @@ func init() { formatFlagName := "format" flags.StringVar(&lsOpts.Format, formatFlagName, "", "Format farm output using Go template") _ = lsCommand.RegisterFlagCompletionFunc(formatFlagName, common.AutocompleteFormat(&config.Farm{})) + + flags.BoolVarP(&lsOpts.Quiet, "quiet", "q", false, "Print farm names only") } func list(cmd *cobra.Command, args []string) error { + if lsOpts.Quiet && cmd.Flag("format").Changed { + return errors.New("quiet and format flags cannot be used together") + } + format := lsOpts.Format if format == "" && len(args) > 0 { format = "json" @@ -75,9 +83,12 @@ func list(cmd *cobra.Command, args []string) error { return err } - if format != "" { + switch { + case format != "": rpt, err = rpt.Parse(report.OriginUser, format) - } else { + case lsOpts.Quiet: + rpt, err = rpt.Parse(report.OriginUser, "{{range .}}{{.Name}}\n{{end -}}") + default: rpt, err = rpt.Parse(report.OriginPodman, "{{range .}}{{.Name}}\t{{.Connections}}\t{{.Default}}\t{{.ReadWrite}}\n{{end -}}") } diff --git a/docs/source/markdown/podman-farm-list.1.md b/docs/source/markdown/podman-farm-list.1.md index bc9d6b3d00c..ed4ba822502 100644 --- a/docs/source/markdown/podman-farm-list.1.md +++ b/docs/source/markdown/podman-farm-list.1.md @@ -25,6 +25,10 @@ Valid placeholders for the Go template listed below: | .Name | Farm name | | .ReadWrite | Indicates if this farm can be modified using the podman farm commands | +#### **--quiet**, **-q** + +Print farm output in quiet mode. Only print the farm names. + ## EXAMPLE List all farms: @@ -65,6 +69,13 @@ farm1 farm2 ``` +Show only farm names using **--quiet**: +``` +$ podman farm list --quiet +farm1 +farm2 +``` + Show detailed farm information: ``` $ podman farm list --format "Farm: {{.Name}} (Default: {{.Default}}, ReadWrite: {{.ReadWrite}})\nConnections: {{.Connections}}" diff --git a/test/e2e/farm_test.go b/test/e2e/farm_test.go index a4833976b2a..fd7c503f2b4 100644 --- a/test/e2e/farm_test.go +++ b/test/e2e/farm_test.go @@ -80,6 +80,37 @@ farm3 [] false true Expect(session).Should(Not(ExitCleanly())) }) + It("list farms quiet", func() { + // create farm with multiple system connections + cmd := []string{"farm", "create", "farm1", "QA", "QB"} + session := podmanTest.Podman(cmd) + session.WaitWithDefaultTimeout() + Expect(session).Should(ExitCleanly()) + + // create farm with only one system connection + cmd = []string{"farm", "create", "farm2", "QA"} + session = podmanTest.Podman(cmd) + session.WaitWithDefaultTimeout() + Expect(session).Should(ExitCleanly()) + + // --quiet should print only farm names, one per line + session = podmanTest.Podman([]string{"farm", "list", "--quiet"}) + session.WaitWithDefaultTimeout() + Expect(session).Should(ExitCleanly()) + Expect(string(session.Out.Contents())).To(Equal("farm1\nfarm2\n")) + + // -q shorthand behaves the same as --quiet + session = podmanTest.Podman([]string{"farm", "list", "-q"}) + session.WaitWithDefaultTimeout() + Expect(session).Should(ExitCleanly()) + Expect(string(session.Out.Contents())).To(Equal("farm1\nfarm2\n")) + + // --quiet and --format together should error + session = podmanTest.Podman([]string{"farm", "list", "--quiet", "--format", "{{.Name}}"}) + session.WaitWithDefaultTimeout() + Expect(session).Should(ExitWithError(125, "quiet and format flags cannot be used together")) + }) + It("update existing farms", func() { // create farm with multiple system connections cmd := []string{"farm", "create", "farm1", "QA", "QB"}