Skip to content

Commit 6d7943e

Browse files
Add pretty JSON output
1 parent 51cf038 commit 6d7943e

2 files changed

Lines changed: 23 additions & 10 deletions

File tree

cmd/commands.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ var (
2323
useMarkdown bool
2424
useJson bool
2525
metrics bool
26+
prettyJSON bool
2627
)
2728

2829
func init() {
@@ -32,6 +33,7 @@ func init() {
3233
summarizeCmd.Flags().BoolVarP(&useMarkdown, "markdown", "m", false, "Use markdown formatting")
3334
summarizeCmd.Flags().BoolVarP(&useJson, "json", "j", false, "Use JSON output")
3435
summarizeCmd.Flags().BoolVarP(&metrics, "metrics", "s", false, "Output metrics")
36+
summarizeCmd.Flags().BoolVarP(&prettyJSON, "pretty-json", "p", false, "Pretty JSON output")
3537
}
3638

3739
// summarizeCmd will parse the tf plan output json to scrape created|updated|deleted resources in a clear outout
@@ -55,7 +57,7 @@ var summarizeCmd = &cobra.Command{
5557
panic(err)
5658
}
5759

58-
parser.Parser(output, showTags, showUnchanged, compact, useMarkdown, useJson, metrics)
60+
parser.Parser(output, showTags, showUnchanged, compact, useMarkdown, useJson, metrics, prettyJSON)
5961
},
6062
}
6163

internal/parser/parser.go

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ var (
2020
resourcesList = make(map[string][]string)
2121
)
2222

23-
func Parser(output []byte, showTags, showUnchanged, compact, useMarkdown bool, useJson bool, metrics bool) {
23+
func Parser(output []byte, showTags, showUnchanged, compact, useMarkdown bool, useJson bool, metrics bool, prettyJSON bool) {
2424
var data tfjson.Plan
2525
if err := json.Unmarshal(output, &data); err != nil {
2626
fmt.Printf("Error unmarshalling plan: %v\n", err)
@@ -31,7 +31,7 @@ func Parser(output []byte, showTags, showUnchanged, compact, useMarkdown bool, u
3131
processResourceChange(resourceChange, showTags)
3232
}
3333

34-
PrintPlanSummary(showTags, showUnchanged, compact, useMarkdown, useJson, metrics)
34+
PrintPlanSummary(showTags, showUnchanged, compact, useMarkdown, useJson, metrics, prettyJSON)
3535
}
3636

3737
func processResourceChange(resourceChange *tfjson.ResourceChange, showTags bool) {
@@ -140,7 +140,7 @@ func PrintResources(message string, resources []string, bulletSymbol string, col
140140
}
141141
}
142142

143-
func PrintPlanSummary(showTags, showUnchanged, compact, useMarkdown bool, useJson bool, metrics bool) {
143+
func PrintPlanSummary(showTags, showUnchanged, compact, useMarkdown bool, useJson bool, metrics bool, prettyJSON bool) {
144144
if !useJson {
145145
if showUnchanged {
146146
PrintResources("🔵 Unchanged:", resourcesList[NOOP], "•", color.New(color.FgBlue), compact, useMarkdown)
@@ -152,11 +152,11 @@ func PrintPlanSummary(showTags, showUnchanged, compact, useMarkdown bool, useJso
152152
PrintResources("🟡 Update:", resourcesList[UPDATE], "~", color.New(color.FgYellow), compact, useMarkdown)
153153
PrintResources("🔴 Destroy:", resourcesList[DELETE], "-", color.New(color.FgRed), compact, useMarkdown)
154154
} else {
155-
PrintResourcesJson(showTags, showUnchanged, metrics)
155+
PrintResourcesJson(showTags, showUnchanged, metrics, prettyJSON)
156156
}
157157
}
158158

159-
func PrintResourcesJson(showTags bool, showUnchanged bool, metrics bool) {
159+
func PrintResourcesJson(showTags bool, showUnchanged bool, metrics bool, prettyJSON bool) {
160160
if metrics {
161161
var metricsData = make(map[string]int)
162162

@@ -172,8 +172,14 @@ func PrintResourcesJson(showTags bool, showUnchanged bool, metrics bool) {
172172
metricsData["update"] = len(resourcesList[UPDATE])
173173
metricsData["delete"] = len(resourcesList[DELETE])
174174

175-
result, _ := json.Marshal(metricsData)
176-
fmt.Println(string(result))
175+
if prettyJSON {
176+
result, _ := json.MarshalIndent(metricsData, "", " ")
177+
fmt.Println(string(result))
178+
} else {
179+
result, _ := json.Marshal(metricsData)
180+
fmt.Println(string(result))
181+
}
182+
177183
} else {
178184
var data = make(map[string][]string)
179185

@@ -197,8 +203,13 @@ func PrintResourcesJson(showTags bool, showUnchanged bool, metrics bool) {
197203
data["delete"] = resourcesList[DELETE]
198204
}
199205

200-
result, _ := json.Marshal(data)
201-
fmt.Println(string(result))
206+
if prettyJSON {
207+
result, _ := json.MarshalIndent(data, "", " ") //json.Marshal(data)
208+
fmt.Println(string(result))
209+
} else {
210+
result, _ := json.Marshal(data)
211+
fmt.Println(string(result))
212+
}
202213
}
203214
}
204215

0 commit comments

Comments
 (0)