Skip to content

Commit 7afddd0

Browse files
committed
added pretty print
1 parent 6e9f1ec commit 7afddd0

File tree

1 file changed

+17
-4
lines changed

1 file changed

+17
-4
lines changed

cmds/jsoncols/jsoncols.go

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -85,9 +85,15 @@ Would yield
8585
delimiter = ","
8686
expressions []string
8787
quote bool
88+
pretty bool
8889
)
8990

9091
func main() {
92+
var (
93+
src []byte
94+
err error
95+
)
96+
9197
app := cli.NewCli(datatools.Version)
9298
appName := app.AppName()
9399

@@ -116,14 +122,13 @@ func main() {
116122
app.BoolVar(&csvOutput, "csv", false, "output as CSV or other flat delimiter row")
117123
app.StringVar(&delimiter, "d,delimiter", delimiter, "set the delimiter for multi-field csv output")
118124
app.BoolVar(&quote, "quote", true, "quote strings and JSON notation")
125+
app.BoolVar(&pretty, "p,pretty", false, "pretty print JSON output")
119126

120127
// Parse Environment and Options
121128
app.Parse()
122129
args := app.Args()
123130

124131
// Setup IO
125-
var err error
126-
127132
app.Eout = os.Stderr
128133
app.In, err = cli.Open(inputFName, os.Stdin)
129134
cli.ExitOnError(app.Eout, err, quiet)
@@ -187,7 +192,11 @@ func main() {
187192
case json.Number:
188193
row = append(row, result.(json.Number).String())
189194
default:
190-
src, err := json.Marshal(result)
195+
if pretty {
196+
src, err = json.MarshalIndent(result, "", " ")
197+
} else {
198+
src, err = json.Marshal(result)
199+
}
191200
cli.ExitOnError(app.Eout, err, quiet)
192201
row = append(row, fmt.Sprintf("%s", src))
193202
}
@@ -227,7 +236,11 @@ func main() {
227236
case json.Number:
228237
fmt.Fprintf(app.Out, "%s", result.(json.Number).String())
229238
default:
230-
src, err := json.Marshal(result)
239+
if pretty {
240+
src, err = json.MarshalIndent(result, "", " ")
241+
} else {
242+
src, err = json.Marshal(result)
243+
}
231244
cli.ExitOnError(app.Eout, err, quiet)
232245
/*
233246
if quote == true {

0 commit comments

Comments
 (0)