Skip to content

Commit

Permalink
print change summary as json (#22)
Browse files Browse the repository at this point in the history
* print change summary as json

* update test
cgroschupp authored Oct 19, 2024

Verified

This commit was signed with the committer’s verified signature.
1 parent 3483af4 commit 4e3f1ce
Showing 4 changed files with 39 additions and 7 deletions.
11 changes: 6 additions & 5 deletions main.go
Original file line number Diff line number Diff line change
@@ -19,6 +19,7 @@ func main() {
tree := flag.Bool("tree", false, "[Optional] print changes in tree format")
json := flag.Bool("json", false, "[Optional] print changes in json format")
html := flag.Bool("html", false, "[Optional] print changes in html format")
jsonSum := flag.Bool("json-sum", false, "[Optional] print summary in json format")
separateTree := flag.Bool("separate-tree", false, "[Optional] print changes in tree format for add/delete/change/recreate changes")
drawable := flag.Bool("draw", false, "[Optional, used only with -tree or -separate-tree] draw trees instead of plain tree")
md := flag.Bool("md", false, "[Optional, used only with table view] output table as markdown")
@@ -36,7 +37,7 @@ func main() {
}

args := flag.Args()
err := validateFlags(*tree, *separateTree, *drawable, *md, *json, *html, args)
err := validateFlags(*tree, *separateTree, *drawable, *md, *json, *jsonSum, *html, args)
logIfErrorAndExit("invalid input flags: %s\n", err, flag.Usage)

newReader, err := reader.CreateReader(args)
@@ -53,7 +54,7 @@ func main() {

terraformstate.FilterNoOpResources(&terraformState)

newWriter := writer.CreateWriter(*tree, *separateTree, *drawable, *md, *json, *html, terraformState)
newWriter := writer.CreateWriter(*tree, *separateTree, *drawable, *md, *json, *html, *jsonSum, terraformState)

var outputFile io.Writer = os.Stdout

@@ -84,7 +85,7 @@ func logIfErrorAndExit(format string, err error, callback func()) {
}
}

func validateFlags(tree, separateTree, drawable bool, md bool, json bool, html bool, args []string) error {
func validateFlags(tree, separateTree, drawable bool, md bool, json bool, jsonSum bool, html bool, args []string) error {
if tree && md {
return fmt.Errorf("both -tree and -md should not be provided")
}
@@ -97,8 +98,8 @@ func validateFlags(tree, separateTree, drawable bool, md bool, json bool, html b
if !tree && !separateTree && drawable {
return fmt.Errorf("drawable should be provided with -tree or -seperate-tree")
}
if multipleTrueVals(md, json, html) {
return fmt.Errorf("only one of -md, -json, or -html should be provided")
if multipleTrueVals(md, json, html, jsonSum) {
return fmt.Errorf("only one of -md, -json, -json-sum, or -html should be provided")
}
if len(args) > 1 {
return fmt.Errorf("only one argument is allowed which is filename, but got %v", args)
4 changes: 3 additions & 1 deletion testdata/multiple_format_flags_error.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
invalid input flags: only one of -md, -json, or -html should be provided
invalid input flags: only one of -md, -json, -json-sum, or -html should be provided

Usage of ./tf-summarize-test [args] [tf-plan.json|tfplan]

@@ -8,6 +8,8 @@ Usage of ./tf-summarize-test [args] [tf-plan.json|tfplan]
[Optional] print changes in html format
-json
[Optional] print changes in json format
-json-sum
[Optional] print summary in json format
-md
[Optional, used only with table view] output table as markdown
-out string
26 changes: 26 additions & 0 deletions writer/json-sum.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package writer

import (
"fmt"
"io"

"github.com/dineshba/tf-summarize/terraformstate"
)

type JsonSumWriter struct {
changes map[string]terraformstate.ResourceChanges
}

func (t JsonSumWriter) Write(writer io.Writer) error {
result := make(map[string]int, len(t.changes))
for k, v := range t.changes {
result[k] = len(v)
}
s, _ := Marshal(map[string]map[string]int{"changes": result})
_, err := fmt.Fprint(writer, string(s))
return err
}

func NewJsonSumWriter(changes map[string]terraformstate.ResourceChanges) Writer {
return JsonSumWriter{changes: changes}
}
5 changes: 4 additions & 1 deletion writer/writer.go
Original file line number Diff line number Diff line change
@@ -11,7 +11,7 @@ type Writer interface {
Write(writer io.Writer) error
}

func CreateWriter(tree, separateTree, drawable, mdEnabled, json, html bool, plan tfjson.Plan) Writer {
func CreateWriter(tree, separateTree, drawable, mdEnabled, json, html bool, jsonSum bool, plan tfjson.Plan) Writer {
if tree {
return NewTreeWriter(plan.ResourceChanges, drawable)
}
@@ -24,6 +24,9 @@ func CreateWriter(tree, separateTree, drawable, mdEnabled, json, html bool, plan
if html {
return NewHTMLWriter(terraformstate.GetAllResourceChanges(plan), terraformstate.GetAllOutputChanges(plan))
}
if jsonSum {
return NewJsonSumWriter(terraformstate.GetAllResourceChanges(plan))
}

return NewTableWriter(terraformstate.GetAllResourceChanges(plan), terraformstate.GetAllOutputChanges(plan), mdEnabled)
}

0 comments on commit 4e3f1ce

Please sign in to comment.