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

Auto-install a version when it is missing #29

Merged
merged 3 commits into from Mar 8, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
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 @@
"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 @@

// 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()

Check failure on line 39 in pkg/use/use.go

View workflow job for this annotation

GitHub Actions / build

undefined: 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 @@
}

// 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 @@
helpers.ExitWithError("installing required version", err)
}

UseVersion(foundVersion)
UseVersion(foundVersion, autoInstall)
}

func GetUseLocation() string {
Expand Down