Skip to content

Commit 8ea4006

Browse files
committed
Add --json-version option to choose between old and new JSON formats
Let's stay backwards-compatible. Switch the default JSON output format to the old one, but add an option that can be used to select the new version.
1 parent 2c88cd8 commit 8ea4006

File tree

2 files changed

+18
-2
lines changed

2 files changed

+18
-2
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ at the command line to view the contents of the object. (Use `--names=none` if y
173173

174174
By default, only statistics above a minimal level of concern are reported. Use `--verbose` (as above) to request that all statistics be output. Use `--threshold=<value>` to suppress the reporting of statistics below a specified level of concern. (`<value>` is interpreted as a numerical value corresponding to the number of asterisks.) Use `--critical` to report only statistics with a critical level of concern (equivalent to `--threshold=30`).
175175

176-
If you'd like the output in machine-readable format, including exact numbers, use the `--json` option.
176+
If you'd like the output in machine-readable format, including exact numbers, use the `--json` option. You can use `--json-version=1` or `--json-version=2` to choose between old and new style JSON output.
177177

178178
To get a list of other options, run
179179

git-sizer.go

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package main
22

33
import (
4+
"encoding/json"
45
"errors"
56
"fmt"
67
"io"
@@ -59,6 +60,7 @@ func mainImplementation() error {
5960
var nameStyle sizes.NameStyle = sizes.NameStyleFull
6061
var cpuprofile string
6162
var jsonOutput bool
63+
var jsonVersion uint
6264
var threshold sizes.Threshold = 1
6365
var progress bool
6466
var version bool
@@ -94,6 +96,7 @@ func mainImplementation() error {
9496
)
9597

9698
pflag.BoolVarP(&jsonOutput, "json", "j", false, "output results in JSON format")
99+
pflag.UintVar(&jsonVersion, "json-version", 1, "JSON format version to output (1 or 2)")
97100

98101
atty, err := isatty.Isatty(os.Stderr.Fd())
99102
if err != nil {
@@ -111,6 +114,10 @@ func mainImplementation() error {
111114

112115
pflag.Parse()
113116

117+
if jsonOutput && !(jsonVersion == 1 || jsonVersion == 2) {
118+
return fmt.Errorf("JSON version must be 1 or 2")
119+
}
120+
114121
if cpuprofile != "" {
115122
f, err := os.Create(cpuprofile)
116123
if err != nil {
@@ -166,7 +173,16 @@ func mainImplementation() error {
166173
}
167174

168175
if jsonOutput {
169-
j, err := historySize.JSON(threshold, nameStyle)
176+
var j []byte
177+
var err error
178+
switch jsonVersion {
179+
case 1:
180+
j, err = json.MarshalIndent(historySize, "", " ")
181+
case 2:
182+
j, err = historySize.JSON(threshold, nameStyle)
183+
default:
184+
return fmt.Errorf("JSON version must be 1 or 2")
185+
}
170186
if err != nil {
171187
return fmt.Errorf("could not convert %v to json: %s", historySize, err)
172188
}

0 commit comments

Comments
 (0)