Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ go 1.25.2

require (
github.com/hashicorp/hcl/v2 v2.24.0
github.com/infracost/proto v1.141.0
github.com/infracost/proto v1.146.0
github.com/stretchr/testify v1.11.1
github.com/zclconf/go-cty v1.17.0
)
Expand Down
8 changes: 2 additions & 6 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,8 @@ github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/hashicorp/hcl/v2 v2.24.0 h1:2QJdZ454DSsYGoaE6QheQZjtKZSUs9Nh2izTWiwQxvE=
github.com/hashicorp/hcl/v2 v2.24.0/go.mod h1:oGoO1FIQYfn/AgyOhlg9qLC6/nOJPX3qGbkZpYAcqfM=
github.com/infracost/proto v1.34.0 h1:zkp3C8YCozE6hZ8bP00dk4jQE2c0/QZDZjkII08k9/g=
github.com/infracost/proto v1.34.0/go.mod h1:Z8vPWBWblwJlw+/ksO+BtsXwf9NiOcSTWx0WRWNbfUA=
github.com/infracost/proto v1.138.0 h1:WDwRHGkhH8nSe0Ctbe/R2E8PB5aVcYdmsc229jaOsxk=
github.com/infracost/proto v1.138.0/go.mod h1:Z8vPWBWblwJlw+/ksO+BtsXwf9NiOcSTWx0WRWNbfUA=
github.com/infracost/proto v1.141.0 h1:Rge7RDThS3cEOzWMLo9d86C3BzMXrVULTO5yLeF2LrQ=
github.com/infracost/proto v1.141.0/go.mod h1:Z8vPWBWblwJlw+/ksO+BtsXwf9NiOcSTWx0WRWNbfUA=
github.com/infracost/proto v1.146.0 h1:1oQ0FbIRsLlMUY6cLYQFa4khWHYIluJeJbM2Wj280Qs=
github.com/infracost/proto v1.146.0/go.mod h1:Z8vPWBWblwJlw+/ksO+BtsXwf9NiOcSTWx0WRWNbfUA=
github.com/mitchellh/go-wordwrap v1.0.1 h1:TLuKupo69TCn6TQSyGxwI1EblZZEsQ0vMlAFQflz0v0=
github.com/mitchellh/go-wordwrap v1.0.1/go.mod h1:R62XHJLzvMFRBbcrT7m7WgmE1eOyTSsCt+hzestvNj0=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
Expand Down
18 changes: 10 additions & 8 deletions pkg/tree/tree.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,19 @@ type Resource interface {

type Tree struct {
AWS aws.AWS `tree:"aws"`
Azure azure.Azure `tree:"azure"`
Google google.Google `tree:"google"`
UnsupportedResources []*resource.Resource `tree:"-"` // these get handled as a special case
Azure azure.Azure `tree:"azure"`
Google google.Google `tree:"google"`
UnsupportedResources []*resource.Resource `tree:"-"` // these get handled as a special case
}

// ToResources returns every struct in the tree that embeds resource.Resource.
func (t *Tree) ToResources() []Resource {
func (t *Tree) ToResources(includeUnsupported bool) []Resource {
var output []Resource
collectResources(reflect.ValueOf(t).Elem(), &output)
for _, res := range t.UnsupportedResources {
output = append(output, res)
if includeUnsupported {
for _, res := range t.UnsupportedResources {
output = append(output, res)
}
}
return output
}
Expand Down Expand Up @@ -62,13 +64,13 @@ func collectResources(v reflect.Value, out *[]Resource) {
}
}

// ModifyResource finds the equivlanet resource of _target_ in a cloned tree and modifies it with the supplied function
// ModifyResource finds the equivalent resource of _target_ in a cloned tree and modifies it with the supplied function
// Note that _target_ MUST be a pointer for this to compile
func ModifyResource[T interface {
Resource
~*E
}, E any](t *Tree, target T, modify func(t T)) error {
for _, resource := range t.ToResources() {
for _, resource := range t.ToResources(true) {
if match, ok := resource.(T); ok && match.GetBase().Definition.Address.Equal(target.GetBase().Definition.Address) {
modify(match)
return nil
Expand Down
12 changes: 8 additions & 4 deletions pkg/tree/tree_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ func TestToResources(t *testing.T) {
},
}

resources := tree.ToResources()
resources := tree.ToResources(false)
require.Len(t, resources, 2)

inst0, ok := resources[0].(*ec2.Instance)
Expand All @@ -41,7 +41,7 @@ func TestToResources(t *testing.T) {

func TestToResourcesEmpty(t *testing.T) {
tree := &Tree{}
resources := tree.ToResources()
resources := tree.ToResources(false)
assert.Empty(t, resources)
}

Expand All @@ -53,7 +53,9 @@ func TestToResourcesUnsupported(t *testing.T) {
},
}

resources := tree.ToResources()
supportedResources := tree.ToResources(false)
require.Len(t, supportedResources, 0)
resources := tree.ToResources(true)
require.Len(t, resources, 2)

assert.Equal(t, "unsupported-1", resources[0].GetBase().ID)
Expand Down Expand Up @@ -103,7 +105,9 @@ func TestToResourcesMixed(t *testing.T) {
},
}

resources := tree.ToResources()
supportedResources := tree.ToResources(false)
require.Len(t, supportedResources, 1)
resources := tree.ToResources(true)
require.Len(t, resources, 2)

assert.Equal(t, "i-111", resources[0].GetBase().ID)
Expand Down
Loading