Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: fix bugs #36

Merged
merged 4 commits into from
Dec 10, 2024
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
9 changes: 5 additions & 4 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,9 @@ require (
github.com/mitchellh/go-wordwrap v0.0.0-20150314170334-ad45545899c7 // indirect
github.com/russross/blackfriday/v2 v2.1.0 // indirect
github.com/xrash/smetrics v0.0.0-20240521201337-686a1a2994c1 // indirect
golang.org/x/mod v0.8.0 // indirect
golang.org/x/sys v0.15.0 // indirect
golang.org/x/text v0.14.0 // indirect
golang.org/x/tools v0.6.0 // indirect
golang.org/x/mod v0.17.0 // indirect
golang.org/x/sync v0.8.0 // indirect
golang.org/x/sys v0.20.0 // indirect
golang.org/x/text v0.19.0 // indirect
golang.org/x/tools v0.21.1-0.20240508182429-e35e4ccd0d2d // indirect
)
20 changes: 10 additions & 10 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -42,18 +42,18 @@ github.com/zclconf/go-cty v1.15.1 h1:RgQYm4j2EvoBRXOPxhUvxPzRrGDo1eCOhHXuGfrj5S0
github.com/zclconf/go-cty v1.15.1/go.mod h1:VvMs5i0vgZdhYawQNq5kePSpLAoz8u1xvZgrPIxfnZE=
github.com/zclconf/go-cty-debug v0.0.0-20240509010212-0d6042c53940 h1:4r45xpDWB6ZMSMNJFMOjqrGHynW3DIBuR2H9j0ug+Mo=
github.com/zclconf/go-cty-debug v0.0.0-20240509010212-0d6042c53940/go.mod h1:CmBdvvj3nqzfzJ6nTCIwDTPZ56aVGvDrmztiO5g3qrM=
golang.org/x/mod v0.8.0 h1:LUYupSeNrTNCGzR/hVBk2NHZO4hXcVaW1k4Qx7rjPx8=
golang.org/x/mod v0.8.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs=
golang.org/x/sync v0.5.0 h1:60k92dhOjHxJkrqnwsfl8KuaHbn/5dl0lUPUklKo3qE=
golang.org/x/sync v0.5.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
golang.org/x/mod v0.17.0 h1:zY54UmvipHiNd+pm+m0x9KhZ9hl1/7QNMyxXbc6ICqA=
golang.org/x/mod v0.17.0/go.mod h1:hTbmBsO62+eylJbnUtE2MGJUyE7QWk4xUqPFrRgJ+7c=
golang.org/x/sync v0.8.0 h1:3NFvSEYkUoMifnESzZl15y791HH1qU2xm6eCJU5ZPXQ=
golang.org/x/sync v0.8.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.15.0 h1:h48lPFYpsTvQJZF4EKyI4aLHaev3CxivZmv7yZig9pc=
golang.org/x/sys v0.15.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/text v0.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ=
golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU=
golang.org/x/tools v0.6.0 h1:BOw41kyTf3PuCW1pVQf8+Cyg8pMlkYB1oo9iJ6D/lKM=
golang.org/x/tools v0.6.0/go.mod h1:Xwgl3UAJ/d3gWutnCtw505GrjyAbvKui8lOU390QaIU=
golang.org/x/sys v0.20.0 h1:Od9JTbYCk261bKm4M/mw7AklTlFYIa0bIp9BgSm1S8Y=
golang.org/x/sys v0.20.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/text v0.19.0 h1:kTxAhCbGbxhK0IwgSKiMO5awPoDQ0RpfiVYBfK860YM=
golang.org/x/text v0.19.0/go.mod h1:BuEKDfySbSR4drPmRPG/7iBdf8hvFMuRexcpahXilzY=
golang.org/x/tools v0.21.1-0.20240508182429-e35e4ccd0d2d h1:vU5i/LfpvrRCpgM/VPfJLg5KjxD3E+hfT1SH+d9zLwg=
golang.org/x/tools v0.21.1-0.20240508182429-e35e4ccd0d2d/go.mod h1:aiJjzUbINMkxbQROHiO6hDPo2LHcIPhhQsa9DLh0yGk=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
Expand Down
28 changes: 19 additions & 9 deletions pkg/cli/find.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,14 @@ func (rc *findCommand) command() *cli.Command {
Name: "s3-key",
Usage: "S3 Bucket Key of terraform_remote_state data source",
},
&cli.StringFlag{
Name: "gcs-bucket",
Usage: "GCS Bucket Name of terraform_remote_state data source",
},
&cli.StringFlag{
Name: "gcs-prefix",
Usage: "GCS Bucket Prefix of terraform_remote_state data source",
},
&cli.StringSliceFlag{
Name: "output",
Usage: "Output name of terraform_remote_state data source",
Expand All @@ -65,14 +73,16 @@ func (rc *findCommand) action(c *cli.Context) error {
return fmt.Errorf("get the current directory: %w", err)
}
return find.Find(c.Context, logE, fs, &find.Param{ //nolint:wrapcheck
Format: c.String("output-format"),
PlanFile: c.String("plan-json"),
Root: c.String("base-dir"),
Dir: c.String("backend-dir"),
Key: c.String("s3-key"),
Bucket: c.String("s3-bucket"),
Outputs: c.StringSlice("output"),
Stdout: os.Stdout,
PWD: pwd,
Format: c.String("output-format"),
PlanFile: c.String("plan-json"),
Root: c.String("base-dir"),
Dir: c.String("backend-dir"),
Key: c.String("s3-key"),
Bucket: c.String("s3-bucket"),
GCSPrefix: c.String("gcs-prefix"),
GCSBucket: c.String("gcs-bucket"),
Outputs: c.StringSlice("output"),
Stdout: os.Stdout,
PWD: pwd,
})
}
17 changes: 11 additions & 6 deletions pkg/controller/find/backend_type.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@ import (
"github.com/sirupsen/logrus"
)

