Skip to content

Commit

Permalink
Merge pull request #5 from FireDrunk/feature/json-support
Browse files Browse the repository at this point in the history
JSON (Metrics) Support
  • Loading branch information
containerscrew authored Mar 28, 2024
2 parents 6e45a40 + f87bf92 commit ab409ee
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 12 deletions.
16 changes: 15 additions & 1 deletion cmd/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,17 @@ var (
showUnchanged bool
compact bool
useMarkdown bool
useJson bool
metrics bool
)

func init() {
summarizeCmd.Flags().BoolVarP(&showTags, "show-tags", "t", false, "Show resources with tag changes")
summarizeCmd.Flags().BoolVarP(&showUnchanged, "show-unchanged", "u", false, "Show resources with no changes")
summarizeCmd.Flags().BoolVarP(&compact, "compact", "c", false, "Use compact formatting")
summarizeCmd.Flags().BoolVarP(&useMarkdown, "markdown", "m", false, "Use markdown formatting")
summarizeCmd.Flags().BoolVarP(&useJson, "json", "j", false, "Use JSON output")
summarizeCmd.Flags().BoolVarP(&metrics, "metrics", "s", false, "Output metrics")
}

// summarizeCmd will parse the tf plan output json to scrape created|updated|deleted resources in a clear outout
Expand All @@ -36,12 +40,22 @@ var summarizeCmd = &cobra.Command{
Short: "Get a summary of terraform/terragrunt output",
Long: "Get a summary of terraform/terragrunt output plan (created|updated|destroyed...)",
Run: func(cmd *cobra.Command, args []string) {
if useMarkdown && useJson {
fmt.Println("-m (Markdown output) and -j (JSON output) are mutually exclusive")
os.Exit(1)
}

if metrics && !useJson {
fmt.Println("Metric output can only be used with JSON output")
os.Exit(1)
}

output, err := reader.Reader(os.Stdin)
if err != nil {
panic(err)
}

parser.Parser(output, showTags, showUnchanged, compact, useMarkdown)
parser.Parser(output, showTags, showUnchanged, compact, useMarkdown, useJson, metrics)
},
}

Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module github.com/containerscrew/tftools

go 1.21.5
go 1.21

require (
github.com/charmbracelet/glamour v0.6.0
Expand Down
70 changes: 60 additions & 10 deletions internal/parser/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ var (
resourcesList = make(map[string][]string)
)

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

PrintPlanSummary(showTags, showUnchanged, compact, useMarkdown)
PrintPlanSummary(showTags, showUnchanged, compact, useMarkdown, useJson, metrics)
}

func processResourceChange(resourceChange *tfjson.ResourceChange, showTags bool) {
Expand Down Expand Up @@ -140,16 +140,66 @@ func PrintResources(message string, resources []string, bulletSymbol string, col
}
}

func PrintPlanSummary(showTags, showUnchanged, compact, useMarkdown bool) {
if showUnchanged {
PrintResources("πŸ”΅ Unchanged:", resourcesList[NOOP], "β€’", color.New(color.FgBlue), compact, useMarkdown)
func PrintPlanSummary(showTags, showUnchanged, compact, useMarkdown bool, useJson bool, metrics bool) {
if !useJson {
if showUnchanged {
PrintResources("πŸ”΅ Unchanged:", resourcesList[NOOP], "β€’", color.New(color.FgBlue), compact, useMarkdown)
}
if showTags {
PrintResources("🟣 Tag/Untag:", resourcesList[TAG], "#", color.New(color.FgMagenta), compact, useMarkdown)
}
PrintResources("🟒 Create:", resourcesList[CREATE], "+", color.New(color.FgGreen), compact, useMarkdown)
PrintResources("🟑 Update:", resourcesList[UPDATE], "~", color.New(color.FgYellow), compact, useMarkdown)
PrintResources("πŸ”΄ Destroy:", resourcesList[DELETE], "-", color.New(color.FgRed), compact, useMarkdown)
} else {
PrintResourcesJson(showTags, showUnchanged, metrics)
}
if showTags {
PrintResources("🟣 Tag/Untag:", resourcesList[TAG], "#", color.New(color.FgMagenta), compact, useMarkdown)
}

func PrintResourcesJson(showTags bool, showUnchanged bool, metrics bool) {
if metrics {
var metricsData = make(map[string]int)

if showUnchanged {
metricsData["unchanged"] = len(resourcesList[NOOP])
}

if showTags {
metricsData["tag"] = len(resourcesList[TAG])
}

metricsData["create"] = len(resourcesList[CREATE])
metricsData["update"] = len(resourcesList[UPDATE])
metricsData["delete"] = len(resourcesList[DELETE])

result, _ := json.Marshal(metricsData)
fmt.Println(string(result))
} else {
var data = make(map[string][]string)

if showUnchanged && len(resourcesList[NOOP]) > 0 {
data["unchanged"] = resourcesList[NOOP]
}

if showTags && len(resourcesList[TAG]) > 0 {
data["tag"] = resourcesList[TAG]
}

if len(resourcesList[CREATE]) > 0 {
data["create"] = resourcesList[CREATE]
}

if len(resourcesList[UPDATE]) > 0 {
data["update"] = resourcesList[UPDATE]
}

if len(resourcesList[DELETE]) > 0 {
data["delete"] = resourcesList[DELETE]
}

result, _ := json.Marshal(data)
fmt.Println(string(result))
}
PrintResources("🟒 Create:", resourcesList[CREATE], "+", color.New(color.FgGreen), compact, useMarkdown)
PrintResources("🟑 Update:", resourcesList[UPDATE], "~", color.New(color.FgYellow), compact, useMarkdown)
PrintResources("πŸ”΄ Destroy:", resourcesList[DELETE], "-", color.New(color.FgRed), compact, useMarkdown)
}

func checkOnlyTagChanges(resourceChange *tfjson.ResourceChange) (bool, error) {
Expand Down

0 comments on commit ab409ee

Please sign in to comment.