Skip to content

Commit

Permalink
Merge pull request containers#15392 from ashley-cui/quiet
Browse files Browse the repository at this point in the history
Add quiet/q flag to podman secret ls
  • Loading branch information
openshift-merge-robot authored Aug 22, 2022
2 parents aefd0ae + eee0ec9 commit 51d4b88
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 2 deletions.
22 changes: 21 additions & 1 deletion cmd/podman/secrets/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ type listFlagType struct {
format string
noHeading bool
filter []string
quiet bool
}

func init() {
Expand All @@ -43,13 +44,20 @@ func init() {
})

flags := lsCmd.Flags()

formatFlagName := "format"
flags.StringVar(&listFlag.format, formatFlagName, "{{.ID}}\t{{.Name}}\t{{.Driver}}\t{{.CreatedAt}}\t{{.UpdatedAt}}\t\n", "Format volume output using Go template")
_ = lsCmd.RegisterFlagCompletionFunc(formatFlagName, common.AutocompleteFormat(&entities.SecretInfoReport{}))

filterFlagName := "filter"
flags.StringSliceVarP(&listFlag.filter, filterFlagName, "f", []string{}, "Filter secret output")
_ = lsCmd.RegisterFlagCompletionFunc(filterFlagName, common.AutocompleteSecretFilters)
flags.BoolVar(&listFlag.noHeading, "noheading", false, "Do not print headers")

noHeadingFlagName := "noheading"
flags.BoolVar(&listFlag.noHeading, noHeadingFlagName, false, "Do not print headers")

quietFlagName := "quiet"
flags.BoolVarP(&listFlag.quiet, quietFlagName, "q", false, "Print secret IDs only")
}

func ls(cmd *cobra.Command, args []string) error {
Expand All @@ -76,9 +84,21 @@ func ls(cmd *cobra.Command, args []string) error {
Driver: response.Spec.Driver.Name,
})
}

if listFlag.quiet && !cmd.Flags().Changed("format") {
return quietOut(listed)
}

return outputTemplate(cmd, listed)
}

func quietOut(responses []*entities.SecretListReport) error {
for _, response := range responses {
fmt.Println(response.ID)
}
return nil
}

func outputTemplate(cmd *cobra.Command, responses []*entities.SecretListReport) error {
headers := report.Headers(entities.SecretListReport{}, map[string]string{
"CreatedAt": "CREATED",
Expand Down
6 changes: 5 additions & 1 deletion docs/source/markdown/podman-secret-ls.1.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,11 @@ Format secret output using Go template.

#### **--noheading**

Omit the table headings from the listing of secrets. .
Omit the table headings from the listing of secrets.

#### **--quiet**, **-q**

Print secret IDs only.

## EXAMPLES

Expand Down
30 changes: 30 additions & 0 deletions test/e2e/secret_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,36 @@ var _ = Describe("Podman secret", func() {

})

It("podman secret ls --quiet", func() {
secretFilePath := filepath.Join(podmanTest.TempDir, "secret")
err := ioutil.WriteFile(secretFilePath, []byte("mysecret"), 0755)
Expect(err).To(BeNil())

secretName := "a"

session := podmanTest.Podman([]string{"secret", "create", secretName, secretFilePath})
session.WaitWithDefaultTimeout()
Expect(session).Should(Exit(0))
secretID := session.OutputToString()

list := podmanTest.Podman([]string{"secret", "ls", "-q"})
list.WaitWithDefaultTimeout()
Expect(list).Should(Exit(0))
Expect(list.OutputToString()).To(Equal(secretID))

list = podmanTest.Podman([]string{"secret", "ls", "--quiet"})
list.WaitWithDefaultTimeout()
Expect(list).Should(Exit(0))
Expect(list.OutputToString()).To(Equal(secretID))

// Prefer format over quiet
list = podmanTest.Podman([]string{"secret", "ls", "-q", "--format", "{{.Name}}"})
list.WaitWithDefaultTimeout()
Expect(list).Should(Exit(0))
Expect(list.OutputToString()).To(Equal(secretName))

})

It("podman secret ls with filters", func() {
secretFilePath := filepath.Join(podmanTest.TempDir, "secret")
err := ioutil.WriteFile(secretFilePath, []byte("mysecret"), 0755)
Expand Down

0 comments on commit 51d4b88

Please sign in to comment.