From 18b9ecbb2440a8ac29e5ba69b3862b1c18bbf5d8 Mon Sep 17 00:00:00 2001 From: Johannes Brunswicker Date: Tue, 2 Apr 2024 09:49:43 +0200 Subject: [PATCH] - extract params into own struct for better usability - extracted all parameter parsing into an explicit package and ensured the precedence --- README.md | 14 +- lib/param_parsing/environment.go | 8 + lib/param_parsing/environment_test.go | 17 + lib/param_parsing/parameters.go | 106 +++++ lib/param_parsing/parameters_test.go | 78 ++++ lib/param_parsing/terraform_version.go | 22 + lib/param_parsing/terraform_version_test.go | 17 + lib/param_parsing/terragrunt.go | 36 ++ lib/param_parsing/terragrunt_test.go | 19 + lib/param_parsing/tfswitch.go | 22 + lib/param_parsing/tfswitch_test.go | 15 + lib/param_parsing/toml.go | 32 ++ lib/param_parsing/toml_test.go | 33 ++ lib/param_parsing/versiontf.go | 26 ++ lib/param_parsing/versiontf_test.go | 19 + lib/semver.go | 17 +- main.go | 412 +++--------------- main_test.go | 2 +- test-data/test_precedence/.terraform-version | 1 + test-data/test_precedence/.tfswitch.toml | 2 + test-data/test_precedence/.tfswitchrc | 1 + test-data/test_precedence/terragrunt.hcl | 1 + .../test_terraform-version/.terraform-version | 2 +- test-data/test_terragrunt_hcl/terragrunt.hcl | 17 +- test-data/test_tfswitchrc/.tfswitchrc | 2 +- test-data/test_tfswitchtoml/.tfswitch.toml | 4 +- 26 files changed, 533 insertions(+), 392 deletions(-) create mode 100644 lib/param_parsing/environment.go create mode 100644 lib/param_parsing/environment_test.go create mode 100644 lib/param_parsing/parameters.go create mode 100644 lib/param_parsing/parameters_test.go create mode 100644 lib/param_parsing/terraform_version.go create mode 100644 lib/param_parsing/terraform_version_test.go create mode 100644 lib/param_parsing/terragrunt.go create mode 100644 lib/param_parsing/terragrunt_test.go create mode 100644 lib/param_parsing/tfswitch.go create mode 100644 lib/param_parsing/tfswitch_test.go create mode 100644 lib/param_parsing/toml.go create mode 100644 lib/param_parsing/toml_test.go create mode 100644 lib/param_parsing/versiontf.go create mode 100644 lib/param_parsing/versiontf_test.go create mode 100644 test-data/test_precedence/.terraform-version create mode 100644 test-data/test_precedence/.tfswitch.toml create mode 100644 test-data/test_precedence/.tfswitchrc create mode 100644 test-data/test_precedence/terragrunt.hcl diff --git a/README.md b/README.md index 0ce94a1f..182d5545 100644 --- a/README.md +++ b/README.md @@ -320,12 +320,14 @@ jobs: ``` ## Order of precedence -| Order | Method | -| --- | ----------- | -| 1 | .tfswitch.toml | -| 2 | .tfswitchrc | -| 3 | .terraform-version | -| 4 | Environment variable | +| Order | Method | +|-------|-----------------------| +| 1 | .tfswitch.toml | +| 2 | .tfswitchrc | +| 3 | .terraform-version | +| 4 | version.tf (TF Modul) | +| 5 | terragrunt-hcl | +| 6 | Environment variable | With 1 being the highest precedence and 4 the lowest *(If you disagree with this order of precedence, please open an issue)* diff --git a/lib/param_parsing/environment.go b/lib/param_parsing/environment.go new file mode 100644 index 00000000..086ac12a --- /dev/null +++ b/lib/param_parsing/environment.go @@ -0,0 +1,8 @@ +package param_parsing + +import "os" + +func GetParamsFromEnvironment(params Params) Params { + params.Version = os.Getenv("TF_VERSION") + return params +} diff --git a/lib/param_parsing/environment_test.go b/lib/param_parsing/environment_test.go new file mode 100644 index 00000000..438e7d07 --- /dev/null +++ b/lib/param_parsing/environment_test.go @@ -0,0 +1,17 @@ +package param_parsing + +import ( + "os" + "testing" +) + +func TestGetParamsFromEnvironment_version_from_env(t *testing.T) { + var params Params + expected := "1.0.0_from_env" + _ = os.Setenv("TF_VERSION", expected) + params = initParams(params) + params = GetParamsFromEnvironment(params) + if params.Version != expected { + t.Error("Determined version is not matchching. Got " + params.Version + ", expected " + expected) + } +} diff --git a/lib/param_parsing/parameters.go b/lib/param_parsing/parameters.go new file mode 100644 index 00000000..1ed086d2 --- /dev/null +++ b/lib/param_parsing/parameters.go @@ -0,0 +1,106 @@ +package param_parsing + +import ( + "fmt" + "github.com/pborman/getopt" + "github.com/warrensbox/terraform-switcher/lib" + "os" +) + +type Params struct { + CustomBinaryPath string + ListAllFlag bool + LatestPre string + ShowLatestPre string + LatestStable string + ShowLatestStable string + LatestFlag bool + ShowLatestFlag bool + MirrorURL string + ChDirPath string + VersionFlag bool + DefaultVersion string + HelpFlag bool + Version string +} + +const ( + defaultMirror = "https://releases.hashicorp.com/terraform" + defaultLatest = "" +) + +func GetParameters() Params { + var params Params + params = initParams(params) + + getopt.StringVarLong(¶ms.ChDirPath, "chdir", 'c', "Switch to a different working directory before executing the given command. Ex: tfswitch --chdir terraform_project will run tfswitch in the terraform_project directory") + getopt.BoolVarLong(¶ms.VersionFlag, "version", 'v', "Displays the version of tfswitch") + getopt.BoolVarLong(¶ms.HelpFlag, "help", 'h', "Displays help message") + getopt.StringVarLong(¶ms.MirrorURL, "mirror", 'm', "Install from a remote API other than the default. Default: "+defaultMirror) + getopt.StringVarLong(¶ms.CustomBinaryPath, "bin", 'b', "Custom binary path. Ex: tfswitch -b "+lib.ConvertExecutableExt("/Users/username/bin/terraform")) + getopt.StringVarLong(¶ms.LatestPre, "latest-pre", 'p', "Latest pre-release implicit version. Ex: tfswitch --latest-pre 0.13 downloads 0.13.0-rc1 (latest)") + getopt.StringVarLong(¶ms.ShowLatestPre, "show-latest-pre", 'P', "Show latest pre-release implicit version. Ex: tfswitch --show-latest-pre 0.13 prints 0.13.0-rc1 (latest)") + getopt.StringVarLong(¶ms.LatestStable, "latest-stable", 's', "Latest implicit version based on a constraint. Ex: tfswitch --latest-stable 0.13.0 downloads 0.13.7 and 0.13 downloads 0.15.5 (latest)") + getopt.StringVarLong(¶ms.ShowLatestStable, "show-latest-stable", 'S', "Show latest implicit version. Ex: tfswitch --show-latest-stable 0.13 prints 0.13.7 (latest)") + getopt.StringVarLong(¶ms.DefaultVersion, "default", 'd', "Default to this version in case no other versions could be detected. Ex: tfswitch --default 1.2.4") + getopt.BoolVarLong(¶ms.ListAllFlag, "list-all", 'l', "List all versions of terraform - including beta and rc") + getopt.BoolVarLong(¶ms.LatestFlag, "latest", 'u', "Get latest stable version") + getopt.BoolVarLong(¶ms.ShowLatestFlag, "show-latest", 'U', "Show latest stable version") + + // Parse the command line parameters to fetch stuff like chdir + getopt.Parse() + + // Read configuration files in increasing precedence (least precedence first) + params = GetParamsFromEnvironment(params) + params = GetVersionFromTerragrunt(params) + params = GetVersionFromVersionsTF(params) + params = GetParamsFromTerraformVersion(params) + params = GetParamsFromTfSwitch(params) + params = GetParamsTOML(params) + + // Parse again to overwrite anything that might by defined on the cli AND in any config file (CLI always wins) + getopt.Parse() + args := getopt.Args() + if len(args) == 1 { + /* version provided on command line as arg */ + params.Version = args[0] + } + return params +} + +func getCommandlineParams(params Params) { + getopt.StringVarLong(¶ms.CustomBinaryPath, "bin", 'b', "Custom binary path. Ex: tfswitch -b "+lib.ConvertExecutableExt("/Users/username/bin/terraform")) + getopt.StringVarLong(¶ms.LatestPre, "latest-pre", 'p', "Latest pre-release implicit version. Ex: tfswitch --latest-pre 0.13 downloads 0.13.0-rc1 (latest)") + getopt.StringVarLong(¶ms.ShowLatestPre, "show-latest-pre", 'P', "Show latest pre-release implicit version. Ex: tfswitch --show-latest-pre 0.13 prints 0.13.0-rc1 (latest)") + getopt.StringVarLong(¶ms.LatestStable, "latest-stable", 's', "Latest implicit version based on a constraint. Ex: tfswitch --latest-stable 0.13.0 downloads 0.13.7 and 0.13 downloads 0.15.5 (latest)") + getopt.StringVarLong(¶ms.ShowLatestStable, "show-latest-stable", 'S', "Show latest implicit version. Ex: tfswitch --show-latest-stable 0.13 prints 0.13.7 (latest)") + getopt.StringVarLong(¶ms.DefaultVersion, "default", 'd', "Default to this version in case no other versions could be detected. Ex: tfswitch --default 1.2.4") + + getopt.BoolVarLong(¶ms.ListAllFlag, "list-all", 'l', "List all versions of terraform - including beta and rc") + getopt.BoolVarLong(¶ms.LatestFlag, "latest", 'u', "Get latest stable version") + getopt.BoolVarLong(¶ms.ShowLatestFlag, "show-latest", 'U', "Show latest stable version") +} + +func initParams(params Params) Params { + params.ChDirPath = lib.GetCurrentDirectory() + params.CustomBinaryPath = lib.ConvertExecutableExt(lib.GetDefaultBin()) + params.MirrorURL = defaultMirror + params.LatestPre = defaultLatest + params.ShowLatestPre = defaultLatest + params.LatestStable = defaultLatest + params.ShowLatestStable = defaultLatest + params.MirrorURL = defaultMirror + params.DefaultVersion = defaultLatest + params.ListAllFlag = false + params.LatestFlag = false + params.ShowLatestFlag = false + params.VersionFlag = false + params.HelpFlag = false + return params +} + +func UsageMessage() { + fmt.Print("\n\n") + getopt.PrintUsage(os.Stderr) + fmt.Println("Supply the terraform version as an argument, or choose from a menu") +} diff --git a/lib/param_parsing/parameters_test.go b/lib/param_parsing/parameters_test.go new file mode 100644 index 00000000..4b43c57e --- /dev/null +++ b/lib/param_parsing/parameters_test.go @@ -0,0 +1,78 @@ +package param_parsing + +import ( + "github.com/pborman/getopt" + "os" + "testing" +) + +func TestGetParameters_version_from_args(t *testing.T) { + expected := "0.13args" + os.Args = []string{"cmd", expected} + params := GetParameters() + actual := params.Version + if actual != expected { + t.Error("Version Param was not parsed correctly. Actual: " + actual + ", Expected: " + expected) + } + t.Cleanup(func() { + getopt.CommandLine = getopt.New() + }) +} +func TestGetParameters_params_are_overridden_by_toml_file(t *testing.T) { + t.Cleanup(func() { + getopt.CommandLine = getopt.New() + }) + expected := "../../test-data/test_tfswitchtoml" + os.Args = []string{"cmd", "--chdir=" + expected} + params := GetParameters() + actual := params.ChDirPath + if actual != expected { + t.Error("ChDir Param was not parsed correctly. Actual: " + actual + ", Expected: " + expected) + } + + expected = "/usr/local/bin/terraform_from_toml" + actual = params.CustomBinaryPath + if actual != expected { + t.Error("CustomBinaryPath Param was not as expected. Actual: " + actual + ", Expected: " + expected) + } + expected = "0.11.3_toml" + actual = params.Version + if actual != expected { + t.Error("Version Param was not as expected. Actual: " + actual + ", Expected: " + expected) + } +} +func TestGetParameters_toml_params_are_overridden_by_cli(t *testing.T) { + t.Cleanup(func() { + getopt.CommandLine = getopt.New() + }) + expected := "../../test-data/test_tfswitchtoml" + os.Args = []string{"cmd", "--chdir=" + expected, "--bin=/usr/test/bin"} + params := GetParameters() + actual := params.ChDirPath + if actual != expected { + t.Error("ChDir Param was not parsed correctly. Actual: " + actual + ", Expected: " + expected) + } + + expected = "/usr/test/bin" + actual = params.CustomBinaryPath + if actual != expected { + t.Error("CustomBinaryPath Param was not as expected. Actual: " + actual + ", Expected: " + expected) + } + expected = "0.11.3_toml" + actual = params.Version + if actual != expected { + t.Error("Version Param was not as expected. Actual: " + actual + ", Expected: " + expected) + } +} + +func TestGetParameters_check_config_precedence(t *testing.T) { + t.Cleanup(func() { + getopt.CommandLine = getopt.New() + }) + os.Args = []string{"cmd", "--chdir=../../test-data/test_precedence"} + parameters := GetParameters() + expected := "0.11.3_toml" + if parameters.Version != expected { + t.Error("Version Param was not as expected. Actual: " + parameters.Version + ", Expected: " + expected) + } +} diff --git a/lib/param_parsing/terraform_version.go b/lib/param_parsing/terraform_version.go new file mode 100644 index 00000000..5139a194 --- /dev/null +++ b/lib/param_parsing/terraform_version.go @@ -0,0 +1,22 @@ +package param_parsing + +import ( + "fmt" + "github.com/warrensbox/terraform-switcher/lib" + "log" + "os" + "strings" +) + +func GetParamsFromTerraformVersion(params Params) Params { + filePath := params.ChDirPath + "/.terraform-version" + if lib.CheckFileExist(filePath) { + fmt.Printf("Reading configuration from %s\n", filePath) + content, err := os.ReadFile(filePath) + if err != nil { + log.Fatal("Could not read file content", filePath, err) + } + params.Version = strings.TrimSpace(string(content)) + } + return params +} diff --git a/lib/param_parsing/terraform_version_test.go b/lib/param_parsing/terraform_version_test.go new file mode 100644 index 00000000..457b6adf --- /dev/null +++ b/lib/param_parsing/terraform_version_test.go @@ -0,0 +1,17 @@ +package param_parsing + +import ( + "fmt" + "testing" +) + +func TestGetParamsFromTerraformVersion(t *testing.T) { + var params Params + params.ChDirPath = "../../test-data/test_terraform-version" + params = GetParamsFromTerraformVersion(params) + expected := "0.11.0_tfversion" + if params.Version != expected { + fmt.Printf("Version from .terraform-version not read correctly. Got: %v, Expect: %v", params.Version, expected) + t.Errorf("Version from .terraform-version not read correctly. Got: %v, Expect: %v", params.Version, expected) + } +} diff --git a/lib/param_parsing/terragrunt.go b/lib/param_parsing/terragrunt.go new file mode 100644 index 00000000..a4561650 --- /dev/null +++ b/lib/param_parsing/terragrunt.go @@ -0,0 +1,36 @@ +package param_parsing + +import ( + "fmt" + "github.com/hashicorp/hcl2/gohcl" + "github.com/hashicorp/hcl2/hclparse" + "github.com/warrensbox/terraform-switcher/lib" + "log" +) + +type terragruntVersionConstraints struct { + TerraformVersionConstraint string `hcl:"terraform_version_constraint"` +} + +func GetVersionFromTerragrunt(params Params) Params { + filePath := params.ChDirPath + "/terragrunt.hcl" + if lib.CheckFileExist(filePath) { + fmt.Printf("Reading configuration from %s\n", filePath) + parser := hclparse.NewParser() + hclFile, diagnostics := parser.ParseHCLFile(filePath) + if diagnostics.HasErrors() { + log.Fatal("Unable to parse HCL file", filePath) + } + var versionFromTerragrunt terragruntVersionConstraints + diagnostics = gohcl.DecodeBody(hclFile.Body, nil, &versionFromTerragrunt) + if diagnostics.HasErrors() { + log.Fatal("Could not decode body of HCL file.") + } + version, err := lib.GetSemver(versionFromTerragrunt.TerraformVersionConstraint, params.MirrorURL) + if err != nil { + log.Fatal("Could not determine semantic version") + } + params.Version = version + } + return params +} diff --git a/lib/param_parsing/terragrunt_test.go b/lib/param_parsing/terragrunt_test.go new file mode 100644 index 00000000..c0452678 --- /dev/null +++ b/lib/param_parsing/terragrunt_test.go @@ -0,0 +1,19 @@ +package param_parsing + +import ( + "github.com/hashicorp/go-version" + "testing" +) + +func TestGetVersionFromTerragrunt(t *testing.T) { + var params Params + params = initParams(params) + params.ChDirPath = "../../test-data/test_terragrunt_hcl" + params = GetVersionFromTerragrunt(params) + v1, _ := version.NewVersion("0.13") + v2, _ := version.NewVersion("0.14") + actualVersion, _ := version.NewVersion(params.Version) + if !actualVersion.GreaterThanOrEqual(v1) || !actualVersion.LessThan(v2) { + t.Error("Determined version is not between 0.13 and 0.14") + } +} diff --git a/lib/param_parsing/tfswitch.go b/lib/param_parsing/tfswitch.go new file mode 100644 index 00000000..b8f33df4 --- /dev/null +++ b/lib/param_parsing/tfswitch.go @@ -0,0 +1,22 @@ +package param_parsing + +import ( + "fmt" + "github.com/warrensbox/terraform-switcher/lib" + "log" + "os" + "strings" +) + +func GetParamsFromTfSwitch(params Params) Params { + filePath := params.ChDirPath + "/.tfswitchrc" + if lib.CheckFileExist(filePath) { + fmt.Printf("Reading configuration from %s\n", filePath) + content, err := os.ReadFile(filePath) + if err != nil { + log.Fatal("Could not read file content", filePath, err) + } + params.Version = strings.TrimSpace(string(content)) + } + return params +} diff --git a/lib/param_parsing/tfswitch_test.go b/lib/param_parsing/tfswitch_test.go new file mode 100644 index 00000000..5cac9213 --- /dev/null +++ b/lib/param_parsing/tfswitch_test.go @@ -0,0 +1,15 @@ +package param_parsing + +import ( + "testing" +) + +func TestGetParamsFromTfSwitch(t *testing.T) { + var params Params + params.ChDirPath = "../../test-data/test_tfswitchrc" + params = GetParamsFromTfSwitch(params) + expected := "0.10.5_tfswitch" + if params.Version != expected { + t.Error("Version from tfswitchrc not read correctly. Actual: " + params.Version + ", Expected: " + expected) + } +} diff --git a/lib/param_parsing/toml.go b/lib/param_parsing/toml.go new file mode 100644 index 00000000..06800eea --- /dev/null +++ b/lib/param_parsing/toml.go @@ -0,0 +1,32 @@ +package param_parsing + +import ( + "fmt" + "github.com/spf13/viper" + "github.com/warrensbox/terraform-switcher/lib" + "log" +) + +// GetParamsTOML parses everything in the toml file, return required version and bin path +func GetParamsTOML(params Params) Params { + tomlFileName := ".tfswitch.toml" + tomlPath := params.ChDirPath + "/" + tomlFileName + if lib.CheckFileExist(tomlPath) { + fmt.Printf("Reading configuration from %s\n", tomlPath) + configfileName := lib.GetFileName(tomlFileName) + viper.SetConfigType("toml") + viper.SetConfigName(configfileName) + viper.AddConfigPath(params.ChDirPath) + + errs := viper.ReadInConfig() // Find and read the config file + if errs != nil { + log.Fatalf("Unable to read %s provided\n", tomlPath) + } + + params.Version = viper.GetString("version") //attempt to get the version if it's provided in the toml + params.CustomBinaryPath = viper.GetString("bin") + } else { + fmt.Println("No configuration file at " + tomlPath) + } + return params +} diff --git a/lib/param_parsing/toml_test.go b/lib/param_parsing/toml_test.go new file mode 100644 index 00000000..e7a6d2d5 --- /dev/null +++ b/lib/param_parsing/toml_test.go @@ -0,0 +1,33 @@ +package param_parsing + +import ( + "testing" +) + +func prepare() Params { + var params Params + params.ChDirPath = "../../test-data/test_tfswitchtoml" + return params +} + +func TestGetParamsTOML_BinaryPath(t *testing.T) { + expected := "/usr/local/bin/terraform_from_toml" + params := prepare() + params = GetParamsTOML(params) + if params.CustomBinaryPath != expected { + t.Log("Actual:", params.CustomBinaryPath) + t.Log("Expected:", expected) + t.Error("BinaryPath not matching") + } +} + +func TestGetParamsTOML_Version(t *testing.T) { + expected := "0.11.3_toml" + params := prepare() + params = GetParamsTOML(params) + if params.Version != expected { + t.Log("Actual:", params.Version) + t.Log("Expected:", expected) + t.Error("Version not matching") + } +} diff --git a/lib/param_parsing/versiontf.go b/lib/param_parsing/versiontf.go new file mode 100644 index 00000000..55de0ff8 --- /dev/null +++ b/lib/param_parsing/versiontf.go @@ -0,0 +1,26 @@ +package param_parsing + +import ( + "fmt" + "github.com/hashicorp/terraform-config-inspect/tfconfig" + "github.com/warrensbox/terraform-switcher/lib" + "log" +) + +func GetVersionFromVersionsTF(params Params) Params { + filePath := params.ChDirPath + "/version.tf" + if lib.CheckFileExist(filePath) { + fmt.Printf("Reading version from %s\n", filePath) + module, err := tfconfig.LoadModule(params.ChDirPath) + if err != nil { + log.Fatal("Could not load terraform module") + } + tfconstraint := module.RequiredCore[0] + version, err2 := lib.GetSemver(tfconstraint, params.MirrorURL) + if err2 != nil { + log.Fatal("Could not determine semantic version") + } + params.Version = version + } + return params +} diff --git a/lib/param_parsing/versiontf_test.go b/lib/param_parsing/versiontf_test.go new file mode 100644 index 00000000..345fa96f --- /dev/null +++ b/lib/param_parsing/versiontf_test.go @@ -0,0 +1,19 @@ +package param_parsing + +import ( + "github.com/hashicorp/go-version" + "testing" +) + +func TestGetVersionFromVersionsTF(t *testing.T) { + var params Params + params = initParams(params) + params.ChDirPath = "../../test-data/test_versiontf" + params = GetVersionFromVersionsTF(params) + v1, _ := version.NewVersion("1.0.0") + v2, _ := version.NewVersion("2.0.0") + actualVersion, _ := version.NewVersion(params.Version) + if !actualVersion.GreaterThanOrEqual(v1) || !actualVersion.LessThan(v2) { + t.Error("Determined version is not between 1.0.0 and 2.0.0") + } +} diff --git a/lib/semver.go b/lib/semver.go index 366b92ee..2e91ba56 100644 --- a/lib/semver.go +++ b/lib/semver.go @@ -7,17 +7,16 @@ import ( semver "github.com/hashicorp/go-version" ) -// GetSemver : returns version that will be installed based on server constaint provided -func GetSemver(tfconstraint *string, mirrorURL *string) (string, error) { - +// GetSemver : returns version that will be installed based on server constraint provided +func GetSemver(tfconstraint string, mirrorURL string) (string, error) { listAll := true - tflist, _ := GetTFList(*mirrorURL, listAll) //get list of versions - fmt.Printf("Reading required version from constraint: %s\n", *tfconstraint) - tfversion, err := SemVerParser(tfconstraint, tflist) + tflist, _ := GetTFList(mirrorURL, listAll) //get list of versions + fmt.Printf("Reading required version from constraint: %s\n", tfconstraint) + tfversion, err := SemVerParser(&tfconstraint, tflist) return tfversion, err } -// ValidateSemVer : Goes through the list of terraform version, return a valid tf version for contraint provided +// SemVerParser : Goes through the list of terraform version, return a valid tf version for contraint provided func SemVerParser(tfconstraint *string, tflist []string) (string, error) { tfversion := "" constraints, err := semver.NewConstraint(*tfconstraint) //NewConstraint returns a Constraints instance that a Version instance can be checked against @@ -50,12 +49,12 @@ func SemVerParser(tfconstraint *string, tflist []string) (string, error) { return "", fmt.Errorf("error parsing constraint: %s", *tfconstraint) } -// Print invalid TF version +// PrintInvalidTFVersion Print invalid TF version func PrintInvalidTFVersion() { fmt.Println("Version does not exist or invalid terraform version format.\n Format should be #.#.# or #.#.#-@# where # are numbers and @ are word characters.\n For example, 0.11.7 and 0.11.9-beta1 are valid versions") } -// Print invalid TF version +// PrintInvalidMinorTFVersion Print invalid minor TF version func PrintInvalidMinorTFVersion() { fmt.Println("Invalid minor terraform version format. Format should be #.# where # are numbers. For example, 0.11 is valid version") } diff --git a/main.go b/main.go index cf31d027..751d2fa2 100644 --- a/main.go +++ b/main.go @@ -19,253 +19,98 @@ package main import ( "fmt" - "io/ioutil" + "github.com/warrensbox/terraform-switcher/lib/param_parsing" "log" "os" "path/filepath" "strings" semver "github.com/hashicorp/go-version" - "github.com/hashicorp/hcl2/gohcl" - "github.com/hashicorp/hcl2/hclparse" - "github.com/hashicorp/terraform-config-inspect/tfconfig" - "github.com/mitchellh/go-homedir" - "github.com/manifoldco/promptui" - "github.com/pborman/getopt" - "github.com/spf13/viper" - lib "github.com/warrensbox/terraform-switcher/lib" ) const ( - defaultMirror = "https://releases.hashicorp.com/terraform" - defaultLatest = "" - tfvFilename = ".terraform-version" - rcFilename = ".tfswitchrc" - tomlFilename = ".tfswitch.toml" - tgHclFilename = "terragrunt.hcl" + tfvFilename = ".terraform-version" + versionPrefix = "terraform_" ) var version = "0.12.0\n" func main() { - dir := lib.GetCurrentDirectory() - custBinPath := getopt.StringLong("bin", 'b', lib.ConvertExecutableExt(lib.GetDefaultBin()), "Custom binary path. Ex: tfswitch -b "+lib.ConvertExecutableExt("/Users/username/bin/terraform")) - listAllFlag := getopt.BoolLong("list-all", 'l', "List all versions of terraform - including beta and rc") - latestPre := getopt.StringLong("latest-pre", 'p', defaultLatest, "Latest pre-release implicit version. Ex: tfswitch --latest-pre 0.13 downloads 0.13.0-rc1 (latest)") - showLatestPre := getopt.StringLong("show-latest-pre", 'P', defaultLatest, "Show latest pre-release implicit version. Ex: tfswitch --show-latest-pre 0.13 prints 0.13.0-rc1 (latest)") - latestStable := getopt.StringLong("latest-stable", 's', defaultLatest, "Latest implicit version based on a constraint. Ex: tfswitch --latest-stable 0.13.0 downloads 0.13.7 and 0.13 downloads 0.15.5 (latest)") - showLatestStable := getopt.StringLong("show-latest-stable", 'S', defaultLatest, "Show latest implicit version. Ex: tfswitch --show-latest-stable 0.13 prints 0.13.7 (latest)") - latestFlag := getopt.BoolLong("latest", 'u', "Get latest stable version") - showLatestFlag := getopt.BoolLong("show-latest", 'U', "Show latest stable version") - mirrorURL := getopt.StringLong("mirror", 'm', defaultMirror, "Install from a remote API other than the default. Default: "+defaultMirror) - chDirPath := getopt.StringLong("chdir", 'c', dir, "Switch to a different working directory before executing the given command. Ex: tfswitch --chdir terraform_project will run tfswitch in the terraform_project directory") - versionFlag := getopt.BoolLong("version", 'v', "Displays the version of tfswitch") - defaultVersion := getopt.StringLong("default", 'd', defaultLatest, "Default to this version in case no other versions could be detected. Ex: tfswitch --default 1.2.4") - helpFlag := getopt.BoolLong("help", 'h', "Displays help message") - _ = versionFlag - - getopt.Parse() - args := getopt.Args() - - homedir, err := homedir.Dir() - if err != nil { - fmt.Printf("Unable to get home directory: %v\n", err) - os.Exit(1) - } - TFVersionFile := filepath.Join(*chDirPath, tfvFilename) //settings for .terraform-version file in current directory (tfenv compatible) - RCFile := filepath.Join(*chDirPath, rcFilename) //settings for .tfswitchrc file in current directory (backward compatible purpose) - TOMLConfigFile := filepath.Join(*chDirPath, tomlFilename) //settings for .tfswitch.toml file in current directory (option to specify bin directory) - HomeTOMLConfigFile := filepath.Join(homedir, tomlFilename) //settings for .tfswitch.toml file in home directory (option to specify bin directory) - TGHACLFile := filepath.Join(*chDirPath, tgHclFilename) //settings for terragrunt.hcl file in current directory (option to specify bin directory) + parameters := param_parsing.GetParameters() + //defaults := lib.GetDefaults() switch { - case *versionFlag: - //if *versionFlag { + case parameters.VersionFlag: fmt.Printf("\nVersion: %v\n", version) - case *helpFlag: - //} else if *helpFlag { - usageMessage() - /* Checks if the .tfswitch.toml file exist in home or current directory - * This block checks to see if the tfswitch toml file is provided in the current path. - * If the .tfswitch.toml file exist, it has a higher precedence than the .tfswitchrc file - * You can specify the custom binary path and the version you desire - * If you provide a custom binary path with the -b option, this will override the bin value in the toml file - * If you provide a version on the command line, this will override the version value in the toml file - */ - case fileExists(TOMLConfigFile) || fileExists(HomeTOMLConfigFile): - version := "" - binPath := *custBinPath - if fileExists(TOMLConfigFile) { //read from toml from current directory - version, binPath = getParamsTOML(binPath, *chDirPath) - } else { // else read from toml from home directory - version, binPath = getParamsTOML(binPath, homedir) - } - - switch { - /* GIVEN A TOML FILE, */ + case parameters.HelpFlag: + param_parsing.UsageMessage() + case parameters.ListAllFlag: /* show all terraform version including betas and RCs*/ - case *listAllFlag: - listAll := true //set list all true - all versions including beta and rc will be displayed - installOption(listAll, &binPath, mirrorURL) + installOption(true, parameters.CustomBinaryPath, parameters.MirrorURL) + case parameters.LatestPre != "": /* latest pre-release implicit version. Ex: tfswitch --latest-pre 0.13 downloads 0.13.0-rc1 (latest) */ - case *latestPre != "": - preRelease := true - installLatestImplicitVersion(*latestPre, &binPath, mirrorURL, preRelease) - /* latest implicit version. Ex: tfswitch --latest 0.13 downloads 0.13.5 (latest) */ - case *latestStable != "": - preRelease := false - installLatestImplicitVersion(*latestStable, &binPath, mirrorURL, preRelease) + installLatestImplicitVersion(parameters.LatestPre, parameters.CustomBinaryPath, parameters.MirrorURL, true) + case parameters.ShowLatestPre != "": + /* show latest pre-release implicit version. Ex: tfswitch --latest-pre 0.13 downloads 0.13.0-rc1 (latest) */ + showLatestImplicitVersion(parameters.ShowLatestPre, parameters.MirrorURL, true) + case parameters.LatestStable != "": + /* latest implicit version. Ex: tfswitch --latest-stable 0.13 downloads 0.13.5 (latest) */ + installLatestImplicitVersion(parameters.LatestStable, parameters.CustomBinaryPath, parameters.MirrorURL, false) + case parameters.ShowLatestStable != "": + /* show latest implicit stable version. Ex: tfswitch --show-latest-stable 0.13 downloads 0.13.5 (latest) */ + showLatestImplicitVersion(parameters.ShowLatestStable, parameters.MirrorURL, false) + case parameters.LatestFlag: /* latest stable version */ - case *latestFlag: - installLatestVersion(&binPath, mirrorURL) - /* version provided on command line as arg */ - case len(args) == 1: - installVersion(args[0], &binPath, mirrorURL) - /* provide an tfswitchrc file (IN ADDITION TO A TOML FILE) */ - case fileExists(RCFile) && len(args) == 0: - readingFileMsg(rcFilename) - tfversion := retrieveFileContents(RCFile) - installVersion(tfversion, &binPath, mirrorURL) - /* if .terraform-version file found (IN ADDITION TO A TOML FILE) */ - case fileExists(TFVersionFile) && len(args) == 0: - readingFileMsg(tfvFilename) - tfversion := retrieveFileContents(TFVersionFile) - installVersion(tfversion, &binPath, mirrorURL) - /* if versions.tf file found (IN ADDITION TO A TOML FILE) */ - case checkTFModuleFileExist(*chDirPath) && len(args) == 0: - installTFProvidedModule(*chDirPath, &binPath, mirrorURL) - /* if Terraform Version environment variable is set */ - case checkTFEnvExist() && len(args) == 0 && version == "": - tfversion := os.Getenv("TF_VERSION") - fmt.Printf("Terraform version environment variable: %s\n", tfversion) - installVersion(tfversion, &binPath, mirrorURL) - /* if terragrunt.hcl file found (IN ADDITION TO A TOML FILE) */ - case fileExists(TGHACLFile) && checkVersionDefinedHCL(&TGHACLFile) && len(args) == 0: - installTGHclFile(&TGHACLFile, &binPath, mirrorURL) - // if no arg is provided - but toml file is provided - case version != "": - installVersion(version, &binPath, mirrorURL) - default: - listAll := false //set list all false - only official release will be displayed - installOption(listAll, &binPath, mirrorURL) - } - - /* show all terraform version including betas and RCs*/ - case *listAllFlag: - installWithListAll(custBinPath, mirrorURL) - - /* latest pre-release implicit version. Ex: tfswitch --latest-pre 0.13 downloads 0.13.0-rc1 (latest) */ - case *latestPre != "": - preRelease := true - installLatestImplicitVersion(*latestPre, custBinPath, mirrorURL, preRelease) - - /* show latest pre-release implicit version. Ex: tfswitch --latest-pre 0.13 downloads 0.13.0-rc1 (latest) */ - case *showLatestPre != "": - preRelease := true - showLatestImplicitVersion(*showLatestPre, custBinPath, mirrorURL, preRelease) - - /* latest implicit version. Ex: tfswitch --latest 0.13 downloads 0.13.5 (latest) */ - case *latestStable != "": - preRelease := false - installLatestImplicitVersion(*latestStable, custBinPath, mirrorURL, preRelease) - - /* show latest implicit stable version. Ex: tfswitch --latest 0.13 downloads 0.13.5 (latest) */ - case *showLatestStable != "": - preRelease := false - showLatestImplicitVersion(*showLatestStable, custBinPath, mirrorURL, preRelease) - - /* latest stable version */ - case *latestFlag: - installLatestVersion(custBinPath, mirrorURL) - - /* show latest stable version */ - case *showLatestFlag: - showLatestVersion(custBinPath, mirrorURL) - - /* version provided on command line as arg */ - case len(args) == 1: - installVersion(args[0], custBinPath, mirrorURL) - - /* provide an tfswitchrc file */ - case fileExists(RCFile) && len(args) == 0: - readingFileMsg(rcFilename) - tfversion := retrieveFileContents(RCFile) - installVersion(tfversion, custBinPath, mirrorURL) - - /* if .terraform-version file found */ - case fileExists(TFVersionFile) && len(args) == 0: - readingFileMsg(tfvFilename) - tfversion := retrieveFileContents(TFVersionFile) - installVersion(tfversion, custBinPath, mirrorURL) - - /* if versions.tf file found */ - case checkTFModuleFileExist(*chDirPath) && len(args) == 0: - installTFProvidedModule(*chDirPath, custBinPath, mirrorURL) - - /* if terragrunt.hcl file found */ - case fileExists(TGHACLFile) && checkVersionDefinedHCL(&TGHACLFile) && len(args) == 0: - installTGHclFile(&TGHACLFile, custBinPath, mirrorURL) - - /* if Terraform Version environment variable is set */ - case checkTFEnvExist() && len(args) == 0: - tfversion := os.Getenv("TF_VERSION") - fmt.Printf("Terraform version environment variable: %s\n", tfversion) - installVersion(tfversion, custBinPath, mirrorURL) - - /* if default version is provided - Pick this instead of going for prompt */ - case *defaultVersion != "": - installVersion(*defaultVersion, custBinPath, mirrorURL) - - // if no arg is provided + installLatestVersion(parameters.CustomBinaryPath, parameters.MirrorURL) + case parameters.ShowLatestFlag: + /* show latest stable version */ + showLatestVersion(parameters.MirrorURL) + case parameters.Version != "": + installVersion(parameters.Version, parameters.CustomBinaryPath, parameters.MirrorURL) + case parameters.DefaultVersion != "": + /* if default version is provided - Pick this instead of going for prompt */ + installVersion(parameters.DefaultVersion, parameters.CustomBinaryPath, parameters.MirrorURL) default: - listAll := false //set list all false - only official release will be displayed - installOption(listAll, custBinPath, mirrorURL) + //set list all false - only official release will be displayed + installOption(false, parameters.CustomBinaryPath, parameters.MirrorURL) } } -/* Helper functions */ - -// install with all possible versions, including beta and rc -func installWithListAll(custBinPath, mirrorURL *string) { - listAll := true //set list all true - all versions including beta and rc will be displayed - installOption(listAll, custBinPath, mirrorURL) -} - // install latest stable tf version -func installLatestVersion(custBinPath, mirrorURL *string) { - tfversion, _ := lib.GetTFLatest(*mirrorURL) - lib.Install(tfversion, *custBinPath, *mirrorURL) +func installLatestVersion(customBinaryPath, mirrorURL string) { + tfversion, _ := lib.GetTFLatest(mirrorURL) + lib.Install(tfversion, customBinaryPath, mirrorURL) } // show install latest stable tf version -func showLatestVersion(custBinPath, mirrorURL *string) { - tfversion, _ := lib.GetTFLatest(*mirrorURL) +func showLatestVersion(mirrorURL string) { + tfversion, _ := lib.GetTFLatest(mirrorURL) fmt.Printf("%s\n", tfversion) } // install latest - argument (version) must be provided -func installLatestImplicitVersion(requestedVersion string, custBinPath, mirrorURL *string, preRelease bool) { +func installLatestImplicitVersion(requestedVersion, customBinaryPath, mirrorURL string, preRelease bool) { _, err := semver.NewConstraint(requestedVersion) if err != nil { fmt.Printf("error parsing constraint: %s\n", err) } //if lib.ValidMinorVersionFormat(requestedVersion) { - tfversion, err := lib.GetTFLatestImplicit(*mirrorURL, preRelease, requestedVersion) + tfversion, err := lib.GetTFLatestImplicit(mirrorURL, preRelease, requestedVersion) if err == nil && tfversion != "" { - lib.Install(tfversion, *custBinPath, *mirrorURL) + lib.Install(tfversion, customBinaryPath, mirrorURL) } fmt.Printf("Error parsing constraint: %s\n", err) lib.PrintInvalidMinorTFVersion() } // show latest - argument (version) must be provided -func showLatestImplicitVersion(requestedVersion string, custBinPath, mirrorURL *string, preRelease bool) { +func showLatestImplicitVersion(requestedVersion, mirrorURL string, preRelease bool) { if lib.ValidMinorVersionFormat(requestedVersion) { - tfversion, _ := lib.GetTFLatestImplicit(*mirrorURL, preRelease, requestedVersion) + tfversion, _ := lib.GetTFLatestImplicit(mirrorURL, preRelease, requestedVersion) if len(tfversion) > 0 { fmt.Printf("%s\n", tfversion) } else { @@ -278,7 +123,7 @@ func showLatestImplicitVersion(requestedVersion string, custBinPath, mirrorURL * } // install with provided version as argument -func installVersion(arg string, custBinPath *string, mirrorURL *string) { +func installVersion(arg, customBinaryPath, mirrorURL string) { if lib.ValidVersionFormat(arg) { requestedVersion := arg @@ -287,19 +132,19 @@ func installVersion(arg string, custBinPath *string, mirrorURL *string) { installFileVersionPath := lib.ConvertExecutableExt(filepath.Join(installLocation, versionPrefix+requestedVersion)) recentDownloadFile := lib.CheckFileExist(installFileVersionPath) if recentDownloadFile { - lib.ChangeSymlink(installFileVersionPath, *custBinPath) + lib.ChangeSymlink(installFileVersionPath, customBinaryPath) fmt.Printf("Switched terraform to version %q \n", requestedVersion) lib.AddRecent(requestedVersion) //add to recent file for faster lookup os.Exit(0) } //if the requested version had not been downloaded before - listAll := true //set list all true - all versions including beta and rc will be displayed - tflist, _ := lib.GetTFList(*mirrorURL, listAll) //get list of versions + //set list all true - all versions including beta and rc will be displayed + tflist, _ := lib.GetTFList(mirrorURL, true) //get list of versions exist := lib.VersionExist(requestedVersion, tflist) //check if version exist before downloading it if exist { - lib.Install(requestedVersion, *custBinPath, *mirrorURL) + lib.Install(requestedVersion, customBinaryPath, mirrorURL) } else { fmt.Println("The provided terraform version does not exist. Try `tfswitch -l` to see all available versions.") os.Exit(1) @@ -308,111 +153,20 @@ func installVersion(arg string, custBinPath *string, mirrorURL *string) { } else { lib.PrintInvalidTFVersion() fmt.Println("Args must be a valid terraform version") - usageMessage() - os.Exit(1) - } -} - -// retrive file content of regular file -func retrieveFileContents(file string) string { - fileContents, err := ioutil.ReadFile(file) - if err != nil { - fmt.Printf("Failed to read %s file. Follow the README.md instructions for setup. https://github.com/warrensbox/terraform-switcher/blob/master/README.md\n", tfvFilename) - fmt.Printf("Error: %s\n", err) - os.Exit(1) - } - tfversion := strings.TrimSuffix(string(fileContents), "\n") - return tfversion -} - -// Print message reading file content of : -func readingFileMsg(filename string) { - fmt.Printf("Reading file %s \n", filename) -} - -// fileExists checks if a file exists and is not a directory before we try using it to prevent further errors. -func fileExists(filename string) bool { - info, err := os.Stat(filename) - if os.IsNotExist(err) { - return false - } - return !info.IsDir() -} - -// fileExists checks if a file exists and is not a directory before we -// try using it to prevent further errors. -func checkTFModuleFileExist(dir string) bool { - - module, _ := tfconfig.LoadModule(dir) - if len(module.RequiredCore) >= 1 { - return true - } - return false -} - -// checkTFEnvExist - checks if the TF_VERSION environment variable is set -func checkTFEnvExist() bool { - tfversion := os.Getenv("TF_VERSION") - if tfversion != "" { - return true - } - return false -} - -/* parses everything in the toml file, return required version and bin path */ -func getParamsTOML(binPath string, dir string) (string, string) { - path, err := homedir.Dir() - - if err != nil { - fmt.Printf("Unable to get home directory: %v\n", err) + fmt.Println("Arg:", arg) + param_parsing.UsageMessage() os.Exit(1) } - - if dir == path { - path = "home directory" - } else { - path = "current directory" - } - fmt.Printf("Reading configuration from %s\n", path+" for "+tomlFilename) //takes the default bin (defaultBin) if user does not specify bin path - configfileName := lib.GetFileName(tomlFilename) //get the config file - viper.SetConfigType("toml") - viper.SetConfigName(configfileName) - viper.AddConfigPath(dir) - - errs := viper.ReadInConfig() // Find and read the config file - if errs != nil { - fmt.Printf("Unable to read %s provided\n", tomlFilename) // Handle errors reading the config file - fmt.Println(errs) - os.Exit(1) // exit immediately if config file provided but it is unable to read it - } - - bin := viper.Get("bin") // read custom binary location - if binPath == lib.ConvertExecutableExt(lib.GetDefaultBin()) && bin != nil { // if the bin path is the same as the default binary path and if the custom binary is provided in the toml file (use it) - binPath = os.ExpandEnv(bin.(string)) - } - //fmt.Println(binPath) //uncomment this to debug - version := viper.Get("version") //attempt to get the version if it's provided in the toml - if version == nil { - version = "" - } - - return version.(string), binPath -} - -func usageMessage() { - fmt.Print("\n\n") - getopt.PrintUsage(os.Stderr) - fmt.Println("Supply the terraform version as an argument, or choose from a menu") } /* installOption : displays & installs tf version */ /* listAll = true - all versions including beta and rc will be displayed */ /* listAll = false - only official stable release are displayed */ -func installOption(listAll bool, custBinPath, mirrorURL *string) { - tflist, _ := lib.GetTFList(*mirrorURL, listAll) //get list of versions - recentVersions, _ := lib.GetRecentVersions() //get recent versions from RECENT file - tflist = append(recentVersions, tflist...) //append recent versions to the top of the list - tflist = lib.RemoveDuplicateVersions(tflist) //remove duplicate version +func installOption(listAll bool, customBinaryPath, mirrorURL string) { + tflist, _ := lib.GetTFList(mirrorURL, listAll) //get list of versions + recentVersions, _ := lib.GetRecentVersions() //get recent versions from RECENT file + tflist = append(recentVersions, tflist...) //append recent versions to the top of the list + tflist = lib.RemoveDuplicateVersions(tflist) //remove duplicate version if len(tflist) == 0 { fmt.Println("[ERROR] : List is empty") @@ -432,60 +186,6 @@ func installOption(listAll bool, custBinPath, mirrorURL *string) { os.Exit(1) } - lib.Install(tfversion, *custBinPath, *mirrorURL) + lib.Install(tfversion, customBinaryPath, mirrorURL) os.Exit(0) } - -// install when tf file is provided -func installTFProvidedModule(dir string, custBinPath, mirrorURL *string) { - fmt.Printf("Reading required version from terraform file\n") - module, _ := tfconfig.LoadModule(dir) - tfconstraint := module.RequiredCore[0] //we skip duplicated definitions and use only first one - installFromConstraint(&tfconstraint, custBinPath, mirrorURL) -} - -// install using a version constraint -func installFromConstraint(tfconstraint *string, custBinPath, mirrorURL *string) { - - tfversion, err := lib.GetSemver(tfconstraint, mirrorURL) - if err == nil { - lib.Install(tfversion, *custBinPath, *mirrorURL) - } - fmt.Println(err) - fmt.Println("No version found to match constraint. Follow the README.md instructions for setup. https://github.com/warrensbox/terraform-switcher/blob/master/README.md") - os.Exit(1) -} - -// Install using version constraint from terragrunt file -func installTGHclFile(tgFile *string, custBinPath, mirrorURL *string) { - fmt.Printf("Terragrunt file found: %s\n", *tgFile) - parser := hclparse.NewParser() - file, diags := parser.ParseHCLFile(*tgFile) //use hcl parser to parse HCL file - if diags.HasErrors() { - fmt.Println("Unable to parse HCL file") - os.Exit(1) - } - var version terragruntVersionConstraints - gohcl.DecodeBody(file.Body, nil, &version) - installFromConstraint(&version.TerraformVersionConstraint, custBinPath, mirrorURL) -} - -type terragruntVersionConstraints struct { - TerraformVersionConstraint string `hcl:"terraform_version_constraint"` -} - -// check if version is defined in hcl file /* lazy-emergency fix - will improve later */ -func checkVersionDefinedHCL(tgFile *string) bool { - parser := hclparse.NewParser() - file, diags := parser.ParseHCLFile(*tgFile) //use hcl parser to parse HCL file - if diags.HasErrors() { - fmt.Println("Unable to parse HCL file") - os.Exit(1) - } - var version terragruntVersionConstraints - gohcl.DecodeBody(file.Body, nil, &version) - if version == (terragruntVersionConstraints{}) { - return false - } - return true -} diff --git a/main_test.go b/main_test.go index e6be58fb..f4156d6e 100644 --- a/main_test.go +++ b/main_test.go @@ -1,4 +1,4 @@ -package main_test +package main import ( "os/user" diff --git a/test-data/test_precedence/.terraform-version b/test-data/test_precedence/.terraform-version new file mode 100644 index 00000000..e1a6ee7b --- /dev/null +++ b/test-data/test_precedence/.terraform-version @@ -0,0 +1 @@ +0.11.0_tfversion diff --git a/test-data/test_precedence/.tfswitch.toml b/test-data/test_precedence/.tfswitch.toml new file mode 100644 index 00000000..9af1b141 --- /dev/null +++ b/test-data/test_precedence/.tfswitch.toml @@ -0,0 +1,2 @@ +bin = "/usr/local/bin/terraform_from_toml" +version = "0.11.3_toml" diff --git a/test-data/test_precedence/.tfswitchrc b/test-data/test_precedence/.tfswitchrc new file mode 100644 index 00000000..4b90204e --- /dev/null +++ b/test-data/test_precedence/.tfswitchrc @@ -0,0 +1 @@ +0.10.5_tfswitch diff --git a/test-data/test_precedence/terragrunt.hcl b/test-data/test_precedence/terragrunt.hcl new file mode 100644 index 00000000..549e96ec --- /dev/null +++ b/test-data/test_precedence/terragrunt.hcl @@ -0,0 +1 @@ +terraform_version_constraint = ">= 0.13, < 0.14" \ No newline at end of file diff --git a/test-data/test_terraform-version/.terraform-version b/test-data/test_terraform-version/.terraform-version index d9df1bbc..e1a6ee7b 100644 --- a/test-data/test_terraform-version/.terraform-version +++ b/test-data/test_terraform-version/.terraform-version @@ -1 +1 @@ -0.11.0 +0.11.0_tfversion diff --git a/test-data/test_terragrunt_hcl/terragrunt.hcl b/test-data/test_terragrunt_hcl/terragrunt.hcl index 806b304f..549e96ec 100644 --- a/test-data/test_terragrunt_hcl/terragrunt.hcl +++ b/test-data/test_terragrunt_hcl/terragrunt.hcl @@ -1,16 +1 @@ -include { - path = "${find_in_parent_folders()}" -} - -terraform { - source = "..." - - extra_arguments "variables" { - commands = get_terraform_commands_that_need_vars() - } -} - inputs = merge( - jsondecode(file("${find_in_parent_folders("general.tfvars")}")) -) - -terraform_version_constraint=">= 0.13, < 0.14" \ No newline at end of file +terraform_version_constraint = ">= 0.13, < 0.14" \ No newline at end of file diff --git a/test-data/test_tfswitchrc/.tfswitchrc b/test-data/test_tfswitchrc/.tfswitchrc index 9028ec63..4b90204e 100644 --- a/test-data/test_tfswitchrc/.tfswitchrc +++ b/test-data/test_tfswitchrc/.tfswitchrc @@ -1 +1 @@ -0.10.5 +0.10.5_tfswitch diff --git a/test-data/test_tfswitchtoml/.tfswitch.toml b/test-data/test_tfswitchtoml/.tfswitch.toml index f234e9cc..9af1b141 100644 --- a/test-data/test_tfswitchtoml/.tfswitch.toml +++ b/test-data/test_tfswitchtoml/.tfswitch.toml @@ -1,2 +1,2 @@ -bin = "/usr/local/bin/terraform" -version = "0.11.3" +bin = "/usr/local/bin/terraform_from_toml" +version = "0.11.3_toml"