diff --git a/go.mod b/go.mod index a909b999..3509abc6 100644 --- a/go.mod +++ b/go.mod @@ -7,7 +7,7 @@ require ( github.com/ProtonMail/gopenpgp/v2 v2.7.5 github.com/PuerkitoBio/goquery v1.9.1 github.com/fatih/color v1.16.0 - github.com/hashicorp/go-hclog v1.6.2 + github.com/hashicorp/go-hclog v1.6.3 github.com/hashicorp/go-version v1.6.0 github.com/hashicorp/hcl/v2 v2.20.1 github.com/spf13/cobra v1.8.0 diff --git a/go.sum b/go.sum index 36cf15de..e564b3dd 100644 --- a/go.sum +++ b/go.sum @@ -31,8 +31,8 @@ github.com/go-test/deep v1.1.0 h1:WOcxcdHcvdgThNXjw0t76K42FXTU7HpNQWHpA2HHNlg= github.com/go-test/deep v1.1.0/go.mod h1:5C2ZWiW0ErCdrYzpqxLbTX7MG14M9iiw8DgHncVwcsE= github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI= github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY= -github.com/hashicorp/go-hclog v1.6.2 h1:NOtoftovWkDheyUM/8JW3QMiXyxJK3uHRK7wV04nD2I= -github.com/hashicorp/go-hclog v1.6.2/go.mod h1:W4Qnvbt70Wk/zYJryRzDRU/4r0kIg0PVHBcfoyhpF5M= +github.com/hashicorp/go-hclog v1.6.3 h1:Qr2kF+eVWjTiYmU7Y31tYlP1h0q/X3Nl3tPGdaB11/k= +github.com/hashicorp/go-hclog v1.6.3/go.mod h1:W4Qnvbt70Wk/zYJryRzDRU/4r0kIg0PVHBcfoyhpF5M= github.com/hashicorp/go-version v1.6.0 h1:feTTfFNnjP967rlCxM/I9g701jU+RN74YKx2mOkIeek= github.com/hashicorp/go-version v1.6.0/go.mod h1:fltr4n8CU8Ke44wwGCBoEymUuxUHl09ZGVZPK5anwXA= github.com/hashicorp/hcl/v2 v2.20.1 h1:M6hgdyz7HYt1UN9e61j+qKJBqR3orTWbI1HKBJEdxtc= diff --git a/versionmanager/semantic/parser/tf/tfparser.go b/versionmanager/semantic/parser/tf/tfparser.go index 908527c3..60c13c21 100644 --- a/versionmanager/semantic/parser/tf/tfparser.go +++ b/versionmanager/semantic/parser/tf/tfparser.go @@ -19,8 +19,8 @@ package tfparser import ( - "io/fs" - "path/filepath" + "os" + "strings" "github.com/hashicorp/go-hclog" "github.com/hashicorp/hcl/v2" @@ -35,7 +35,6 @@ const requiredVersionName = "required_version" type extDescription struct { value string - len int parseHCL bool } @@ -49,64 +48,60 @@ var versionPartialSchema = &hcl.BodySchema{ //nolint Attributes: []hcl.AttributeSchema{{Name: requiredVersionName}}, } -func init() { - for i, desc := range exts { - desc.len = len(desc.value) - exts[i] = desc // override with updated copy - } -} - func GatherRequiredVersion(conf *config.Config) ([]string, error) { conf.Displayer.Display("Scan project to find .tf files") - var requireds []string var foundFiles []string + if conf.Displayer.IsDebug() { + defer func() { + if len(foundFiles) == 0 { + conf.Displayer.Log(hclog.Debug, "No .tf file found") + } else { + conf.Displayer.Log(hclog.Debug, "Read", "filePaths", foundFiles) + } + }() + } + + entries, err := os.ReadDir(".") + if err != nil { + return nil, err + } + + var requireds []string + var parsedFile *hcl.File + var diags hcl.Diagnostics parser := hclparse.NewParser() - err := filepath.WalkDir(".", func(path string, entry fs.DirEntry, err error) error { - if err != nil || entry.IsDir() { - return err + for _, entry := range entries { + if entry.IsDir() { + continue } - pathLen := len(path) - var parsedFile *hcl.File - var diags hcl.Diagnostics + name := entry.Name() for _, extDesc := range exts { - if start := pathLen - extDesc.len; start < 0 || path[start:] != extDesc.value { + if !strings.HasSuffix(name, extDesc.value) { continue } - foundFiles = append(foundFiles, path) + foundFiles = append(foundFiles, name) if extDesc.parseHCL { - parsedFile, diags = parser.ParseHCLFile(path) + parsedFile, diags = parser.ParseHCLFile(name) } else { - parsedFile, diags = parser.ParseJSONFile(path) + parsedFile, diags = parser.ParseJSONFile(name) } if diags.HasErrors() { - return diags + return nil, diags } if parsedFile == nil { - return nil + continue } extracted := extractRequiredVersion(parsedFile.Body, conf) requireds = append(requireds, extracted...) - - return nil - } - - return nil - }) - - if conf.Displayer.IsDebug() { - if len(foundFiles) == 0 { - conf.Displayer.Log(hclog.Debug, "No .tf file found") - } else { - conf.Displayer.Log(hclog.Debug, "Read", "filePaths", foundFiles) } } - return requireds, err + return requireds, nil } func extractRequiredVersion(body hcl.Body, conf *config.Config) []string {