const (
backendTypeGCS = "gcs"
backendTypeS3 = "s3"
)

type Bucket struct {
Type string `json:"type"`
Bucket string `json:"bucket"`
Expand Down Expand Up @@ -69,12 +74,12 @@ func extractBackendFromJSON(src []byte, bucket *Bucket) (bool, error) {
return false, fmt.Errorf("unmarshal *.tf.json as JSON: %w", err)
}
if bj.Terraform.Backend.S3 != nil {
bj.Terraform.Backend.S3.Type = "s3"
bj.Terraform.Backend.S3.Type = backendTypeS3
bj.Terraform.Backend.S3.Copy(bucket)
return true, nil
}
if bj.Terraform.Backend.GCS != nil {
bj.Terraform.Backend.S3.Type = "gcs"
bj.Terraform.Backend.S3.Type = backendTypeGCS
bj.Terraform.Backend.S3.Copy(bucket)
return true, nil
}
Expand All @@ -83,13 +88,13 @@ func extractBackendFromJSON(src []byte, bucket *Bucket) (bool, error) {

func getHandlers() map[string]handleBackend {
return map[string]handleBackend{
"s3": handleS3Backend,
"gcs": handleGCSBackend,
backendTypeS3: handleS3Backend,
backendTypeGCS: handleGCSBackend,
}
}

func handleS3Backend(backend *hclsyntax.Block, bucket *Bucket) error {
bucket.Type = "s3"
bucket.Type = backendTypeS3
if key, ok := backend.Body.Attributes["key"]; ok {
val, diag := key.Expr.Value(nil)
if diag.HasErrors() {
Expand All @@ -116,7 +121,7 @@ func handleGCSBackend(backend *hclsyntax.Block, bucket *Bucket) error {
}
}
*/
bucket.Type = "gcs"
bucket.Type = backendTypeGCS
if prefix, ok := backend.Body.Attributes["prefix"]; ok {
val, diag := prefix.Expr.Value(nil)
if diag.HasErrors() {
Expand Down
40 changes: 23 additions & 17 deletions pkg/controller/find/find.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,17 @@ import (
)

type Param struct {
Format string
PlanFile string
Dir string
Root string
PWD string
Bucket string
Key string
Outputs []string
Stdout io.Writer
Format string
PlanFile string
Dir string
Root string
PWD string
Bucket string
Key string
GCSBucket string
GCSPrefix string
Outputs []string
Stdout io.Writer
}

type FileWithBackend struct {
Expand All @@ -45,9 +47,18 @@ type Dir struct {
}

func Find(_ context.Context, logE *logrus.Entry, afs afero.Fs, param *Param) error { //nolint:funlen,cyclop
bucket := &Bucket{
Bucket: param.Bucket,
Key: param.Key,
Prefix: param.GCSPrefix,
}
if param.Bucket != "" {
bucket.Type = backendTypeS3
}
// parse plan file and extract changed outputs
if err := validateParam(param); err != nil {
return err
if param.GCSBucket != "" {
bucket.Bucket = param.GCSBucket
bucket.Type = backendTypeGCS
}
changedOutputs := param.Outputs
if param.PlanFile != "" {
Expand All @@ -62,12 +73,7 @@ func Find(_ context.Context, logE *logrus.Entry, afs afero.Fs, param *Param) err
changedOutputs = arr
}

bucket := &Bucket{
Bucket: param.Bucket,
Key: param.Key,
}

if param.PlanFile != "" {
if bucket.Bucket == "" {
// parse HCLs in dir and extract backend configurations
if err := findBackendConfig(afs, param.Dir, bucket); err != nil {
return err
Expand Down
9 changes: 3 additions & 6 deletions pkg/controller/find/find_file.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,24 +13,21 @@ func findTFFiles(afs afero.Fs, baseDir string) ([]string, error) {
// Find **/*.tf
tfFiles := []string{}
ignorePatterns := []string{".terraform", ".git", ".github", "vendor", "node_modules"}
if err := doublestar.GlobWalk(afero.NewIOFS(afs), filepath.Join(baseDir, "**/*.tf"), func(path string, d fs.DirEntry) error {
if err := doublestar.GlobWalk(afero.NewIOFS(afs), filepath.Join(baseDir, "**/*.tf"), func(path string, _ fs.DirEntry) error {
if err := ignorePath(path, ignorePatterns); err != nil {
return err
}
if d.IsDir() {
return nil
}
tfFiles = append(tfFiles, path)
return nil
}, doublestar.WithNoFollow()); err != nil {
}, doublestar.WithNoFollow(), doublestar.WithFilesOnly()); err != nil {
return nil, fmt.Errorf("search files: %w", err)
}
return tfFiles, nil
}

func ignorePath(path string, ignorePatterns []string) error {
for _, ignoredDir := range ignorePatterns {
f, err := doublestar.PathMatch(ignoredDir, path)
f, err := doublestar.PathMatch("**/"+ignoredDir+"/**/*", path)
if err != nil {
return fmt.Errorf("check if a path is included in a ignored directory: %w", err)
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/controller/find/terraform_remote_state.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func extractRemoteStates(logE *logrus.Entry, src []byte, filePath string, backen
}
name := block.Labels[1]
if !bucket.Compare(backend) {
break
continue
}
states = append(states, &RemoteState{
Name: name,
Expand Down
21 changes: 0 additions & 21 deletions pkg/controller/find/validate.go

This file was deleted.

Loading