diff --git a/cmd/podman/system/df.go b/cmd/podman/system/df.go index 962b05ede1..c734bea7e5 100644 --- a/cmd/podman/system/df.go +++ b/cmd/podman/system/df.go @@ -1,6 +1,7 @@ package system import ( + "errors" "fmt" "math" "os" @@ -9,6 +10,7 @@ import ( "github.com/containers/common/pkg/completion" "github.com/containers/common/pkg/report" + "github.com/containers/podman/v4/cmd/podman/common" "github.com/containers/podman/v4/cmd/podman/registry" "github.com/containers/podman/v4/cmd/podman/validate" "github.com/containers/podman/v4/pkg/domain/entities" @@ -46,7 +48,7 @@ func init() { formatFlagName := "format" flags.StringVar(&dfOptions.Format, formatFlagName, "", "Pretty-print images using a Go template") - _ = dfSystemCommand.RegisterFlagCompletionFunc(formatFlagName, completion.AutocompleteNone) + _ = dfSystemCommand.RegisterFlagCompletionFunc(formatFlagName, common.AutocompleteFormat(&dfSummary{})) } func df(cmd *cobra.Command, args []string) error { @@ -55,6 +57,10 @@ func df(cmd *cobra.Command, args []string) error { return err } + if dfOptions.Format != "" && dfOptions.Verbose { + return errors.New("cannot combine --format and --verbose flags") + } + if dfOptions.Verbose { return printVerbose(cmd, reports) } @@ -142,6 +148,9 @@ func printSummary(cmd *cobra.Command, reports *entities.SystemDfReport) error { var err error if cmd.Flags().Changed("format") { + if report.IsJSON(dfOptions.Format) { + return printJSON(dfSummaries) + } rpt, err = rpt.Parse(report.OriginUser, dfOptions.Format) } else { row := "{{range . }}{{.Type}}\t{{.Total}}\t{{.Active}}\t{{.Size}}\t{{.Reclaimable}}\n{{end -}}" @@ -153,6 +162,16 @@ func printSummary(cmd *cobra.Command, reports *entities.SystemDfReport) error { return writeTemplate(rpt, hdrs, dfSummaries) } +func printJSON(data interface{}) error { + bytes, err := json.MarshalIndent(data, "", " ") + if err != nil { + return err + } + + fmt.Println(string(bytes)) + return nil +} + func printVerbose(cmd *cobra.Command, reports *entities.SystemDfReport) error { //nolint:interfacer rpt := report.New(os.Stdout, cmd.Name()) defer rpt.Flush() diff --git a/docs/source/markdown/podman-system-df.1.md b/docs/source/markdown/podman-system-df.1.md index ac37d5e4d4..4e414ce14e 100644 --- a/docs/source/markdown/podman-system-df.1.md +++ b/docs/source/markdown/podman-system-df.1.md @@ -12,7 +12,7 @@ Show podman disk usage ## OPTIONS #### **--format**=*format* -Pretty-print images using a Go template +Pretty-print images using a Go template or JSON. This flag is not allowed in combination with **--verbose** #### **--verbose**, **-v** Show detailed information on space usage diff --git a/test/e2e/system_df_test.go b/test/e2e/system_df_test.go index 998fa8b59f..04221847e0 100644 --- a/test/e2e/system_df_test.go +++ b/test/e2e/system_df_test.go @@ -106,8 +106,29 @@ var _ = Describe("podman system df", func() { session = podmanTest.Podman([]string{"system", "df", "--format", "{{ json . }}"}) session.WaitWithDefaultTimeout() Expect(session).Should(Exit(0)) - Expect(session.LineInOutputContains("Size")) - Expect(session.LineInOutputContains("Reclaimable")) - Expect(session.IsJSONOutputValid()) + Expect(session.OutputToString()).To(ContainSubstring("Size")) + Expect(session.OutputToString()).To(ContainSubstring("Reclaimable")) + Expect(session.OutputToString()).To(BeValidJSON()) }) + + It("podman system df --format with --verbose", func() { + session := podmanTest.Podman([]string{"system", "df", "--format", "json", "--verbose"}) + session.WaitWithDefaultTimeout() + Expect(session).To(ExitWithError()) + Expect(session.ErrorToString()).To(Equal("Error: cannot combine --format and --verbose flags")) + }) + + It("podman system df --format json", func() { + session := podmanTest.Podman([]string{"create", ALPINE}) + session.WaitWithDefaultTimeout() + Expect(session).Should(Exit(0)) + + session = podmanTest.Podman([]string{"system", "df", "--format", "json"}) + session.WaitWithDefaultTimeout() + Expect(session).Should(Exit(0)) + Expect(session.OutputToString()).To(ContainSubstring("Size")) + Expect(session.OutputToString()).To(ContainSubstring("Reclaimable")) + Expect(session.OutputToString()).To(BeValidJSON()) + }) + })