Skip to content

Commit

Permalink
Podman system df JSON format outputs Size and Reclaimable
Browse files Browse the repository at this point in the history
Previously, `podman system df --format "{{json .}}"` would not output
`Size` and `Reclaimable` like `podman system df` would.

```
{"Type":"Images","Total":5,"Active":0,"Size":39972240,"Reclaimable":39972240}
{"Type":"Containers","Total":0,"Active":0,"Size":0,"Reclaimable":0}
{"Type":"Local Volumes","Total":0,"Active":0,"Size":0,"Reclaimable":0}
```

Closes: containers#14769

Signed-off-by: Jake Correnti <[email protected]>
  • Loading branch information
Jake Correnti committed Jul 5, 2022
1 parent c66a489 commit 4fe7b8b
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 24 deletions.
48 changes: 24 additions & 24 deletions cmd/podman/system/df.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,11 +78,11 @@ func printSummary(cmd *cobra.Command, reports *entities.SystemDfReport) error {
}
}
imageSummary := dfSummary{
Type: "Images",
Total: len(reports.Images),
Active: active,
size: size,
reclaimable: reclaimable,
Type: "Images",
Total: len(reports.Images),
Active: active,
RawSize: size,
RawReclaimable: reclaimable,
}
dfSummaries = append(dfSummaries, &imageSummary)

Expand All @@ -100,11 +100,11 @@ func printSummary(cmd *cobra.Command, reports *entities.SystemDfReport) error {
conSize += c.RWSize
}
containerSummary := dfSummary{
Type: "Containers",
Total: len(reports.Containers),
Active: conActive,
size: conSize,
reclaimable: conReclaimable,
Type: "Containers",
Total: len(reports.Containers),
Active: conActive,
RawSize: conSize,
RawReclaimable: conReclaimable,
}
dfSummaries = append(dfSummaries, &containerSummary)

Expand All @@ -120,11 +120,11 @@ func printSummary(cmd *cobra.Command, reports *entities.SystemDfReport) error {
volumesReclaimable += v.ReclaimableSize
}
volumeSummary := dfSummary{
Type: "Local Volumes",
Total: len(reports.Volumes),
Active: activeVolumes,
size: volumesSize,
reclaimable: volumesReclaimable,
Type: "Local Volumes",
Total: len(reports.Volumes),
Active: activeVolumes,
RawSize: volumesSize,
RawReclaimable: volumesReclaimable,
}
dfSummaries = append(dfSummaries, &volumeSummary)

Expand Down Expand Up @@ -277,22 +277,22 @@ func (d *dfVolume) Size() string {
}

type dfSummary struct {
Type string
Total int
Active int
size int64
reclaimable int64
Type string
Total int
Active int
RawSize int64 `json:"Size"`
RawReclaimable int64 `json:"Reclaimable"`
}

func (d *dfSummary) Size() string {
return units.HumanSize(float64(d.size))
return units.HumanSize(float64(d.RawSize))
}

func (d *dfSummary) Reclaimable() string {
percent := 0
// make sure to check this to prevent div by zero problems
if d.size > 0 {
percent = int(math.Round(float64(d.reclaimable) / float64(d.size) * float64(100)))
if d.RawSize > 0 {
percent = int(math.Round(float64(d.RawReclaimable) / float64(d.RawSize) * float64(100)))
}
return fmt.Sprintf("%s (%d%%)", units.HumanSize(float64(d.reclaimable)), percent)
return fmt.Sprintf("%s (%d%%)", units.HumanSize(float64(d.RawReclaimable)), percent)
}
13 changes: 13 additions & 0 deletions test/e2e/system_df_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,4 +86,17 @@ var _ = Describe("podman system df", func() {
session.WaitWithDefaultTimeout()
Expect(session).Should(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 4fe7b8b

Please sign in to comment.