Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 13 additions & 2 deletions cmd/podman/farm/list.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package farm

import (
"errors"
"fmt"
"os"
"sort"
Expand Down Expand Up @@ -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
}{}
)

Expand All @@ -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"
Expand All @@ -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 -}}")
}
Expand Down
11 changes: 11 additions & 0 deletions docs/source/markdown/podman-farm-list.1.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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}}"
Expand Down
31 changes: 31 additions & 0 deletions test/e2e/farm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"}
Expand Down
Loading