Skip to content

Commit

Permalink
Add support for "product" config key in tfswitch TOML file to set pro…
Browse files Browse the repository at this point in the history
…duct

Issue #315
  • Loading branch information
MatthewJohn committed May 28, 2024
1 parent 67433db commit 147fd93
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 4 deletions.
28 changes: 25 additions & 3 deletions lib/param_parsing/parameters_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
})
Expand All @@ -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 {
Expand All @@ -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) {
Expand Down Expand Up @@ -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.")
}
Expand Down
6 changes: 5 additions & 1 deletion lib/param_parsing/toml.go
Original file line number Diff line number Diff line change
@@ -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"
Expand Down Expand Up @@ -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
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
bin = "/usr/local/bin/terraform_from_toml"
version = "0.11.4"
log-level = "NOTICE"
product = "opentofu"

0 comments on commit 147fd93

Please sign in to comment.