Skip to content

Commit

Permalink
Feature: differentiate between (+/-) and (-/+) in output (#59)
Browse files Browse the repository at this point in the history
* Feature: differentiate between (+/-) and (-/+) in output

* Apply linter suggestions

* Update go to v1.21

* Update gh actions to go v1.21

* Fix build

* Whoops
  • Loading branch information
QuintenBruynseraede authored Dec 22, 2023
1 parent c4f1426 commit 5c19269
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 13 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ jobs:
- name: Set up Go
uses: actions/setup-go@v4
with:
go-version: '1.20'
go-version: '1.21'

- name: golangci-lint
uses: golangci/golangci-lint-action@v3
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ jobs:
name: Set up Go
uses: actions/setup-go@v4
with:
go-version: '1.20'
go-version: '1.21'

-
name: Import GPG key
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/dineshba/tf-summarize

go 1.20
go 1.21

require (
github.com/fatih/color v1.15.0
Expand Down
28 changes: 18 additions & 10 deletions terraformstate/terraform_state.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package terraformstate
import (
"encoding/json"
"fmt"
"slices"
)

const ColorReset = "\033[0m"
Expand All @@ -19,15 +20,19 @@ type ResourceChange struct {
Type string `json:"type"`
Name string `json:"name"`
ProviderName string `json:"provider_name"`
Change struct {
Actions []string `json:"actions"`
Before json.RawMessage `json:"before,omitempty"`
After json.RawMessage `json:"after,omitempty"`
Importing struct {
ID string `json:"id"`
} `json:"importing"`
} `json:"change"`
ActionReason string `json:"action_reason,omitempty"`
Change Change `json:"change"`
ActionReason string `json:"action_reason,omitempty"`
}

type Change struct {
Actions []string `json:"actions"`
Before json.RawMessage `json:"before,omitempty"`
After json.RawMessage `json:"after,omitempty"`
Importing Importing `json:"importing"`
}

type Importing struct {
ID string `json:"id"`
}

type OutputValues struct {
Expand All @@ -53,7 +58,10 @@ func (rc ResourceChange) ColorPrefixAndSuffixText() (string, string) {
} else if rc.Change.Importing.ID != "" {
colorPrefix = ColorCyan
suffix = "(i)"
} else {
} else if slices.Equal(actions, []string{"delete", "create"}) {
colorPrefix = ColorMagenta
suffix = "(-/+)"
} else if slices.Equal(actions, []string{"create", "delete"}) {
colorPrefix = ColorMagenta
suffix = "(+/-)"
}
Expand Down
59 changes: 59 additions & 0 deletions terraformstate/terraform_state_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package terraformstate

import (
"testing"

"github.com/stretchr/testify/assert"
)

func TestResourceChangeColor(t *testing.T) {
ExpectedColors := map[string]string{
"create": ColorGreen,
"delete": ColorRed,
"update": ColorYellow,
}

for action, expectedColor := range ExpectedColors {
create := ResourceChange{Change: Change{Actions: []string{action}}}
color, _ := create.ColorPrefixAndSuffixText()

assert.Equal(t, color, expectedColor)
}
create := ResourceChange{Change: Change{Actions: []string{"create", "delete"}}}
color, _ := create.ColorPrefixAndSuffixText()
assert.Equal(t, color, ColorMagenta)

create = ResourceChange{Change: Change{Actions: []string{"delete", "create"}}}
color, _ = create.ColorPrefixAndSuffixText()
assert.Equal(t, color, ColorMagenta)
}

func TestResourceChangeSuffix(t *testing.T) {
ExpectedSuffix := map[string]string{
"create": "(+)",
"delete": "(-)",
"update": "(~)",
}

for action, expectedSuffix := range ExpectedSuffix {
create := ResourceChange{Change: Change{Actions: []string{action}}}
_, suffix := create.ColorPrefixAndSuffixText()

assert.Equal(t, suffix, expectedSuffix)
}
create := ResourceChange{Change: Change{Actions: []string{"create", "delete"}}}
_, suffix := create.ColorPrefixAndSuffixText()
assert.Equal(t, suffix, "(+/-)")

create = ResourceChange{Change: Change{Actions: []string{"delete", "create"}}}
_, suffix = create.ColorPrefixAndSuffixText()
assert.Equal(t, suffix, "(-/+)")
}

func TestResourceChangeColorAndSuffixImport(t *testing.T) {
importing := ResourceChange{Change: Change{Importing: Importing{ID: "id"}}}
color, suffix := importing.ColorPrefixAndSuffixText()

assert.Equal(t, color, ColorCyan)
assert.Equal(t, suffix, "(i)")
}

0 comments on commit 5c19269

Please sign in to comment.