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

Improve printing and errors #15

Merged
merged 21 commits into from Mar 5, 2024
Merged
Show file tree
Hide file tree
Changes from 19 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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.ErorWithHelp("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.ErorWithHelp("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.ErorWithHelp("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.ErorWithHelp("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.ErorWithHelp("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.ErorWithHelp("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.ErorWithHelp("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.ErorWithHelp("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