Skip to content

Commit

Permalink
Moved count related code.
Browse files Browse the repository at this point in the history
  • Loading branch information
mkyc committed Dec 2, 2020
1 parent c49312a commit 1d2a851
Show file tree
Hide file tree
Showing 2 changed files with 99 additions and 0 deletions.
25 changes: 25 additions & 0 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,11 @@ func main() {
panic(err)
}
println(s)
c, err := terra.Count(s)
if err != nil {
panic(err)
}
fmt.Printf("[Plan] Add: %d, Change: %d, Destroy: %d\n", c.Add, c.Change, c.Destroy)

println("================")
println("================")
Expand All @@ -100,6 +105,11 @@ func main() {
panic(err)
}
println(s)
c, err = terra.Count(s)
if err != nil {
panic(err)
}
fmt.Printf("[Apply] Added: %d, Changed: %d, Destroyed: %d\n", c.Add, c.Change, c.Destroy)

println("================")
println("================")
Expand All @@ -126,6 +136,11 @@ func main() {
if err != nil {
panic(err)
}
c, err = terra.Count(s)
if err != nil {
panic(err)
}
fmt.Printf("[Destroy] Added: %d, Changed: %d, Destroyed: %d\n", c.Add, c.Change, c.Destroy)

println("================")
println("=== plan =======")
Expand Down Expand Up @@ -163,6 +178,11 @@ func main() {
panic(err)
}
println(s)
c, err = terra.Count(s)
if err != nil {
panic(err)
}
fmt.Printf("[Plan Destroy] Add: %d, Change: %d, Destroy: %d\n", c.Add, c.Change, c.Destroy)

println("================")
println("=== show =======")
Expand All @@ -187,4 +207,9 @@ func main() {
panic(err)
}
println(s)
c, err = terra.Count(s)
if err != nil {
panic(err)
}
fmt.Printf("[Apply Destruction] Added: %d, Changed: %d, Destroyed: %d\n", c.Add, c.Change, c.Destroy)
}
74 changes: 74 additions & 0 deletions count.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package terra

import (
"errors"
"regexp"
"strconv"
)

// ResourceCount represents counts of resources affected by terraform apply/plan/destroy command.
type ResourceCount struct {
Add int
Change int
Destroy int
}

// Regular expressions for terraform commands stdout pattern matching.
const (
applyRegexp = `Apply complete! Resources: (\d+) added, (\d+) changed, (\d+) destroyed\.`
destroyRegexp = `Destroy complete! Resources: (\d+) destroyed\.`
planWithChangesRegexp = `(\033\[1m)?Plan:(\033\[0m)? (\d+) to add, (\d+) to change, (\d+) to destroy\.`
planWithNoChangesRegexp = `No changes\. Infrastructure is up-to-date\.`
)

// GetResourceCount parses stdout/stderr of apply/plan/destroy commands and returns number of affected resources.
func Count(cmdout string) (*ResourceCount, error) {
cnt := ResourceCount{}

terraformCommandPatterns := []struct {
regexpStr string
addPosition int
changePosition int
destroyPosition int
}{
{applyRegexp, 1, 2, 3},
{destroyRegexp, -1, -1, 1},
{planWithChangesRegexp, 3, 4, 5},
{planWithNoChangesRegexp, -1, -1, -1},
}

for _, tc := range terraformCommandPatterns {
pattern, err := regexp.Compile(tc.regexpStr)
if err != nil {
return nil, err
}

matches := pattern.FindStringSubmatch(cmdout)
if matches != nil {
if tc.addPosition != -1 {
cnt.Add, err = strconv.Atoi(matches[tc.addPosition])
if err != nil {
return nil, err
}
}

if tc.changePosition != -1 {
cnt.Change, err = strconv.Atoi(matches[tc.changePosition])
if err != nil {
return nil, err
}
}

if tc.destroyPosition != -1 {
cnt.Destroy, err = strconv.Atoi(matches[tc.destroyPosition])
if err != nil {
return nil, err
}
}

return &cnt, nil
}
}

return nil, errors.New("can't parse Terraform output")
}

0 comments on commit 1d2a851

Please sign in to comment.