Skip to content

Commit

Permalink
List tf files only in current directory (#91)
Browse files Browse the repository at this point in the history
* list tf files only in current directory #89

Signed-off-by: Denis Vaumoron <[email protected]>
  • Loading branch information
dvaumoron authored Apr 2, 2024
1 parent 5d7816a commit 69b6a84
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 39 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down
67 changes: 31 additions & 36 deletions versionmanager/semantic/parser/tf/tfparser.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@
package tfparser

import (
"io/fs"
"path/filepath"
"os"
"strings"

"github.com/hashicorp/go-hclog"
"github.com/hashicorp/hcl/v2"
Expand All @@ -35,7 +35,6 @@ const requiredVersionName = "required_version"

type extDescription struct {
value string
len int
parseHCL bool
}

Expand All @@ -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 {
Expand Down

0 comments on commit 69b6a84

Please sign in to comment.