Output Size and Reclaimable in human form for json output

Signed-off-by: Daniel J Walsh <dwalsh@redhat.com>
This commit is contained in:
Daniel J Walsh 2022-12-20 15:12:40 -05:00
parent aecb5d3853
commit 24b1e81c5d
No known key found for this signature in database
GPG Key ID: A2DF901DABE2C028
2 changed files with 59 additions and 1 deletions

View File

@ -1,6 +1,7 @@
package system
import (
"bytes"
"errors"
"fmt"
"math"
@ -145,6 +146,7 @@ func printSummary(cmd *cobra.Command, reports *entities.SystemDfReport) error {
rpt := report.New(os.Stdout, cmd.Name())
defer rpt.Flush()
report.DefaultFuncs["json"] = rptJSON
var err error
if cmd.Flags().Changed("format") {
@ -162,7 +164,46 @@ func printSummary(cmd *cobra.Command, reports *entities.SystemDfReport) error {
return writeTemplate(rpt, hdrs, dfSummaries)
}
func printJSON(data interface{}) error {
type jsonSummary struct {
Type string
Total int
Active int
RawSize int64
RawReclaimable int64
TotalCount int
Size string
Reclaimable string
}
func jsonConvert(df *dfSummary) *jsonSummary {
return &jsonSummary{
Type: df.Type,
Total: df.Total,
TotalCount: df.Total,
Active: df.Active,
RawSize: df.RawSize,
RawReclaimable: df.RawReclaimable,
Size: df.Size(),
Reclaimable: df.Reclaimable(),
}
}
func rptJSON(df *dfSummary) string {
buf := new(bytes.Buffer)
enc := json.NewEncoder(buf)
enc.SetEscapeHTML(false)
_ = enc.Encode(jsonConvert(df))
// Remove the trailing new line added by the encoder
return strings.TrimSpace(buf.String())
}
func printJSON(dfSummaries []*dfSummary) error {
data := make([]jsonSummary, len(dfSummaries))
for i, df := range dfSummaries {
data[i] = *jsonConvert(df)
}
bytes, err := json.MarshalIndent(data, "", " ")
if err != nil {
return err

View File

@ -19,6 +19,23 @@ function teardown() {
is "$output" ".*Local Volumes *0 *0 " "No volumes"
}
@test "podman system df --format {{ json . }} functionality" {
run_podman system df --format '{{json .}}'
is "$output" '.*"TotalCount":1' "Exactly one image"
is "$output" '.*"RawSize".*"Size"' "RawSize and Size reported"
is "$output" '.*"RawReclaimable".*"Reclaimable"' "RawReclaimable and Reclaimable reported"
is "$output" '.*"Containers".*"Total":0' "Total containers reported"
is "$output" '.*"Local Volumes".*"Size":"0B"' "Total containers reported"
is "$output" '.*"Local Volumes".*"Size":"0B"' "Total containers reported"
}
@test "podman system df --format json functionality" {
run_podman system df --format json
is "$output" '.*"TotalCount": 1' "Exactly one image"
is "$output" '.*"RawSize": 0' "RawSize reported"
is "$output" '.*"Size": "0B"' "Size reported"
}
@test "podman system df - with active containers and volumes" {
run_podman run -v /myvol1 --name c1 $IMAGE true
run_podman run -d -v /myvol2 --name c2 $IMAGE \