Skip to content

Commit

Permalink
Auto-install a version when it is missing (#29)
Browse files Browse the repository at this point in the history
  • Loading branch information
ChrisTerBeke committed Mar 8, 2024
1 parent 3df93ca commit bfa849f
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 14 deletions.
7 changes: 7 additions & 0 deletions README.md
Expand Up @@ -78,6 +78,13 @@ tfversion use --latest --pre-release
tfversion use --required
```

### Automatically install a version when using

```sh
tfversion use 1.7.4 --install
tfversion use --latest --install
```

### Create and use an alias

```sh
Expand Down
16 changes: 11 additions & 5 deletions cmd/use.go
Expand Up @@ -22,11 +22,16 @@ const (
"\n" +
"\n" +
"# Use the required Terraform version for your current directory\n" +
"tfversion use --required"
"tfversion use --required\n" +
"\n" +
"\n" +
"# Automatically install the target version if it is missing\n" +
"tfversion use 1.7.3 --install"
)

var (
useCmd = &cobra.Command{
autoInstall bool
useCmd = &cobra.Command{
Use: "use",
Short: "Activates a given Terraform version",
Example: useExample,
Expand All @@ -43,7 +48,7 @@ var (
err := helpers.ErrorWithHelp("tfversion use -h")
helpers.ExitWithError("`--latest` flag does not require specifying a Terraform version", err)
}
use.UseLatestVersion(preRelease)
use.UseLatestVersion(preRelease, autoInstall)
os.Exit(0)
}

Expand All @@ -53,7 +58,7 @@ var (
err := helpers.ErrorWithHelp("tfversion use -h")
helpers.ExitWithError("`--required` flag does not require specifying a Terraform version", err)
}
use.UseRequiredVersion()
use.UseRequiredVersion(autoInstall)
os.Exit(0)
}

Expand All @@ -62,7 +67,7 @@ var (
err := helpers.ErrorWithHelp("tfversion use -h")
helpers.ExitWithError("provide a Terraform version to activate", err)
}
use.UseVersion(args[0])
use.UseVersion(args[0], autoInstall)
},
}
)
Expand All @@ -72,4 +77,5 @@ func init() {
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")
useCmd.Flags().BoolVar(&required, "required", false, "use the required Terraform version for your current directory")
useCmd.Flags().BoolVar(&autoInstall, "install", false, "automatically install if the target version is missing")
}
21 changes: 12 additions & 9 deletions pkg/use/use.go
Expand Up @@ -10,11 +10,12 @@ import (
"github.com/tfversion/tfversion/pkg/alias"
"github.com/tfversion/tfversion/pkg/download"
"github.com/tfversion/tfversion/pkg/helpers"
"github.com/tfversion/tfversion/pkg/install"
"github.com/tfversion/tfversion/pkg/list"
)

// UseVersion activates the specified Terraform version or one of the latest versions
func UseVersion(versionOrAlias string) {
func UseVersion(versionOrAlias string, autoInstall bool) {

// find the version (via alias or directly)
var version string
Expand All @@ -26,14 +27,16 @@ func UseVersion(versionOrAlias string) {

// check if the version is installed
if !download.IsAlreadyDownloaded(version) {
err := fmt.Errorf("terraform version %s not found, run %s to install", helpers.ColoredVersion(version), helpers.ColoredInstallHelper(version))
helpers.ExitWithError("using", err)
if !autoInstall {
err := fmt.Errorf("terraform version %s not found, run %s to install", helpers.ColoredVersion(version), helpers.ColoredInstallHelper(version))
helpers.ExitWithError("using", err)
}
install.InstallVersion(version)
}

useLocation := GetUseLocation()

// inform the user that they need to update their PATH
path := os.Getenv("PATH")
useLocation := GetUseLocation()
if !strings.Contains(path, useLocation) {
fmt.Printf("%s not found in your shell PATH\n", color.CyanString(useLocation))
fmt.Printf("Please run %s to make this version available in your shell\n", color.CyanString("`export PATH=%s:$PATH`", useLocation))
Expand Down Expand Up @@ -62,13 +65,13 @@ func UseVersion(versionOrAlias string) {
}

// UseLatestVersion activates the latest Terraform version
func UseLatestVersion(preRelease bool) {
func UseLatestVersion(preRelease bool, autoInstall bool) {
version := list.FindLatestVersion(preRelease)
UseVersion(version)
UseVersion(version, autoInstall)
}

// UseRequiredVersion activates the required Terraform version from the .tf files in the current directory
func UseRequiredVersion() {
func UseRequiredVersion(autoInstall bool) {
terraformFiles, err := helpers.FindTerraformFiles()
if err != nil {
helpers.ExitWithError("finding Terraform files", err)
Expand All @@ -93,7 +96,7 @@ func UseRequiredVersion() {
helpers.ExitWithError("installing required version", err)
}

UseVersion(foundVersion)
UseVersion(foundVersion, autoInstall)
}

func GetUseLocation() string {
Expand Down

0 comments on commit bfa849f

Please sign in to comment.