diff --git a/lib/param_parsing/parameters.go b/lib/param_parsing/parameters.go index 18bff92b..10c420ad 100644 --- a/lib/param_parsing/parameters.go +++ b/lib/param_parsing/parameters.go @@ -26,6 +26,7 @@ type Params struct { ShowLatestPre string ShowLatestStable string Product string + ProductEntity lib.Product Version string VersionFlag bool } @@ -91,6 +92,7 @@ func GetParameters() Params { logger.Fatalf("Invalid \"product\" configuration value: %q", params.Product) } else { // Use else as there is a warning that params maybe nil, as it does not see Fatalf as a break condition params.MirrorURL = product.GetDefaultMirrorUrl() + params.ProductEntity = product } // Logger config was changed by the config files. Reinitialise. diff --git a/lib/param_parsing/parameters_test.go b/lib/param_parsing/parameters_test.go index 79ce8fc4..fef50b49 100644 --- a/lib/param_parsing/parameters_test.go +++ b/lib/param_parsing/parameters_test.go @@ -1,11 +1,12 @@ package param_parsing import ( - "github.com/pborman/getopt" - "github.com/warrensbox/terraform-switcher/lib" "os" "path/filepath" "testing" + + "github.com/pborman/getopt" + "github.com/warrensbox/terraform-switcher/lib" ) func TestGetParameters_version_from_args(t *testing.T) { @@ -68,6 +69,25 @@ func TestGetParameters_toml_params_are_overridden_by_cli(t *testing.T) { if actual != expected { t.Error("Version Param was not as expected. Actual: " + actual + ", Expected: " + expected) } +} + +func TestGetParameters_set_product_entity(t *testing.T) { + logger = lib.InitLogger("DEBUG") + os.Args = []string{"cmd", "--product=opentofu"} + params := GetParameters() + + if expected := "opentofu"; params.ProductEntity.GetId() != expected { + t.Errorf("Incorrect product entity set on params. Expected: %q, Actual: %q", expected, params.ProductEntity.GetId()) + } + + getopt.CommandLine = getopt.New() + os.Args = []string{"cmd", "--product=terraform"} + params = GetParameters() + + if expected := "terraform"; params.ProductEntity.GetId() != expected { + t.Errorf("Incorrect product entity set on params. Expected: %q, Actual: %q", expected, params.ProductEntity.GetId()) + } + t.Cleanup(func() { getopt.CommandLine = getopt.New() }) diff --git a/main.go b/main.go index f697305b..4101c655 100644 --- a/main.go +++ b/main.go @@ -31,13 +31,6 @@ var version string func main() { - product := lib.GetProductById(parameters.Product) - if product == nil { - fmt.Printf("Invalid product specified") - lib.UsageMessage() - os.Exit(0) - } - switch { case parameters.VersionFlag: if version != "" { @@ -51,16 +44,16 @@ func main() { os.Exit(0) case parameters.ListAllFlag: /* show all terraform version including betas and RCs*/ - lib.InstallProductOption(product, true, parameters.DryRun, parameters.CustomBinaryPath, parameters.InstallPath, parameters.MirrorURL) + lib.InstallProductOption(parameters.ProductEntity, true, parameters.DryRun, parameters.CustomBinaryPath, parameters.InstallPath, parameters.MirrorURL) case parameters.LatestPre != "": /* latest pre-release implicit version. Ex: tfswitch --latest-pre 0.13 downloads 0.13.0-rc1 (latest) */ - lib.InstallLatestProductImplicitVersion(product, parameters.DryRun, parameters.LatestPre, parameters.CustomBinaryPath, parameters.InstallPath, parameters.MirrorURL, true) + lib.InstallLatestProductImplicitVersion(parameters.ProductEntity, parameters.DryRun, parameters.LatestPre, parameters.CustomBinaryPath, parameters.InstallPath, parameters.MirrorURL, true) case parameters.ShowLatestPre != "": /* show latest pre-release implicit version. Ex: tfswitch --latest-pre 0.13 downloads 0.13.0-rc1 (latest) */ lib.ShowLatestImplicitVersion(parameters.ShowLatestPre, parameters.MirrorURL, true) case parameters.LatestStable != "": /* latest implicit version. Ex: tfswitch --latest-stable 0.13 downloads 0.13.5 (latest) */ - lib.InstallLatestProductImplicitVersion(product, parameters.DryRun, parameters.LatestStable, parameters.CustomBinaryPath, parameters.InstallPath, parameters.MirrorURL, false) + lib.InstallLatestProductImplicitVersion(parameters.ProductEntity, parameters.DryRun, parameters.LatestStable, parameters.CustomBinaryPath, parameters.InstallPath, parameters.MirrorURL, false) case parameters.ShowLatestStable != "": /* show latest implicit stable version. Ex: tfswitch --show-latest-stable 0.13 downloads 0.13.5 (latest) */ lib.ShowLatestImplicitVersion(parameters.ShowLatestStable, parameters.MirrorURL, false) @@ -71,12 +64,12 @@ func main() { /* show latest stable version */ lib.ShowLatestVersion(parameters.MirrorURL) case parameters.Version != "": - lib.InstallProductVersion(product, parameters.DryRun, parameters.Version, parameters.CustomBinaryPath, parameters.InstallPath, parameters.MirrorURL) + lib.InstallProductVersion(parameters.ProductEntity, parameters.DryRun, parameters.Version, parameters.CustomBinaryPath, parameters.InstallPath, parameters.MirrorURL) case parameters.DefaultVersion != "": /* if default version is provided - Pick this instead of going for prompt */ - lib.InstallProductVersion(product, parameters.DryRun, parameters.DefaultVersion, parameters.CustomBinaryPath, parameters.InstallPath, parameters.MirrorURL) + lib.InstallProductVersion(parameters.ProductEntity, parameters.DryRun, parameters.DefaultVersion, parameters.CustomBinaryPath, parameters.InstallPath, parameters.MirrorURL) default: // Set list all false - only official release will be displayed - lib.InstallProductOption(product, false, parameters.DryRun, parameters.CustomBinaryPath, parameters.InstallPath, parameters.MirrorURL) + lib.InstallProductOption(parameters.ProductEntity, false, parameters.DryRun, parameters.CustomBinaryPath, parameters.InstallPath, parameters.MirrorURL) } }