Skip to content

Commit

Permalink
fix: migrate-resource tool (#36)
Browse files Browse the repository at this point in the history
* fix: migrate-resource tool

* improve format of output
  • Loading branch information
ekristen committed Jan 30, 2024
1 parent e7ec8d3 commit dd65769
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 19 deletions.
41 changes: 23 additions & 18 deletions tools/compare-resources/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"fmt"
"github.com/fatih/color"
"io/fs"
"os"
"path/filepath"
Expand All @@ -26,12 +27,13 @@ func main() {
panic("no arguments given")
}

awsNukeDirectory := filepath.Join(args[0], "resources")
upstreamDirectory := filepath.Join(args[0], "resources")

var awsNukeResourceFiles []string
var awsNukeResourceTypes []string
var upstreamResourceFiles []string
var upstreamResourceTypes []string
var upstreamTypeToFile = map[string]string{}

err := filepath.WalkDir(awsNukeDirectory, func(path string, di fs.DirEntry, err error) error {
err := filepath.WalkDir(upstreamDirectory, func(path string, di fs.DirEntry, err error) error {
if !strings.HasSuffix(path, ".go") {
return nil
}
Expand All @@ -40,15 +42,15 @@ func main() {
return nil
}

awsNukeResourceFiles = append(awsNukeResourceFiles, filepath.Base(path))
upstreamResourceFiles = append(upstreamResourceFiles, filepath.Base(path))
return nil
})
if err != nil {
panic(err)
}

for _, file := range awsNukeResourceFiles {
originalFileContents, err := os.ReadFile(filepath.Join(awsNukeDirectory, file))
for _, file := range upstreamResourceFiles {
originalFileContents, err := os.ReadFile(filepath.Join(upstreamDirectory, file))
if err != nil {
panic(err)
}
Expand All @@ -63,7 +65,8 @@ func main() {
funcName := matches[2]
_ = funcName

awsNukeResourceTypes = append(awsNukeResourceTypes, resourceType)
upstreamResourceTypes = append(upstreamResourceTypes, resourceType)
upstreamTypeToFile[resourceType] = file
}

var localResourcesPath = filepath.Join("resources")
Expand Down Expand Up @@ -105,17 +108,9 @@ func main() {
localResourceTypes = append(localResourceTypes, resourceType)
}

fmt.Println("\naws-nuke resource count:", len(awsNukeResourceTypes))
fmt.Println("\naws-nuke resource count:", len(upstreamResourceTypes))
fmt.Println("local resource count:", len(localResourceTypes))

fmt.Println("\nResources not in local aws-nuke:")
for _, resource := range awsNukeResourceTypes {
_, ok := aliases[resource]
if !slices.Contains(localResourceTypes, resource) && !ok {
fmt.Println("->", resource)
}
}

fmt.Println("\nResources not in aws-nuke:")
for _, resource := range localResourceTypes {
found := false
Expand All @@ -125,8 +120,18 @@ func main() {
}
}

if !slices.Contains(awsNukeResourceTypes, resource) && !found {
if !slices.Contains(upstreamResourceTypes, resource) && !found {
fmt.Println("+>", resource)
}
}

fmt.Println("\nResources not in local aws-nuke:")
for _, resource := range upstreamResourceTypes {
_, ok := aliases[resource]
if !slices.Contains(localResourceTypes, resource) && !ok {
color.New(color.Bold).Printf("%-55s", resource)
color.New(color.FgYellow).Println(upstreamTypeToFile[resource])
}
}

}
2 changes: 1 addition & 1 deletion tools/migrate-resource/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ func main() {
match := regexp.MustCompile("register\\(\"(?P<resource>.*)\",\\s?(?P<function>\\w+)(,)?(\\s+mapCloudControl\\(\"(?P<cc>.*)\"\\))?")
funcMatch := regexp.MustCompile("func List.*{")

filename := filepath.Join(originalSourceDir, "resources", args[1]+".go")
filename := filepath.Join(originalSourceDir, args[1]+".go")

originalFileContents, err := os.ReadFile(filename)
if err != nil {
Expand Down

0 comments on commit dd65769

Please sign in to comment.