Skip to content

Commit

Permalink
Improve printing and errors (#15)
Browse files Browse the repository at this point in the history
* Scaffold alias command

* Implement alias functionality

* Move actual versions to separate directory

* Add second example to alias --help

* Fix alias command

* Implement list --aliases

* Fix use command for aliases

* Update readme with alias command example

* Add list --aliases to readme

* Fix help example

* Replace all occurances of os.Exit(1) in pgk with helpers.ExitWithError()

* Use helpers.ColoredVersion everywhere

* Replace all occurances of exit(1) with the helper

* Add helper function for printing an error with help command

* Fix path check condition

* Error = red

* fix: typo in func

---------

Co-authored-by: Bruno Schaatsbergen <58337159+bschaatsbergen@users.noreply.github.com>
Co-authored-by: Bruno Schaatsbergen <git@bschaatsbergen.com>
  • Loading branch information
3 people committed Mar 5, 2024
1 parent 2d80717 commit 88021a6
Show file tree
Hide file tree
Showing 13 changed files with 145 additions and 181 deletions.
10 changes: 3 additions & 7 deletions cmd/alias.go
@@ -1,13 +1,10 @@
package cmd

import (
"fmt"
"os"

"github.com/fatih/color"
"github.com/spf13/cobra"

"github.com/tfversion/tfversion/pkg/alias"
"github.com/tfversion/tfversion/pkg/helpers"
)

const (
Expand All @@ -23,9 +20,8 @@ var (
Example: aliasExample,
Run: func(cmd *cobra.Command, args []string) {
if len(args) != 2 {
fmt.Println("error: provide an alias name and Terraform version")
fmt.Printf("See %s for help and examples\n", color.CyanString("`tfversion alias -h`"))
os.Exit(1)
err := helpers.ErrorWithHelp("tfversion alias -h")
helpers.ExitWithError("provide an alias name and Terraform version", err)
}
alias.AliasVersion(args[0], args[1])
},
Expand Down
18 changes: 7 additions & 11 deletions cmd/install.go
@@ -1,11 +1,10 @@
package cmd

import (
"fmt"
"os"

"github.com/fatih/color"
"github.com/spf13/cobra"
"github.com/tfversion/tfversion/pkg/helpers"
"github.com/tfversion/tfversion/pkg/install"
)

Expand Down Expand Up @@ -44,9 +43,8 @@ var (
// install latest
if latest {
if len(args) != 0 {
fmt.Println("error: `--latest` flag does not require specifying a Terraform version")
fmt.Printf("See %s for help and examples\n", color.CyanString("`tfversion install -h`"))
os.Exit(1)
err := helpers.ErrorWithHelp("tfversion install -h")
helpers.ExitWithError("`--latest` flag does not require specifying a Terraform version", err)
}
install.InstallLatestVersion(preRelease)
os.Exit(0)
Expand All @@ -55,19 +53,17 @@ var (
// installed required version
if required {
if len(args) != 0 {
fmt.Println("error: `--required` flag does not require specifying a Terraform version")
fmt.Printf("See %s for help and examples\n", color.CyanString("`tfversion install -h`"))
os.Exit(1)
err := helpers.ErrorWithHelp("tfversion install -h")
helpers.ExitWithError("`--required` flag does not require specifying a Terraform version", err)
}
install.InstallRequiredVersion()
os.Exit(0)
}

// install specific version
if len(args) != 1 {
fmt.Println("error: provide a Terraform version to install")
fmt.Printf("See %s for help and examples\n", color.CyanString("`tfversion install -h`"))
os.Exit(1)
err := helpers.ErrorWithHelp("tfversion install -h")
helpers.ExitWithError("provide a Terraform version to install", err)
}
install.InstallVersion(args[0])
},
Expand Down
7 changes: 1 addition & 6 deletions cmd/list.go
Expand Up @@ -3,7 +3,6 @@ package cmd
import (
"fmt"

"github.com/fatih/color"
"github.com/spf13/cobra"
"github.com/tfversion/tfversion/pkg/helpers"
"github.com/tfversion/tfversion/pkg/list"
Expand Down Expand Up @@ -47,11 +46,7 @@ var (

limit := min(maxResults, len(versions))
for _, version := range versions[:limit] {
if helpers.IsPreReleaseVersion(version) {
fmt.Println(color.YellowString(version))
} else {
fmt.Println(color.CyanString(version))
}
fmt.Println(helpers.ColoredVersion(version))
}
},
}
Expand Down
11 changes: 4 additions & 7 deletions cmd/root.go
@@ -1,12 +1,11 @@
package cmd

import (
"fmt"
"os"
"strings"

"github.com/fatih/color"
"github.com/spf13/cobra"
"github.com/tfversion/tfversion/pkg/helpers"
)

var (
Expand All @@ -15,12 +14,11 @@ var (
rootCmd = &cobra.Command{
Use: "tfversion",
Short: "tfversion - A simple tool to manage Terraform versions",
Version: version, // The version is set during the build by making using of `go build -ldflags`.
Version: version, // the version is set during the build by making using of `go build -ldflags`
Run: func(cmd *cobra.Command, args []string) {
err := cmd.Help()
if err != nil {
fmt.Println(err)
os.Exit(1)
helpers.ExitWithError("unable to display help", err)
}
},
}
Expand All @@ -45,7 +43,6 @@ func init() {

func Execute() {
if err := rootCmd.Execute(); err != nil {
fmt.Println(err)
os.Exit(1)
helpers.ExitWithError("unable to execute command", err)
}
}
10 changes: 3 additions & 7 deletions cmd/uninstall.go
@@ -1,11 +1,8 @@
package cmd

import (
"fmt"
"os"

"github.com/fatih/color"
"github.com/spf13/cobra"
"github.com/tfversion/tfversion/pkg/helpers"
"github.com/tfversion/tfversion/pkg/uninstall"
)

Expand All @@ -25,9 +22,8 @@ var (
Example: uninstallExample,
Run: func(cmd *cobra.Command, args []string) {
if len(args) != 1 {
fmt.Println("error: provide a Terraform version to uninstall")
fmt.Printf("See %s for help and examples\n", color.CyanString("`tfversion uninstall -h`"))
os.Exit(1)
err := helpers.ErrorWithHelp("tfversion uninstall -h")
helpers.ExitWithError("provide a Terraform version to uninstall", err)
}
uninstall.Uninstall(args[0])
},
Expand Down
18 changes: 7 additions & 11 deletions cmd/use.go
@@ -1,11 +1,10 @@
package cmd

import (
"fmt"
"os"

"github.com/fatih/color"
"github.com/spf13/cobra"
"github.com/tfversion/tfversion/pkg/helpers"
"github.com/tfversion/tfversion/pkg/use"
)

Expand Down Expand Up @@ -41,9 +40,8 @@ var (
// use latest version
if latest {
if len(args) != 0 {
fmt.Println("error: `--latest` flag does not require specifying a Terraform version")
fmt.Printf("See %s for help and examples\n", color.CyanString("`tfversion use -h`"))
os.Exit(1)
err := helpers.ErrorWithHelp("tfversion use -h")
helpers.ExitWithError("`--latest` flag does not require specifying a Terraform version", err)
}
use.UseLatestVersion(preRelease)
os.Exit(0)
Expand All @@ -52,19 +50,17 @@ var (
// use required version
if required {
if len(args) != 0 {
fmt.Println("error: `--required` flag does not require specifying a Terraform version")
fmt.Printf("See %s for help and examples\n", color.CyanString("`tfversion use -h`"))
os.Exit(1)
err := helpers.ErrorWithHelp("tfversion use -h")
helpers.ExitWithError("`--required` flag does not require specifying a Terraform version", err)
}
use.UseRequiredVersion()
os.Exit(0)
}

// use specific version
if len(args) != 1 {
fmt.Println("error: provide a Terraform version to activate")
fmt.Printf("See %s for help and examples\n", color.CyanString("`tfversion use -h`"))
os.Exit(1)
err := helpers.ErrorWithHelp("tfversion use -h")
helpers.ExitWithError("provide a Terraform version to activate", err)
}
use.UseVersion(args[0])
},
Expand Down
27 changes: 7 additions & 20 deletions pkg/alias/alias.go
Expand Up @@ -5,20 +5,15 @@ import (
"os"
"path/filepath"

"github.com/fatih/color"
"github.com/tfversion/tfversion/pkg/download"
"github.com/tfversion/tfversion/pkg/helpers"
)

// AliasVersion creates a symlink to the specified Terraform version.
func AliasVersion(alias string, version string) {
if !download.IsAlreadyDownloaded(version) {
if helpers.IsPreReleaseVersion(version) {
fmt.Printf("Terraform version %s not found, run %s to install\n", color.YellowString(version), color.CyanString(fmt.Sprintf("`tfversion install %s`", version)))
} else {
fmt.Printf("Terraform version %s not found, run %s to install\n", color.CyanString(version), color.CyanString(fmt.Sprintf("`tfversion install %s`", version)))
}
os.Exit(0)
err := fmt.Errorf("terraform version %s not found, run %s to install", helpers.ColoredVersion(version), helpers.ColoredInstallHelper(version))
helpers.ExitWithError("aliasing", err)
}

aliasLocation := GetAliasLocation()
Expand All @@ -29,40 +24,32 @@ func AliasVersion(alias string, version string) {
if err == nil {
err = os.RemoveAll(aliasPath)
if err != nil {
fmt.Printf("error removing symlink: %v\n", err)
os.Exit(1)
helpers.ExitWithError("error removing symlink", err)
}
}

// create the symlink
binaryVersionPath := download.GetInstallLocation(version)
err = os.Symlink(binaryVersionPath, aliasPath)
if err != nil {
fmt.Printf("error creating symlink: %v\n", err)
os.Exit(1)
helpers.ExitWithError("creating symlink", err)
}

if helpers.IsPreReleaseVersion(version) {
fmt.Printf("Aliased Terraform version %s as %s\n", color.YellowString(version), color.YellowString(alias))
} else {
fmt.Printf("Aliased Terraform version %s as %s\n", color.CyanString(version), color.CyanString(alias))
}
fmt.Printf("Aliased Terraform version %s as %s\n", helpers.ColoredVersion(version), helpers.ColoredVersion(alias))
}

// GetAliasLocation returns the directory where tfversion stores the aliases.
func GetAliasLocation() string {
user, err := os.UserHomeDir()
if err != nil {
fmt.Printf("error getting user home directory: %s", err)
os.Exit(1)
helpers.ExitWithError("error getting user home directory", err)
}

aliasLocation := filepath.Join(user, download.ApplicationDir, download.AliasesDir)
if _, err := os.Stat(aliasLocation); os.IsNotExist(err) {
err := os.Mkdir(aliasLocation, 0755)
if err != nil {
fmt.Printf("error creating alias directory: %s", err)
os.Exit(1)
helpers.ExitWithError("error creating alias directory", err)
}
}

Expand Down
20 changes: 6 additions & 14 deletions pkg/download/download.go
Expand Up @@ -8,7 +8,6 @@ import (
"path/filepath"
"time"

"github.com/fatih/color"
"github.com/tfversion/tfversion/pkg/helpers"
)

Expand All @@ -23,16 +22,14 @@ func IsAlreadyDownloaded(version string) bool {
func GetDownloadLocation() string {
user, err := os.UserHomeDir()
if err != nil {
fmt.Printf("error getting user home directory: %s", err)
os.Exit(1)
helpers.ExitWithError("error getting user home directory", err)
}

downloadLocation := filepath.Join(user, ApplicationDir, VersionsDir)
if _, err := os.Stat(downloadLocation); os.IsNotExist(err) {
err := os.Mkdir(downloadLocation, 0755)
if err != nil {
fmt.Printf("error creating download directory: %s", err)
os.Exit(1)
helpers.ExitWithError("error creating download directory", err)
}
}

Expand All @@ -53,26 +50,21 @@ func GetBinaryLocation(version string) string {
func Download(version, goos, goarch string) (string, error) {
downloadLocation := GetDownloadLocation()

// Construct the download URL based on the version and the OS and architecture.
// construct the download URL based on the version and the OS and architecture
downloadURL := fmt.Sprintf("%s/%s/terraform_%s_%s_%s.zip", TerraformReleasesUrl, version, version, goos, goarch)

var err error
for attempt := 1; attempt <= MaxRetries; attempt++ {
if err = downloadWithRetry(downloadURL, downloadLocation, version, goos, goarch); err == nil {
if helpers.IsPreReleaseVersion(version) {
fmt.Printf("Terraform version %s downloaded successfully\n", color.YellowString(version))
} else {
fmt.Printf("Terraform version %s downloaded successfully\n", color.CyanString(version))
}
// Return the path to the downloaded file.
fmt.Printf("Terraform version %s downloaded successfully\n", helpers.ColoredVersion(version))
return fmt.Sprintf("%s/terraform_%s_%s_%s.zip", downloadLocation, version, goos, goarch), nil
}

fmt.Printf("Attempt %d failed: %s\n", attempt, err)
time.Sleep(time.Second * RetryTimeInSeconds) // sleep before retrying.
time.Sleep(time.Second * RetryTimeInSeconds)
}

// If we got here, we failed to download Terraform after MaxRetries attempts.
// if we got here, we failed to download Terraform after MaxRetries attempts
return "", fmt.Errorf("failed to download Terraform after %d attempts: %s", MaxRetries, err)
}

Expand Down

0 comments on commit 88021a6

Please sign in to comment.