Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #6 from tfversion/feature/required-version
Feature: install/use required version
- Loading branch information
Showing
10 changed files
with
199 additions
and
49 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,7 +20,6 @@ linters: | |
- exportloopref | ||
- forcetypeassert | ||
- funlen | ||
- godot | ||
- godox | ||
- gofmt | ||
- gosimple | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,60 @@ | ||
package helpers | ||
|
||
import "strings" | ||
import ( | ||
"fmt" | ||
"os" | ||
"path/filepath" | ||
"regexp" | ||
"strings" | ||
|
||
"github.com/hashicorp/go-version" | ||
) | ||
|
||
// IsPreReleaseVersion checks if the given version is a Terraform pre-release version | ||
func IsPreReleaseVersion(version string) bool { | ||
return strings.Contains(version, "-alpha") || strings.Contains(version, "-beta") || strings.Contains(version, "-rc") | ||
} | ||
|
||
// FindRequiredVersionInFile finds the required Terraform version in a given .tf file (using required_version = ">= x.x.x") | ||
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?"([^"]+)"`) | ||
match := re.FindStringSubmatch(string(bytes)) | ||
if len(match) == 0 { | ||
return "" | ||
} | ||
|
||
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) | ||
func FindTerraformFiles() []string { | ||
var files []string | ||
err := filepath.Walk(".", func(path string, info os.FileInfo, err error) error { | ||
if info.IsDir() { | ||
return nil | ||
} | ||
if strings.HasSuffix(path, ".tf") { | ||
files = append(files, path) | ||
} | ||
return nil | ||
}) | ||
if err != nil { | ||
fmt.Println("No Terraform files found in current directory:", err) | ||
os.Exit(1) | ||
} | ||
return files | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters