Skip to content

Commit

Permalink
Better constraint matching
Browse files Browse the repository at this point in the history
  • Loading branch information
ChrisTerBeke committed Feb 27, 2024
1 parent 35ffecb commit 669e0f1
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 7 deletions.
1 change: 1 addition & 0 deletions go.mod
Expand Up @@ -4,6 +4,7 @@ go 1.21.4

require (
github.com/fatih/color v1.16.0
github.com/hashicorp/go-version v1.6.0
github.com/spf13/cobra v1.8.0
golang.org/x/net v0.21.0
)
Expand Down
2 changes: 2 additions & 0 deletions go.sum
@@ -1,6 +1,8 @@
github.com/cpuguy83/go-md2man/v2 v2.0.3/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
github.com/fatih/color v1.16.0 h1:zmkK9Ngbjj+K0yRhTVONQh1p/HknKYSlNT+vZCzyokM=
github.com/fatih/color v1.16.0/go.mod h1:fL2Sau1YI5c0pdGEVCbKQbLXB6edEj1ZgiY4NijnWvE=
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/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8=
github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw=
github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA=
Expand Down
18 changes: 15 additions & 3 deletions pkg/helpers/helper.go
Expand Up @@ -6,6 +6,8 @@ import (
"path/filepath"
"regexp"
"strings"

"github.com/hashicorp/go-version"
)

// IsPreReleaseVersion checks if the given version is a Terraform pre-release version

Check failure on line 13 in pkg/helpers/helper.go

View workflow job for this annotation

GitHub Actions / build

Comment should end in a period (godot)
Expand All @@ -15,18 +17,28 @@ func IsPreReleaseVersion(version string) bool {

// FindRequiredVersionInFile finds the required Terraform version in a given .tf file (using required_version = ">= x.x.x")
// TODO: improve logic by using go-version/constraint?
func FindRequiredVersionInFile(filepath string) string {
func FindRequiredVersionInFile(filepath string, availableVersions []string) string {
bytes, err := os.ReadFile(filepath)
if err != nil {
fmt.Printf("Unable to find version number in file: %s", filepath)
return ""
}
re := regexp.MustCompile(`required_version\s*=\s*">=? ?(\d+\.\d+\.\d+)"`)

re := regexp.MustCompile(`required_version\s?=\s?"([^"]+)"`)
match := re.FindStringSubmatch(string(bytes))
if len(match) == 0 {
return ""
}
return match[1]

for _, v := range availableVersions {
testVersion, _ := version.NewVersion(v)
constraints, _ := version.NewConstraint(match[1])
if constraints.Check(testVersion) {
return v
}
}

return ""
}

// FindTerraformFiles finds all .tf files in the current directory (module)

Check failure on line 44 in pkg/helpers/helper.go

View workflow job for this annotation

GitHub Actions / build

Comment should end in a period (godot)
Expand Down
7 changes: 4 additions & 3 deletions pkg/install/install.go
Expand Up @@ -55,20 +55,21 @@ func InstallLatestVersion(preRelease bool) {
func InstallRequiredVersion() {
terraformFiles := helpers.FindTerraformFiles()
if len(terraformFiles) == 0 {
fmt.Println("No Terraform files found in current directory")
fmt.Println("error: no Terraform files found in current directory")
os.Exit(1)
}

var foundVersion string
availableVersions := list.GetAvailableVersions()
for _, file := range terraformFiles {
requiredVersion := helpers.FindRequiredVersionInFile(file)
requiredVersion := helpers.FindRequiredVersionInFile(file, availableVersions)
if requiredVersion != "" {
foundVersion = requiredVersion
}
}

if len(foundVersion) == 0 {
fmt.Println("No required version found in current directory")
fmt.Println("error: no required version found in current directory")
os.Exit(1)
}

Expand Down
4 changes: 3 additions & 1 deletion pkg/use/use.go
Expand Up @@ -82,8 +82,10 @@ func UseRequiredVersion() {
fmt.Println("No Terraform files found in current directory")
os.Exit(1)
}

availableVersions := list.GetAvailableVersions()
for _, file := range terraformFiles {
requiredVersion := helpers.FindRequiredVersionInFile(file)
requiredVersion := helpers.FindRequiredVersionInFile(file, availableVersions)
if requiredVersion != "" {
UseVersion(requiredVersion)
break
Expand Down

0 comments on commit 669e0f1

Please sign in to comment.