diff --git a/lib/install.go b/lib/install.go index 8fe7d11c..7618bbc3 100644 --- a/lib/install.go +++ b/lib/install.go @@ -196,11 +196,12 @@ func InstallLatestVersion(dryRun bool, customBinaryPath, installPath string, mir } // InstallLatestProductVersion install latest stable tf version -func InstallLatestProductVersion(product Product, dryRun bool, customBinaryPath, installPath string, mirrorURL string) { +func InstallLatestProductVersion(product Product, dryRun bool, customBinaryPath, installPath string, mirrorURL string) error { tfversion, _ := getTFLatest(mirrorURL) if !dryRun { install(product, tfversion, customBinaryPath, installPath, mirrorURL) } + return nil } // InstallLatestImplicitVersion install latest - argument (version) must be provided @@ -212,17 +213,19 @@ func InstallLatestImplicitVersion(dryRun bool, requestedVersion, customBinaryPat } // InstallLatestProductImplicitVersion install latest - argument (version) must be provided -func InstallLatestProductImplicitVersion(product Product, dryRun bool, requestedVersion, customBinaryPath, installPath string, mirrorURL string, preRelease bool) { +func InstallLatestProductImplicitVersion(product Product, dryRun bool, requestedVersion, customBinaryPath, installPath string, mirrorURL string, preRelease bool) error { _, err := version.NewConstraint(requestedVersion) if err != nil { + // @TODO Should this return an error? logger.Errorf("Error parsing constraint %q: %v", requestedVersion, err) } tfversion, err := getTFLatestImplicit(mirrorURL, preRelease, requestedVersion) if err == nil && tfversion != "" && !dryRun { install(product, tfversion, customBinaryPath, installPath, mirrorURL) + return nil } - logger.Errorf("Error parsing constraint %q: %v", requestedVersion, err) PrintInvalidMinorTFVersion() + return fmt.Errorf("error parsing constraint %q: %v", requestedVersion, err) } // InstallVersion install Terraform product @@ -234,7 +237,7 @@ func InstallVersion(dryRun bool, version, customBinaryPath, installPath, mirrorU } // InstallProductVersion install with provided version as argument -func InstallProductVersion(product Product, dryRun bool, version, customBinaryPath, installPath, mirrorURL string) { +func InstallProductVersion(product Product, dryRun bool, version, customBinaryPath, installPath, mirrorURL string) error { logger.Debugf("Install version %s. Dry run: %s", version, strconv.FormatBool(dryRun)) if !dryRun { if validVersionFormat(version) { @@ -248,7 +251,7 @@ func InstallProductVersion(product Product, dryRun bool, version, customBinaryPa ChangeProductSymlink(product, installFileVersionPath, customBinaryPath) logger.Infof("Switched %s to version %q", product.GetName(), requestedVersion) addRecent(requestedVersion, installPath, product) //add to recent file for faster lookup - return + return nil } // If the requested version had not been downloaded before @@ -259,15 +262,15 @@ func InstallProductVersion(product Product, dryRun bool, version, customBinaryPa if exist { install(product, requestedVersion, customBinaryPath, installPath, mirrorURL) } else { - logger.Fatalf("The provided terraform version does not exist: %q.\n Try `tfswitch -l` to see all available versions", requestedVersion) + return fmt.Errorf("the provided terraform version does not exist: %q.\n Try `tfswitch -l` to see all available versions", requestedVersion) } } else { PrintInvalidTFVersion() - logger.Error("Args must be a valid terraform version") UsageMessage() - os.Exit(1) + return fmt.Errorf("args must be a valid terraform version") } } + return nil } // InstallProductOption displays & installs tf version @@ -288,7 +291,7 @@ type VersionSelector struct { // InstallProductOption displays & installs tf version /* listAll = true - all versions including beta and rc will be displayed */ /* listAll = false - only official stable release are displayed */ -func InstallProductOption(product Product, listAll, dryRun bool, customBinaryPath, installPath string, mirrorURL string) { +func InstallProductOption(product Product, listAll, dryRun bool, customBinaryPath, installPath string, mirrorURL string) error { var selectVersions []VersionSelector var versionMap map[string]bool = make(map[string]bool) @@ -315,8 +318,7 @@ func InstallProductOption(product Product, listAll, dryRun bool, customBinaryPat } if len(selectVersions) == 0 { - logger.Fatalf("%s version list is empty: %s", product.GetName(), mirrorURL) - os.Exit(1) + return fmt.Errorf("%s version list is empty: %s", product.GetName(), mirrorURL) } /* prompt user to select version of terraform */ @@ -338,13 +340,13 @@ func InstallProductOption(product Product, listAll, dryRun bool, customBinaryPat if errPrompt != nil { if errPrompt.Error() == "^C" { // Cancel execution - os.Exit(1) + return fmt.Errorf("user interrupt") } else { - logger.Fatalf("Prompt failed %v", errPrompt) + return fmt.Errorf("prompt failed %v", errPrompt) } } if !dryRun { install(product, selectVersions[selectedItx].Version, customBinaryPath, installPath, mirrorURL) } - os.Exit(0) + return nil } diff --git a/main.go b/main.go index 4101c655..1ce6f1e5 100644 --- a/main.go +++ b/main.go @@ -31,6 +31,7 @@ var version string func main() { + var err error = nil switch { case parameters.VersionFlag: if version != "" { @@ -44,32 +45,35 @@ func main() { os.Exit(0) case parameters.ListAllFlag: /* show all terraform version including betas and RCs*/ - lib.InstallProductOption(parameters.ProductEntity, true, parameters.DryRun, parameters.CustomBinaryPath, parameters.InstallPath, parameters.MirrorURL) + err = 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(parameters.ProductEntity, parameters.DryRun, parameters.LatestPre, parameters.CustomBinaryPath, parameters.InstallPath, parameters.MirrorURL, true) + err = 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(parameters.ProductEntity, parameters.DryRun, parameters.LatestStable, parameters.CustomBinaryPath, parameters.InstallPath, parameters.MirrorURL, false) + err = 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) case parameters.LatestFlag: /* latest stable version */ - lib.InstallLatestVersion(parameters.DryRun, parameters.CustomBinaryPath, parameters.InstallPath, parameters.MirrorURL) + err = lib.InstallLatestProductVersion(parameters.ProductEntity, parameters.DryRun, parameters.CustomBinaryPath, parameters.InstallPath, parameters.MirrorURL) case parameters.ShowLatestFlag: /* show latest stable version */ lib.ShowLatestVersion(parameters.MirrorURL) case parameters.Version != "": - lib.InstallProductVersion(parameters.ProductEntity, parameters.DryRun, parameters.Version, parameters.CustomBinaryPath, parameters.InstallPath, parameters.MirrorURL) + err = 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(parameters.ProductEntity, parameters.DryRun, parameters.DefaultVersion, parameters.CustomBinaryPath, parameters.InstallPath, parameters.MirrorURL) + err = 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(parameters.ProductEntity, false, parameters.DryRun, parameters.CustomBinaryPath, parameters.InstallPath, parameters.MirrorURL) + err = lib.InstallProductOption(parameters.ProductEntity, false, parameters.DryRun, parameters.CustomBinaryPath, parameters.InstallPath, parameters.MirrorURL) + } + if err != nil { + logger.Fatal(err) } }