Skip to content

Commit

Permalink
Assign product entity to Params to avoid fetching product in main
Browse files Browse the repository at this point in the history
Issue #315
  • Loading branch information
MatthewJohn committed May 28, 2024
1 parent 1542aa0 commit 67433db
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 15 deletions.
2 changes: 2 additions & 0 deletions lib/param_parsing/parameters.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ type Params struct {
ShowLatestPre string
ShowLatestStable string
Product string
ProductEntity lib.Product
Version string
VersionFlag bool
}
Expand Down Expand Up @@ -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.
Expand Down
24 changes: 22 additions & 2 deletions lib/param_parsing/parameters_test.go
Original file line number Diff line number Diff line change
@@ -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) {
Expand Down Expand Up @@ -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()
})
Expand Down
19 changes: 6 additions & 13 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 != "" {
Expand All @@ -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)
Expand All @@ -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)
}
}

0 comments on commit 67433db

Please sign in to comment.