Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix system df issues with -f and -v #16234

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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())
})

})