Skip to content

Commit

Permalink
Merge pull request #901 from cloudskiff/group_by_state_console
Browse files Browse the repository at this point in the history
Group changes by IaC source in console output
  • Loading branch information
eliecharra committed Aug 2, 2021
2 parents dcc67c8 + 85870c8 commit 62e0efe
Show file tree
Hide file tree
Showing 8 changed files with 241 additions and 176 deletions.
66 changes: 41 additions & 25 deletions pkg/cmd/scan/output/console.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,35 +65,51 @@ func (c *Console) Write(analysis *analyser.Analysis) error {
}

if analysis.Summary().TotalDrifted > 0 {
fmt.Println("Found changed resources:")
for _, difference := range analysis.Differences() {
humanString := fmt.Sprintf(" - %s (%s):", difference.Res.TerraformId(), difference.Res.TerraformType())
whiteSpace := " "
if humanAttrs := formatResourceAttributes(difference.Res); humanAttrs != "" {
humanString += fmt.Sprintf("\n %s", humanAttrs)
whiteSpace = " "

groupedBySource := make(map[string][]analyser.Difference)
for _, diff := range analysis.Differences() {
res := diff.Res.(*resource.AbstractResource)
key := res.Source.Source()
if _, exist := groupedBySource[key]; !exist {
groupedBySource[key] = []analyser.Difference{diff}
continue
}
fmt.Println(humanString)
for _, change := range difference.Changelog {
path := strings.Join(change.Path, ".")
pref := fmt.Sprintf("%s %s:", color.YellowString("~"), path)
if change.Type == diff.CREATE {
pref = fmt.Sprintf("%s %s:", color.GreenString("+"), path)
} else if change.Type == diff.DELETE {
pref = fmt.Sprintf("%s %s:", color.RedString("-"), path)
groupedBySource[key] = append(groupedBySource[key], diff)
}

fmt.Println("Found changed resources:")
for source, differences := range groupedBySource {
fmt.Print(color.BlueString(" From %s\n", source))
for _, difference := range differences {
res := difference.Res.(*resource.AbstractResource)
humanString := fmt.Sprintf(" - %s (%s):", res.TerraformId(), res.SourceString())
whiteSpace := " "
if humanAttrs := formatResourceAttributes(res); humanAttrs != "" {
humanString += fmt.Sprintf("\n %s", humanAttrs)
whiteSpace = " "
}
if change.Type == diff.UPDATE {
if change.JsonString {
prefix := " "
fmt.Printf("%s%s\n%s%s\n", whiteSpace, pref, prefix, jsonDiff(change.From, change.To, prefix))
continue
fmt.Println(humanString)
for _, change := range difference.Changelog {
path := strings.Join(change.Path, ".")
pref := fmt.Sprintf("%s %s:", color.YellowString("~"), path)
if change.Type == diff.CREATE {
pref = fmt.Sprintf("%s %s:", color.GreenString("+"), path)
} else if change.Type == diff.DELETE {
pref = fmt.Sprintf("%s %s:", color.RedString("-"), path)
}
if change.Type == diff.UPDATE {
if change.JsonString {
prefix := " "
fmt.Printf("%s%s\n%s%s\n", whiteSpace, pref, prefix, jsonDiff(change.From, change.To, prefix))
continue
}
}
fmt.Printf("%s%s %s => %s", whiteSpace, pref, prettify(change.From), prettify(change.To))
if change.Computed {
fmt.Printf(" %s", color.YellowString("(computed)"))
}
fmt.Printf("\n")
}
fmt.Printf("%s%s %s => %s", whiteSpace, pref, prettify(change.From), prettify(change.To))
if change.Computed {
fmt.Printf(" %s", color.YellowString("(computed)"))
}
fmt.Printf("\n")
}
}
}
Expand Down
100 changes: 53 additions & 47 deletions pkg/cmd/scan/output/html_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,59 +93,65 @@ func TestHTML_Write(t *testing.T) {
Type: "aws_unmanaged_resource",
},
)
a.AddDifference(analyser.Difference{Res: &testresource.FakeResource{
Id: "diff-id-2",
Type: "aws_diff_resource",
}, Changelog: []analyser.Change{
{
Change: diff.Change{
Type: diff.DELETE,
Path: []string{"path", "to", "fields", "0"},
From: "value",
To: nil,
a.AddDifference(analyser.Difference{
Res: &resource.AbstractResource{
Id: "diff-id-2",
Type: "aws_diff_resource",
Source: &resource.TerraformStateSource{
State: "tfstate://state.tfstate",
Module: "module",
Name: "name",
},
},
{
Change: diff.Change{
Type: diff.UPDATE,
Path: []string{"path", "to", "fields", "1"},
From: 12,
To: "12",
}, Changelog: []analyser.Change{
{
Change: diff.Change{
Type: diff.DELETE,
Path: []string{"path", "to", "fields", "0"},
From: "value",
To: nil,
},
},
},
{
Change: diff.Change{
Type: diff.DELETE,
Path: []string{"group_ids"},
From: []string{"a071314398026"},
To: nil,
{
Change: diff.Change{
Type: diff.UPDATE,
Path: []string{"path", "to", "fields", "1"},
From: 12,
To: "12",
},
},
},
{
Change: diff.Change{
Type: diff.UPDATE,
Path: []string{"Policies", "0"},
From: testresource.FakeResource{},
To: testresource.FakeResource{Id: "093cd6ba-cf6d-4800-b252-6a50ca8903cd", Type: "aws_iam_policy"},
{
Change: diff.Change{
Type: diff.DELETE,
Path: []string{"group_ids"},
From: []string{"a071314398026"},
To: nil,
},
},
},
{
Change: diff.Change{
Type: diff.CREATE,
Path: []string{"Tags", "0", "Name"},
From: nil,
To: "test",
{
Change: diff.Change{
Type: diff.UPDATE,
Path: []string{"Policies", "0"},
From: testresource.FakeResource{},
To: testresource.FakeResource{Id: "093cd6ba-cf6d-4800-b252-6a50ca8903cd", Type: "aws_iam_policy"},
},
},
},
{
Change: diff.Change{
Type: diff.UPDATE,
Path: []string{"InstanceInitiatedShutdownBehavior"},
From: "",
To: nil,
{
Change: diff.Change{
Type: diff.CREATE,
Path: []string{"Tags", "0", "Name"},
From: nil,
To: "test",
},
},
},
}})
{
Change: diff.Change{
Type: diff.UPDATE,
Path: []string{"InstanceInitiatedShutdownBehavior"},
From: "",
To: nil,
},
},
}})
a.ProviderName = "AWS"
a.ProviderVersion = "3.19.0"
return a
Expand Down

0 comments on commit 62e0efe

Please sign in to comment.