Skip to content

Commit

Permalink
Fix system df issues with -f and -v
Browse files Browse the repository at this point in the history
Fixed the issue of `--format` and `--verbose` flags being allowed in
combination with one another.

Implemented functionality for `--format json` or `--format '{{ json }}' `.

Implemented command-completion help for `--format`.

Fixes: #16204

Signed-off-by: Jake Correnti <jcorrenti13@gmail.com>
  • Loading branch information
Jake Correnti committed Oct 20, 2022
1 parent a30c9ef commit 7da7d90
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 1 deletion.
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 @@ -139,6 +145,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 @@ -150,6 +159,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
20 changes: 20 additions & 0 deletions test/e2e/system_df_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,4 +110,24 @@ var _ = Describe("podman system df", func() {
Expect(session.LineInOutputContains("Reclaimable"))
Expect(session.IsJSONOutputValid())
})

It("podman system df --format with --verbose", func() {
session := podmanTest.Podman([]string{"system", "df", "--format", "json", "--verbose"})
session.WaitWithDefaultTimeout()
Expect(session).ShouldNot(Exit(0))
})

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.LineInOutputContains("Size"))
Expect(session.LineInOutputContains("Reclaimable"))
Expect(session.IsJSONOutputValid())
})

})

0 comments on commit 7da7d90

Please sign in to comment.