Skip to content

Commit

Permalink
feat: add the ability to use the latest or latest prerelease version …
Browse files Browse the repository at this point in the history
…of Terraform installed
  • Loading branch information
bschaatsbergen committed Feb 24, 2024
1 parent 41542b6 commit 4cb071b
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 8 deletions.
38 changes: 31 additions & 7 deletions cmd/use.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,26 +9,50 @@ import (
)

const (
useExample = "# Activate a specific Terraform version\n" +
"tfversion use 1.7.3"
useExample = "# Use a specific Terraform version\n" +
"tfversion use 1.7.3" +
"\n" +
"\n" +
"# Use the latest stable Terraform version\n" +
"tfversion use --latest" +
"\n" +
"\n" +
"# Use the latest pre-release Terraform version\n" +
"tfversion use --latest --pre-release"
)

var (
useCmd = &cobra.Command{
Use: "use",
Short: "Activates a given Terraform version",
Example: useExample,
PreRun: func(cmd *cobra.Command, args []string) {
if preRelease && !latest {
cmd.MarkFlagRequired("latest")
}
},
Run: func(cmd *cobra.Command, args []string) {
if len(args) != 1 {
fmt.Println("error: provide a Terraform version to activate")
fmt.Println("See 'tfversion use -h' for help and examples")
os.Exit(1)
if latest {
if len(args) != 0 {
fmt.Println("error: 'latest' flag does not require specifying a Terraform version")
fmt.Println("See 'tfversion use -h' for help and examples")
os.Exit(1)
}
use.UseVersion("", latest, preRelease)
} else {
if len(args) != 1 {
fmt.Println("error: provide a Terraform version to activate")
fmt.Println("See 'tfversion use -h' for help and examples")
os.Exit(1)
}
use.UseVersion(args[0], latest, preRelease)
}
use.UseVersion(args[0])
},
}
)

func init() {
rootCmd.AddCommand(useCmd)
useCmd.Flags().BoolVar(&latest, "latest", false, "use the latest stable Terraform version")
useCmd.Flags().BoolVar(&preRelease, "pre-release", false, "When used with --latest, use the latest pre-release version")
}
19 changes: 18 additions & 1 deletion pkg/use/use.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,26 @@ import (
"strings"

"github.com/tfversion/tfversion/pkg/download"
"github.com/tfversion/tfversion/pkg/list"
)

func UseVersion(version string) {
// UseVersion activates the specified Terraform version or one of the latest versions
func UseVersion(version string, latest bool, preRelease bool) {
// Get the available Terraform versions
versions := list.GetAvailableVersions()

// Set the version to the latest stable version if the `latest` flag is set
// or to the latest pre-release version if the `latest` and `pre-release` flags are set
if latest {
for _, v := range versions {
if !preRelease && list.IsPreReleaseVersion(v) {
continue
}
version = v
break
}
}

if !download.IsAlreadyDownloaded(version) {
fmt.Printf("Terraform version %s is not installed\n", version)
os.Exit(0)
Expand Down

0 comments on commit 4cb071b

Please sign in to comment.