From 147fd939ebbf3d7890128d997e0a1e19f98d0a93 Mon Sep 17 00:00:00 2001 From: Matthew John Date: Tue, 28 May 2024 06:53:41 +0100 Subject: [PATCH] Add support for "product" config key in tfswitch TOML file to set product Issue #315 --- lib/param_parsing/parameters_test.go | 28 +++++++++++++++++-- lib/param_parsing/toml.go | 6 +++- .../test_tfswitchtoml/.tfswitch.toml | 1 + 3 files changed, 31 insertions(+), 4 deletions(-) diff --git a/lib/param_parsing/parameters_test.go b/lib/param_parsing/parameters_test.go index fef50b49..1aa03a54 100644 --- a/lib/param_parsing/parameters_test.go +++ b/lib/param_parsing/parameters_test.go @@ -39,11 +39,18 @@ func TestGetParameters_params_are_overridden_by_toml_file(t *testing.T) { if actual != expected { t.Error("CustomBinaryPath Param was not as expected. Actual: " + actual + ", Expected: " + expected) } + expected = "0.11.4" actual = params.Version if actual != expected { t.Error("Version Param was not as expected. Actual: " + actual + ", Expected: " + expected) } + + expected = "opentofu" + actual = params.Product + if actual != expected { + t.Error("Product Param was not as expected. Actual: " + actual + ", Expected: " + expected) + } t.Cleanup(func() { getopt.CommandLine = getopt.New() }) @@ -52,7 +59,7 @@ func TestGetParameters_params_are_overridden_by_toml_file(t *testing.T) { func TestGetParameters_toml_params_are_overridden_by_cli(t *testing.T) { logger = lib.InitLogger("DEBUG") expected := "../../test-data/integration-tests/test_tfswitchtoml" - os.Args = []string{"cmd", "--chdir=" + expected, "--bin=/usr/test/bin"} + os.Args = []string{"cmd", "--chdir=" + expected, "--bin=/usr/test/bin", "--product=terraform"} params := GetParameters() actual := params.ChDirPath if actual != expected { @@ -64,11 +71,22 @@ func TestGetParameters_toml_params_are_overridden_by_cli(t *testing.T) { if actual != expected { t.Error("CustomBinaryPath Param was not as expected. Actual: " + actual + ", Expected: " + expected) } + expected = "0.11.4" actual = params.Version if actual != expected { t.Error("Version Param was not as expected. Actual: " + actual + ", Expected: " + expected) } + + expected = "terraform" + actual = params.Product + if actual != expected { + t.Error("Product Param was not as expected. Actual: " + actual + ", Expected: " + expected) + } + + t.Cleanup(func() { + getopt.CommandLine = getopt.New() + }) } func TestGetParameters_set_product_entity(t *testing.T) { @@ -99,10 +117,14 @@ func TestGetParameters_dry_run_wont_download_anything(t *testing.T) { os.Args = []string{"cmd", "--chdir=" + expected, "--bin=/tmp", "--dry-run"} params := GetParameters() installLocation := lib.GetInstallLocation(params.InstallPath) - installFileVersionPath := lib.ConvertExecutableExt(filepath.Join(installLocation, lib.VersionPrefix+params.Version)) + product := lib.GetProductById(params.Product) + if product == nil { + t.Error("Nil product returned") + } + installFileVersionPath := lib.ConvertExecutableExt(filepath.Join(installLocation, product.GetVersionPrefix()+params.Version)) // Make sure the file tfswitch WOULD download is absent _ = os.Remove(installFileVersionPath) - lib.InstallVersion(params.DryRun, params.Version, params.CustomBinaryPath, params.InstallPath, params.MirrorURL) + lib.InstallProductVersion(product, params.DryRun, params.Version, params.CustomBinaryPath, params.InstallPath, params.MirrorURL) if lib.FileExistsAndIsNotDir(installFileVersionPath) { t.Error("Dry run should NOT download any files.") } diff --git a/lib/param_parsing/toml.go b/lib/param_parsing/toml.go index e1748c73..b3158204 100644 --- a/lib/param_parsing/toml.go +++ b/lib/param_parsing/toml.go @@ -1,9 +1,10 @@ package param_parsing import ( + "path/filepath" + "github.com/spf13/viper" "github.com/warrensbox/terraform-switcher/lib" - "path/filepath" ) const tfSwitchTOMLFileName = ".tfswitch.toml" @@ -34,6 +35,9 @@ func getParamsTOML(params Params) (Params, error) { if viperParser.Get("version") != nil { params.Version = viperParser.GetString("version") } + if configKey := "product"; viperParser.Get(configKey) != nil { + params.Product = viperParser.GetString(configKey) + } } return params, nil } diff --git a/test-data/integration-tests/test_tfswitchtoml/.tfswitch.toml b/test-data/integration-tests/test_tfswitchtoml/.tfswitch.toml index 1089c0b4..e78a5a5a 100644 --- a/test-data/integration-tests/test_tfswitchtoml/.tfswitch.toml +++ b/test-data/integration-tests/test_tfswitchtoml/.tfswitch.toml @@ -1,3 +1,4 @@ bin = "/usr/local/bin/terraform_from_toml" version = "0.11.4" log-level = "NOTICE" +product = "opentofu" \ No newline at end of file