mirror of
https://github.com/containers/podman.git
synced 2025-05-21 17:16:22 +08:00
Merge pull request #16234 from jakecorrenti/system-df-verbose-format-usability
Fix `system df` issues with `-f` and `-v`
This commit is contained in:
@ -1,6 +1,7 @@
|
|||||||
package system
|
package system
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"math"
|
"math"
|
||||||
"os"
|
"os"
|
||||||
@ -9,6 +10,7 @@ import (
|
|||||||
|
|
||||||
"github.com/containers/common/pkg/completion"
|
"github.com/containers/common/pkg/completion"
|
||||||
"github.com/containers/common/pkg/report"
|
"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/registry"
|
||||||
"github.com/containers/podman/v4/cmd/podman/validate"
|
"github.com/containers/podman/v4/cmd/podman/validate"
|
||||||
"github.com/containers/podman/v4/pkg/domain/entities"
|
"github.com/containers/podman/v4/pkg/domain/entities"
|
||||||
@ -46,7 +48,7 @@ func init() {
|
|||||||
|
|
||||||
formatFlagName := "format"
|
formatFlagName := "format"
|
||||||
flags.StringVar(&dfOptions.Format, formatFlagName, "", "Pretty-print images using a Go template")
|
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 {
|
func df(cmd *cobra.Command, args []string) error {
|
||||||
@ -55,6 +57,10 @@ func df(cmd *cobra.Command, args []string) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if dfOptions.Format != "" && dfOptions.Verbose {
|
||||||
|
return errors.New("cannot combine --format and --verbose flags")
|
||||||
|
}
|
||||||
|
|
||||||
if dfOptions.Verbose {
|
if dfOptions.Verbose {
|
||||||
return printVerbose(cmd, reports)
|
return printVerbose(cmd, reports)
|
||||||
}
|
}
|
||||||
@ -142,6 +148,9 @@ func printSummary(cmd *cobra.Command, reports *entities.SystemDfReport) error {
|
|||||||
|
|
||||||
var err error
|
var err error
|
||||||
if cmd.Flags().Changed("format") {
|
if cmd.Flags().Changed("format") {
|
||||||
|
if report.IsJSON(dfOptions.Format) {
|
||||||
|
return printJSON(dfSummaries)
|
||||||
|
}
|
||||||
rpt, err = rpt.Parse(report.OriginUser, dfOptions.Format)
|
rpt, err = rpt.Parse(report.OriginUser, dfOptions.Format)
|
||||||
} else {
|
} else {
|
||||||
row := "{{range . }}{{.Type}}\t{{.Total}}\t{{.Active}}\t{{.Size}}\t{{.Reclaimable}}\n{{end -}}"
|
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)
|
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
|
func printVerbose(cmd *cobra.Command, reports *entities.SystemDfReport) error { //nolint:interfacer
|
||||||
rpt := report.New(os.Stdout, cmd.Name())
|
rpt := report.New(os.Stdout, cmd.Name())
|
||||||
defer rpt.Flush()
|
defer rpt.Flush()
|
||||||
|
@ -12,7 +12,7 @@ Show podman disk usage
|
|||||||
## OPTIONS
|
## OPTIONS
|
||||||
#### **--format**=*format*
|
#### **--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**
|
#### **--verbose**, **-v**
|
||||||
Show detailed information on space usage
|
Show detailed information on space usage
|
||||||
|
@ -106,8 +106,29 @@ var _ = Describe("podman system df", func() {
|
|||||||
session = podmanTest.Podman([]string{"system", "df", "--format", "{{ json . }}"})
|
session = podmanTest.Podman([]string{"system", "df", "--format", "{{ json . }}"})
|
||||||
session.WaitWithDefaultTimeout()
|
session.WaitWithDefaultTimeout()
|
||||||
Expect(session).Should(Exit(0))
|
Expect(session).Should(Exit(0))
|
||||||
Expect(session.LineInOutputContains("Size"))
|
Expect(session.OutputToString()).To(ContainSubstring("Size"))
|
||||||
Expect(session.LineInOutputContains("Reclaimable"))
|
Expect(session.OutputToString()).To(ContainSubstring("Reclaimable"))
|
||||||
Expect(session.IsJSONOutputValid())
|
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())
|
||||||
|
})
|
||||||
|
|
||||||
})
|
})
|
||||||
|
Reference in New Issue
Block a user