Skip to content

Commit

Permalink
Merge pull request #16234 from jakecorrenti/system-df-verbose-format-…
Browse files Browse the repository at this point in the history
…usability

Fix `system df` issues with `-f` and `-v`
  • Loading branch information
openshift-merge-robot authored Oct 24, 2022
2 parents 1b94470 + e5032a8 commit 63a8f9a
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 5 deletions.
21 changes: 20 additions & 1 deletion cmd/podman/system/df.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package system

import (
"errors"
"fmt"
"math"
"os"
Expand All @@ -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"
Expand Down Expand Up @@ -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 {
Expand All @@ -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)
}
Expand Down Expand Up @@ -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 -}}"
Expand All @@ -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()
Expand Down
2 changes: 1 addition & 1 deletion docs/source/markdown/podman-system-df.1.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
27 changes: 24 additions & 3 deletions test/e2e/system_df_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
})

})

0 comments on commit 63a8f9a

Please sign in to comment.