Skip to content

Commit

Permalink
Add "show" options to just show the latest versions rather than actually
Browse files Browse the repository at this point in the history
switch or install.

  These options can be useful for automations that just need to check
  what the latest version is before proceeding.

  Also added to the README and cleaned up some typos and whitespace there a bit.
  • Loading branch information
grimm26 committed Aug 27, 2021
1 parent b9e4693 commit c730f27
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 8 deletions.
28 changes: 20 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,12 +74,12 @@ The most recently selected versions are presented at the top of the dropdown.
### See all versions including beta, alpha and release candidates(rc)
<img src="https://s3.us-east-2.amazonaws.com/kepler-images/warrensbox/tfswitch/tfswitch-v5.gif#1" alt="drawing" style="width: 370px;"/>

1. Display all versions including beta, alpha and release candidates(rc).
1. Display all versions including beta, alpha and release candidates(rc).
2. For example, `tfswitch -l` or `tfswitch --list-all` to see all versions.
3. Hit **Enter** to select the desired version.

### Use environment variable
You can also set the `TF_VERSION` environment variable to your desired terraform version.
You can also set the `TF_VERSION` environment variable to your desired terraform version.
For example:
```bash
export TF_VERSION=0.14.4
Expand All @@ -97,8 +97,20 @@ tfswitch #will automatically switch to terraform version 0.14.4
1. Install the latest implicit pre-release version.
2. Ex: `tfswitch -p 0.13` or `tfswitch --latest-pre 0.13` downloads 0.13.0-rc1 (latest) version.
3. Hit **Enter** to install.
### Use version.tf file
If a .tf file with the terraform constrain is included in the current directory, it should automatically download or switch to that terraform version. For example, the following should automatically switch terraform to the lastest version:
### Show latest version only
1. Just show what the latest version is.
2. Run `tfswitch -U` or `tfswitch --show-latest`
3. Hit **Enter** to show.
### Show latest implicit version for stable releases
1. Show the latest implicit stable version.
2. Ex: `tfswitch -S 0.13` or `tfswitch --show-latest-stable 0.13` shows 0.13.6 (latest) version.
3. Hit **Enter** to show.
### Show latest implicit version for beta, alpha and release candidates(rc)
1. Show the latest implicit pre-release version.
2. Ex: `tfswitch -P 0.13` or `tfswitch --show-latest-pre 0.13` shows 0.13.0-rc1 (latest) version.
3. Hit **Enter** to show.
### Use version.tf file
If a .tf file with the terraform constrain is included in the current directory, it should automatically download or switch to that terraform version. For example, the following should automatically switch terraform to the latest version:
```ruby
terraform {
required_version = ">= 0.12.9"
Expand All @@ -114,7 +126,7 @@ terraform {
### Use .tfswitch.toml file (For non-admin - users with limited privilege on their computers)
This is similiar to using a .tfswitchrc file, but you can specify a custom binary path for your terraform installation

<img src="https://s3.us-east-2.amazonaws.com/kepler-images/warrensbox/tfswitch/tfswitch-v7.gif#1" alt="drawing" style="width: 370px;"/>
<img src="https://s3.us-east-2.amazonaws.com/kepler-images/warrensbox/tfswitch/tfswitch-v7.gif#1" alt="drawing" style="width: 370px;"/>

<img src="https://s3.us-east-2.amazonaws.com/kepler-images/warrensbox/tfswitch/tfswitch-v8.gif#1" alt="drawing" style="width: 370px;"/>

Expand Down Expand Up @@ -146,15 +158,15 @@ bin = "C:\\Users\\<%USRNAME%>\\bin\\terraform.exe"
#### *Instead of a `.tfswitchrc` file, a `.terraform-version` file may be used for compatibility with [`tfenv`](https://github.com/tfutils/tfenv#terraform-version-file) and other tools which use it*

### Use terragrunt.hcl file
If a terragrunt.hcl file with the terraform constrain is included in the current directory, it should automatically download or switch to that terraform version. For example, the following should automatically switch terraform to the lastest version 0.13:
If a terragrunt.hcl file with the terraform constrain is included in the current directory, it should automatically download or switch to that terraform version. For example, the following should automatically switch terraform to the latest version 0.13:
```ruby
terragrunt_version_constraint = ">= 0.26, < 0.27"
terraform_version_constraint = ">= 0.13, < 0.14"
...
```

### Use custom mirror
To install from a remote mirror other than the default(https://releases.hashicorp.com/terraform). Use the `-m` or `--mirror` parameter.
### Use custom mirror
To install from a remote mirror other than the default(https://releases.hashicorp.com/terraform). Use the `-m` or `--mirror` parameter.
Ex: `tfswitch --mirror https://example.jfrog.io/artifactory/hashicorp`

## Automation
Expand Down
38 changes: 38 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,11 @@ func main() {
custBinPath := getopt.StringLong("bin", 'b', lib.ConvertExecutableExt(defaultBin), "Custom binary path. Ex: "+lib.ConvertExecutableExt("/Users/username/bin/terraform"))
listAllFlag := getopt.BoolLong("list-all", 'l', "List all versions of terraform - including beta and rc")
latestPre := getopt.StringLong("latest-pre", 'p', defaultLatest, "Latest pre-release implicit version. Ex: tfswitch --latest-pre 0.13 downloads 0.13.0-rc1 (latest)")
showLatestPre := getopt.StringLong("show-latest-pre", 'P', defaultLatest, "Show latest pre-release implicit version. Ex: tfswitch --show-latest-pre 0.13 prints 0.13.0-rc1 (latest)")
latestStable := getopt.StringLong("latest-stable", 's', defaultLatest, "Latest implicit version. Ex: tfswitch --latest-stable 0.13 downloads 0.13.7 (latest)")
showLatestStable := getopt.StringLong("show-latest-stable", 'S', defaultLatest, "Show latest implicit version. Ex: tfswitch --show-latest-stable 0.13 prints 0.13.7 (latest)")
latestFlag := getopt.BoolLong("latest", 'u', "Get latest stable version")
showLatestFlag := getopt.BoolLong("show-latest", 'U', "Show latest stable version")
mirrorURL := getopt.StringLong("mirror", 'm', defaultMirror, "Install from a remote other than the default. Default: https://releases.hashicorp.com/terraform")
versionFlag := getopt.BoolLong("version", 'v', "Displays the version of tfswitch")
helpFlag := getopt.BoolLong("help", 'h', "Displays help message")
Expand Down Expand Up @@ -168,15 +171,29 @@ func main() {
preRelease := true
installLatestImplicitVersion(*latestPre, custBinPath, mirrorURL, preRelease)

/* show latest pre-release implicit version. Ex: tfswitch --latest-pre 0.13 downloads 0.13.0-rc1 (latest) */
case *showLatestPre != "":
preRelease := true
showLatestImplicitVersion(*showLatestPre, custBinPath, mirrorURL, preRelease)

/* latest implicit version. Ex: tfswitch --latest 0.13 downloads 0.13.5 (latest) */
case *latestStable != "":
preRelease := false
installLatestImplicitVersion(*latestStable, custBinPath, mirrorURL, preRelease)

/* show latest implicit stable version. Ex: tfswitch --latest 0.13 downloads 0.13.5 (latest) */
case *showLatestStable != "":
preRelease := false
showLatestImplicitVersion(*showLatestStable, custBinPath, mirrorURL, preRelease)

/* latest stable version */
case *latestFlag:
installLatestVersion(custBinPath, mirrorURL)

/* show latest stable version */
case *showLatestFlag:
showLatestVersion(custBinPath, mirrorURL)

/* version provided on command line as arg */
case len(args) == 1:
installVersion(args[0], custBinPath, mirrorURL)
Expand Down Expand Up @@ -228,6 +245,12 @@ func installLatestVersion(custBinPath, mirrorURL *string) {
lib.Install(tfversion, *custBinPath, *mirrorURL)
}

// show install latest stable tf version
func showLatestVersion(custBinPath, mirrorURL *string) {
tfversion, _ := lib.GetTFLatest(*mirrorURL)
fmt.Printf("%s\n", tfversion)
}

// install latest - argument (version) must be provided
func installLatestImplicitVersion(requestedVersion string, custBinPath, mirrorURL *string, preRelease bool) {
if lib.ValidMinorVersionFormat(requestedVersion) {
Expand All @@ -238,6 +261,21 @@ func installLatestImplicitVersion(requestedVersion string, custBinPath, mirrorUR
}
}

// show latest - argument (version) must be provided
func showLatestImplicitVersion(requestedVersion string, custBinPath, mirrorURL *string, preRelease bool) {
if lib.ValidMinorVersionFormat(requestedVersion) {
tfversion, _ := lib.GetTFLatestImplicit(*mirrorURL, preRelease, requestedVersion)
if len(tfversion) > 0 {
fmt.Printf("%s\n", tfversion)
} else {
fmt.Println("The provided terraform version does not exist. Try `tfswitch -l` to see all available versions.")
os.Exit(1)
}
} else {
printInvalidMinorTFVersion()
}
}

// install with provided version as argument
func installVersion(arg string, custBinPath *string, mirrorURL *string) {
if lib.ValidVersionFormat(arg) {
Expand Down

0 comments on commit c730f27

Please sign in to comment.