mirror of
https://github.com/containers/podman.git
synced 2025-10-26 02:35:43 +08:00
Add quiet/q flag to podman secret ls
Add quiet/q flag to podman secret ls, which will print only the secret ID. Signed-off-by: Ashley Cui <acui@redhat.com>
This commit is contained in:
@ -34,6 +34,7 @@ type listFlagType struct {
|
|||||||
format string
|
format string
|
||||||
noHeading bool
|
noHeading bool
|
||||||
filter []string
|
filter []string
|
||||||
|
quiet bool
|
||||||
}
|
}
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
@ -43,13 +44,20 @@ func init() {
|
|||||||
})
|
})
|
||||||
|
|
||||||
flags := lsCmd.Flags()
|
flags := lsCmd.Flags()
|
||||||
|
|
||||||
formatFlagName := "format"
|
formatFlagName := "format"
|
||||||
flags.StringVar(&listFlag.format, formatFlagName, "{{.ID}}\t{{.Name}}\t{{.Driver}}\t{{.CreatedAt}}\t{{.UpdatedAt}}\t\n", "Format volume output using Go template")
|
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{}))
|
_ = lsCmd.RegisterFlagCompletionFunc(formatFlagName, common.AutocompleteFormat(&entities.SecretInfoReport{}))
|
||||||
|
|
||||||
filterFlagName := "filter"
|
filterFlagName := "filter"
|
||||||
flags.StringSliceVarP(&listFlag.filter, filterFlagName, "f", []string{}, "Filter secret output")
|
flags.StringSliceVarP(&listFlag.filter, filterFlagName, "f", []string{}, "Filter secret output")
|
||||||
_ = lsCmd.RegisterFlagCompletionFunc(filterFlagName, common.AutocompleteSecretFilters)
|
_ = 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 {
|
func ls(cmd *cobra.Command, args []string) error {
|
||||||
@ -76,9 +84,21 @@ func ls(cmd *cobra.Command, args []string) error {
|
|||||||
Driver: response.Spec.Driver.Name,
|
Driver: response.Spec.Driver.Name,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if listFlag.quiet && !cmd.Flags().Changed("format") {
|
||||||
|
return quietOut(listed)
|
||||||
|
}
|
||||||
|
|
||||||
return outputTemplate(cmd, 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 {
|
func outputTemplate(cmd *cobra.Command, responses []*entities.SecretListReport) error {
|
||||||
headers := report.Headers(entities.SecretListReport{}, map[string]string{
|
headers := report.Headers(entities.SecretListReport{}, map[string]string{
|
||||||
"CreatedAt": "CREATED",
|
"CreatedAt": "CREATED",
|
||||||
|
|||||||
@ -30,7 +30,11 @@ Format secret output using Go template.
|
|||||||
|
|
||||||
#### **--noheading**
|
#### **--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
|
## EXAMPLES
|
||||||
|
|
||||||
|
|||||||
@ -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() {
|
It("podman secret ls with filters", func() {
|
||||||
secretFilePath := filepath.Join(podmanTest.TempDir, "secret")
|
secretFilePath := filepath.Join(podmanTest.TempDir, "secret")
|
||||||
err := ioutil.WriteFile(secretFilePath, []byte("mysecret"), 0755)
|
err := ioutil.WriteFile(secretFilePath, []byte("mysecret"), 0755)
|
||||||
|
|||||||
Reference in New Issue
Block a user