Merge pull request #16234 from jakecorrenti/system-df-verbose-format-usability

Fix `system df` issues with `-f` and `-v`
This commit is contained in:
OpenShift Merge Robot
2022-10-24 11:44:09 -04:00
committed by GitHub
3 changed files with 45 additions and 5 deletions

View File

@ -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()

View File

@ -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

View File

@ -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())
})
})