Skip to content

Commit

Permalink
Return errors instead of fatal logging and exits in public methods (n…
Browse files Browse the repository at this point in the history
…ot including methods called by the public method)
  • Loading branch information
MatthewJohn committed Jun 8, 2024
1 parent c280376 commit 45964d6
Showing 1 changed file with 8 additions and 10 deletions.
18 changes: 8 additions & 10 deletions lib/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -216,15 +216,16 @@ func InstallLatestImplicitVersion(dryRun bool, requestedVersion, customBinaryPat
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 nil
return fmt.Errorf("error parsing constraint %q: %v", requestedVersion, err)
}

// InstallVersion install Terraform product
Expand Down Expand Up @@ -261,13 +262,12 @@ 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
Expand Down Expand Up @@ -318,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 @@ -341,14 +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 interupt")
} 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
}

0 comments on commit 45964d6

Please sign in to comment.