Skip to content

Commit

Permalink
Add helper function for printing an error with help command
Browse files Browse the repository at this point in the history
  • Loading branch information
ChrisTerBeke committed Mar 5, 2024
1 parent 4949dd3 commit cd73fb7
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 18 deletions.
5 changes: 1 addition & 4 deletions cmd/alias.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
package cmd

import (
"fmt"

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

"github.com/tfversion/tfversion/pkg/alias"
Expand All @@ -23,7 +20,7 @@ var (
Example: aliasExample,
Run: func(cmd *cobra.Command, args []string) {
if len(args) != 2 {
err := fmt.Errorf("see %s for help and examples", color.CyanString("`tfversion alias -h`"))
err := helpers.ErorWithHelp("tfversion alias -h")
helpers.ExitWithError("provide an alias name and Terraform version", err)
}
alias.AliasVersion(args[0], args[1])
Expand Down
8 changes: 3 additions & 5 deletions cmd/install.go
Original file line number Diff line number Diff line change
@@ -1,10 +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/install"
Expand Down Expand Up @@ -45,7 +43,7 @@ var (
// install latest
if latest {
if len(args) != 0 {
err := fmt.Errorf("see %s for help and examples", color.CyanString("`tfversion install -h`"))
err := helpers.ErorWithHelp("tfversion install -h")
helpers.ExitWithError("`--latest` flag does not require specifying a Terraform version", err)
}
install.InstallLatestVersion(preRelease)
Expand All @@ -55,7 +53,7 @@ var (
// installed required version
if required {
if len(args) != 0 {
err := fmt.Errorf("see %s for help and examples", color.CyanString("`tfversion install -h`"))
err := helpers.ErorWithHelp("tfversion install -h")
helpers.ExitWithError("`--required` flag does not require specifying a Terraform version", err)
}
install.InstallRequiredVersion()
Expand All @@ -64,7 +62,7 @@ var (

// install specific version
if len(args) != 1 {
err := fmt.Errorf("see %s for help and examples", color.CyanString("`tfversion install -h`"))
err := helpers.ErorWithHelp("tfversion install -h")
helpers.ExitWithError("provide a Terraform version to install", err)
}
install.InstallVersion(args[0])
Expand Down
5 changes: 1 addition & 4 deletions cmd/uninstall.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
package cmd

import (
"fmt"

"github.com/fatih/color"
"github.com/spf13/cobra"
"github.com/tfversion/tfversion/pkg/helpers"
"github.com/tfversion/tfversion/pkg/uninstall"
Expand All @@ -25,7 +22,7 @@ var (
Example: uninstallExample,
Run: func(cmd *cobra.Command, args []string) {
if len(args) != 1 {
err := fmt.Errorf("see %s for help and examples", color.CyanString("`tfversion uninstall -h`"))
err := helpers.ErorWithHelp("tfversion uninstall -h")
helpers.ExitWithError("provide a Terraform version to uninstall", err)
}
uninstall.Uninstall(args[0])
Expand Down
8 changes: 3 additions & 5 deletions cmd/use.go
Original file line number Diff line number Diff line change
@@ -1,10 +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/use"
Expand Down Expand Up @@ -42,7 +40,7 @@ var (
// use latest version
if latest {
if len(args) != 0 {
err := fmt.Errorf("see %s for help and examples", color.CyanString("`tfversion use -h`"))
err := helpers.ErorWithHelp("tfversion use -h")
helpers.ExitWithError("`--latest` flag does not require specifying a Terraform version", err)
}
use.UseLatestVersion(preRelease)
Expand All @@ -52,7 +50,7 @@ var (
// use required version
if required {
if len(args) != 0 {
err := fmt.Errorf("see %s for help and examples", color.CyanString("`tfversion use -h`"))
err := helpers.ErorWithHelp("tfversion use -h")
helpers.ExitWithError("`--required` flag does not require specifying a Terraform version", err)
}
use.UseRequiredVersion()
Expand All @@ -61,7 +59,7 @@ var (

// use specific version
if len(args) != 1 {
err := fmt.Errorf("see %s for help and examples", color.CyanString("`tfversion use -h`"))
err := helpers.ErorWithHelp("tfversion use -h")
helpers.ExitWithError("provide a Terraform version to activate", err)
}
use.UseVersion(args[0])
Expand Down
5 changes: 5 additions & 0 deletions pkg/helpers/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,11 @@ func ExitWithError(message string, err error) {
os.Exit(1)
}

// ErorWithHelp returns an error with a help message
func ErorWithHelp(help string) error {
return fmt.Errorf("see %s for help and examples", color.CyanString("`%s`", help))
}

// IsPreReleaseVersion checks if the given version is a Terraform pre-release version
func IsPreReleaseVersion(version string) bool {
return strings.Contains(version, "-alpha") || strings.Contains(version, "-beta") || strings.Contains(version, "-rc")
Expand Down

0 comments on commit cd73fb7

Please sign in to comment.