From dc05d1212c41d34f23d12cf1957917f5ce022f26 Mon Sep 17 00:00:00 2001 From: Ashley Cui Date: Wed, 14 Sep 2022 15:28:00 -0400 Subject: Add --pretty to podman secret inspect Pretty-print podman secret inspect output in a human-readable format Signed-off-by: Ashley Cui --- cmd/podman/secrets/inspect.go | 42 ++++++++++++++++++++++--- docs/source/markdown/podman-secret-inspect.1.md | 4 +++ test/e2e/secret_test.go | 18 ++++++++++- 3 files changed, 59 insertions(+), 5 deletions(-) diff --git a/cmd/podman/secrets/inspect.go b/cmd/podman/secrets/inspect.go index f4c395b0f..9054fc3b0 100644 --- a/cmd/podman/secrets/inspect.go +++ b/cmd/podman/secrets/inspect.go @@ -25,7 +25,23 @@ var ( } ) -var format string +var ( + format string + pretty bool +) + +const ( + prettyTemplate = `ID: {{.ID}} +Name: {{.Spec.Name}} +{{- if .Spec.Labels }} +Labels: +{{- range $k, $v := .Spec.Labels }} + - {{ $k }}{{if $v }}={{ $v }}{{ end }} +{{- end }}{{ end }} +Driver: {{.Spec.Driver.Name}} +Created at: {{.CreatedAt}} +Updated at: {{.UpdatedAt}}` +) func init() { registry.Commands = append(registry.Commands, registry.CliCommand{ @@ -34,8 +50,11 @@ func init() { }) flags := inspectCmd.Flags() formatFlagName := "format" - flags.StringVarP(&format, formatFlagName, "f", "", "Format volume output using Go template") + flags.StringVarP(&format, formatFlagName, "f", "", "Format inspect output using Go template") _ = inspectCmd.RegisterFlagCompletionFunc(formatFlagName, common.AutocompleteFormat(&entities.SecretInfoReport{})) + + prettyFlagName := "pretty" + flags.BoolVar(&pretty, prettyFlagName, false, "Print inspect output in human-readable format") } func inspect(cmd *cobra.Command, args []string) error { @@ -46,7 +65,21 @@ func inspect(cmd *cobra.Command, args []string) error { inspected = []*entities.SecretInfoReport{} } - if cmd.Flags().Changed("format") { + switch { + case cmd.Flags().Changed("pretty"): + rpt := report.New(os.Stdout, cmd.Name()) + defer rpt.Flush() + + rpt, err := rpt.Parse(report.OriginUser, prettyTemplate) + if err != nil { + return err + } + + if err := rpt.Execute(inspected); err != nil { + return err + } + + case cmd.Flags().Changed("format"): rpt := report.New(os.Stdout, cmd.Name()) defer rpt.Flush() @@ -58,7 +91,8 @@ func inspect(cmd *cobra.Command, args []string) error { if err := rpt.Execute(inspected); err != nil { return err } - } else { + + default: buf, err := json.MarshalIndent(inspected, "", " ") if err != nil { return err diff --git a/docs/source/markdown/podman-secret-inspect.1.md b/docs/source/markdown/podman-secret-inspect.1.md index 77d9276bd..b5bcd2b92 100644 --- a/docs/source/markdown/podman-secret-inspect.1.md +++ b/docs/source/markdown/podman-secret-inspect.1.md @@ -34,6 +34,10 @@ Format secret output using Go template. Print usage statement. +#### **--pretty** + +Print inspect output in human-readable format + ## EXAMPLES diff --git a/test/e2e/secret_test.go b/test/e2e/secret_test.go index 286815e67..668a4943c 100644 --- a/test/e2e/secret_test.go +++ b/test/e2e/secret_test.go @@ -96,6 +96,23 @@ var _ = Describe("Podman secret", func() { Expect(inspect.OutputToString()).To(Equal(secrID)) }) + It("podman secret inspect with --pretty", func() { + secretFilePath := filepath.Join(podmanTest.TempDir, "secret") + err := os.WriteFile(secretFilePath, []byte("mysecret"), 0755) + Expect(err).To(BeNil()) + + session := podmanTest.Podman([]string{"secret", "create", "a", secretFilePath}) + session.WaitWithDefaultTimeout() + secrID := session.OutputToString() + Expect(session).Should(Exit(0)) + + inspect := podmanTest.Podman([]string{"secret", "inspect", "--pretty", secrID}) + inspect.WaitWithDefaultTimeout() + Expect(inspect).Should(Exit(0)) + Expect(inspect.OutputToString()).To(ContainSubstring("Name:")) + Expect(inspect.OutputToString()).To(ContainSubstring(secrID)) + }) + It("podman secret inspect multiple secrets", func() { secretFilePath := filepath.Join(podmanTest.TempDir, "secret") err := os.WriteFile(secretFilePath, []byte("mysecret"), 0755) @@ -125,7 +142,6 @@ var _ = Describe("Podman secret", func() { inspect := podmanTest.Podman([]string{"secret", "inspect", "bogus"}) inspect.WaitWithDefaultTimeout() Expect(inspect).To(ExitWithError()) - }) It("podman secret ls", func() { -- cgit v1.2.3-54-g00ecf