Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add error return values to signatures of public methods to allow migrating to returning errors rather than Fatals in future #457

Merged
merged 8 commits into from
Jun 20, 2024
30 changes: 16 additions & 14 deletions lib/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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) {
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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)
Expand All @@ -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 */
Expand All @@ -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
}
18 changes: 11 additions & 7 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ var version string

func main() {

var err error = nil
switch {
case parameters.VersionFlag:
if version != "" {
Expand All @@ -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)
}
}