-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathchanges.go
48 lines (40 loc) · 1021 Bytes
/
changes.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
package main
import (
tfmt "github.com/hashicorp/terraform/command/format"
"github.com/hashicorp/terraform/terraform"
)
type ResourceType string
type ResourceChanges struct {
Created []tfmt.InstanceDiff
Destroyed []tfmt.InstanceDiff
}
func (c *ResourceChanges) Add(diff tfmt.InstanceDiff) {
switch diff.Action {
case terraform.DiffCreate:
c.Created = append(c.Created, diff)
case terraform.DiffDestroy:
c.Destroyed = append(c.Destroyed, diff)
}
}
type ChangesByType map[ResourceType]*ResourceChanges
func (ct ChangesByType) Add(diff tfmt.InstanceDiff) {
rType := ResourceType(diff.Addr.Type)
if ct[rType] == nil {
ct[rType] = &ResourceChanges{}
}
changes := ct[rType]
changes.Add(diff)
}
func (ct ChangesByType) Get(rType ResourceType) *ResourceChanges {
return ct[rType]
}
func (ct ChangesByType) GetTypes() []ResourceType {
// https://stackoverflow.com/a/27848197/358804
types := make([]ResourceType, len(ct))
i := 0
for rType := range ct {
types[i] = rType
i++
}
return types
}