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

Show latest not working on .toml and README needs to be updated #252

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -85,24 +85,24 @@ tfswitch #will automatically switch to terraform version 0.14.4
3. Hit **Enter** to install.
### Install latest implicit version for stable releases
1. Install the latest implicit stable version.
2. Ex: `tfswitch -s 0.13` or `tfswitch --latest-stable 0.13` downloads 0.13.6 (latest) version.
2. Ex: `tfswitch -s 0.13` or `tfswitch --latest-stable 0.13` downloads <1.1.0, >=1.0.0, 1.0 downloads <2.0.0, >=1.0.0.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I had to re-read several times to make myself sure I get the things, hence the suggestion from regular user's standpoint:

Suggested change
2. Ex: `tfswitch -s 0.13` or `tfswitch --latest-stable 0.13` downloads <1.1.0, >=1.0.0, 1.0 downloads <2.0.0, >=1.0.0.
2. Ex: `tfswitch -s 0.13` or `tfswitch --latest-stable 0.13` downloads `>=1.0.0, <1.1.0`, and `tfswitch -s 1.0` or `tfswitch --latest-stable 1.0` downloads `>=1.0.0, <2.0.0`.

Also would it worth to mention the logic is the same as what pessimistic constraint operator does in TF version constraint?
https://www.terraform.io/language/expressions/version-constraints#-3

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will try to reword it

3. Hit **Enter** to install.
### Install latest implicit version for beta, alpha and release candidates(rc)
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.
2. Ex: `tfswitch -p 0.13` or `tfswitch --latest-pre 0.13` downloads 0.13.0-rc1.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a bit confusing that -p works differently than e.g. -s. Though this flag's audience assumably is quite small to rework the logic 🤔

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, -p only works for major.minor while -s works for major.minor.patch.

At that time, I do not know modify the regex to work with 0.0.X with X being 0-rc1 (alphanumeral+dash).

I am open to modifications, if anyone can make it better.

3. Hit **Enter** to install.
### 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.
3. Hit **Enter** to print.
### 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.
2. Ex: `tfswitch -S 0.13` or `tfswitch --show-latest-stable 0.13` prints 0.15.5
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm sure they won't release any more version lower than 1.0, but just to stay on safe side:

Suggested change
2. Ex: `tfswitch -S 0.13` or `tfswitch --show-latest-stable 0.13` prints 0.15.5
2. Ex: `tfswitch -S 0.13` or `tfswitch --show-latest-stable 0.13` prints 0.15.5 (latest version which falls under `>=1.0.0, <1.1.0` constraint as of June, 2022)

3. Hit **Enter** to print.
### 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.
2. Ex: `tfswitch -P 0.13` or `tfswitch --show-latest-pre 0.13` prints 0.13.0-rc1.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a bit confusing that -P works differently than e.g. -S. Though this flag's audience assumably is quite small to rework the logic 🤔

3. Hit **Enter** to print.
### 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
Expand Down
38 changes: 23 additions & 15 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,10 @@ func main() {
dir := lib.GetCurrentDirectory()
custBinPath := getopt.StringLong("bin", 'b', lib.ConvertExecutableExt(defaultBin), "Custom binary path. Ex: tfswitch -b "+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 based on a constraint. Ex: tfswitch --latest-stable 0.13.0 downloads 0.13.7 and 0.13 downloads 0.15.5 (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)")
latestPre := getopt.StringLong("latest-pre", 'p', defaultLatest, "Latest pre-release implicit version. Ex: tfswitch --latest-pre 0.13 downloads 0.13.0-rc1")
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it worth quoting the command here to provide visible separation between the command and the expected result?
e.g.

latestPre := getopt.StringLong("latest-pre", 'p', defaultLatest, "Latest pre-release implicit version. Ex: `tfswitch --latest-pre 0.13` - downloads 0.13.0-rc1")

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1
Though I'd suggest to use double quotes instead of backticks as a more common thing in descriptions I guess.

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")
latestStable := getopt.StringLong("latest-stable", 's', defaultLatest, "Latest implicit version based on a constraint. Ex: tfswitch --latest-stable 0.13.0 downloads 0.13.7 and 0.13 downloads 0.15.5")
showLatestStable := getopt.StringLong("show-latest-stable", 'S', defaultLatest, "Show latest implicit version. Ex: tfswitch --show-latest-stable 0.13 prints 0.15.5")
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 API other than the default. Default: "+defaultMirror)
Expand Down Expand Up @@ -102,21 +102,29 @@ func main() {

switch {
/* GIVEN A TOML FILE, */
/* show all terraform version including betas and RCs*/
/* show all terraform version including betas and RCs */
case *listAllFlag:
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm trying to work out why this case switch logic is duplicated for the TOML file.
Can the TOML file detection not be done before the switch case and just update the variables for version/binPath and then run the single case switch?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Though, I'm happy to take a look at trying to simplify this outside of this PR, if you agree?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, just realised the PR was started 2 years ago 😅

listAll := true //set list all true - all versions including beta and rc will be displayed
installOption(listAll, &binPath, mirrorURL)
/* latest pre-release implicit version. Ex: tfswitch --latest-pre 0.13 downloads 0.13.0-rc1 (latest) */
/* latest pre-release implicit version. Ex: tfswitch --latest-pre 0.13 downloads 0.13.0-rc1 */
case *latestPre != "":
preRelease := true
installLatestImplicitVersion(*latestPre, custBinPath, mirrorURL, preRelease)
/* latest implicit version. Ex: tfswitch --latest 0.13 downloads 0.13.5 (latest) */
installLatestImplicitVersion(*latestPre, &binPath, mirrorURL, preRelease)
/* show latest pre-release implicit version. Ex: tfswitch --show-latest-pre 0.13 prints 0.13.0-rc1 */
case *showLatestPre != "":
preRelease := true
showLatestImplicitVersion(*showLatestPre, &binPath, mirrorURL, preRelease)
/* latest implicit version. Ex: tfswitch --latest-stable 1.0.0 downloads <1.1.0, >=1.0.0, 1.0 downloads <2.0.0, >=1.0.0" */
case *latestStable != "":
preRelease := false
installLatestImplicitVersion(*latestStable, custBinPath, mirrorURL, preRelease)
installLatestImplicitVersion(*latestStable, &binPath, mirrorURL, preRelease)
/* show latest implicit stable version. Ex: tfswitch --latest 0.13 prints 0.15.5 */
case *showLatestStable != "":
preRelease := false
showLatestImplicitVersion(*showLatestStable, &binPath, mirrorURL, preRelease)
/* latest stable version */
case *latestFlag:
installLatestVersion(custBinPath, mirrorURL)
installLatestVersion(&binPath, mirrorURL)
/* version provided on command line as arg */
case len(args) == 1:
installVersion(args[0], &binPath, mirrorURL)
Expand All @@ -137,7 +145,7 @@ func main() {
case checkTFEnvExist() && len(args) == 0 && version == "":
tfversion := os.Getenv("TF_VERSION")
fmt.Printf("Terraform version environment variable: %s\n", tfversion)
installVersion(tfversion, custBinPath, mirrorURL)
installVersion(tfversion, &binPath, mirrorURL)
/* if terragrunt.hcl file found (IN ADDITION TO A TOML FILE) */
case fileExists(TGHACLFile) && checkVersionDefinedHCL(&TGHACLFile) && len(args) == 0:
installTGHclFile(&TGHACLFile, &binPath, mirrorURL)
Expand All @@ -153,22 +161,22 @@ func main() {
case *listAllFlag:
installWithListAll(custBinPath, mirrorURL)

/* latest pre-release implicit version. Ex: tfswitch --latest-pre 0.13 downloads 0.13.0-rc1 (latest) */
/* latest pre-release implicit version. Ex: tfswitch --latest-pre 0.13 downloads 0.13.0-rc1 */
case *latestPre != "":
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) */
/* show latest pre-release implicit version. Ex: tfswitch --show-latest-pre 0.13 prints 0.13.0-rc1 */
case *showLatestPre != "":
preRelease := true
showLatestImplicitVersion(*showLatestPre, custBinPath, mirrorURL, preRelease)

/* latest implicit version. Ex: tfswitch --latest 0.13 downloads 0.13.5 (latest) */
/* latest implicit version. Ex: tfswitch --latest-stable 1.0.0 downloads <1.1.0, >=1.0.0, 1.0 downloads <2.0.0, >=1.0.0 */
case *latestStable != "":
preRelease := false
installLatestImplicitVersion(*latestStable, custBinPath, mirrorURL, preRelease)

/* show latest implicit stable version. Ex: tfswitch --latest 0.13 downloads 0.13.5 (latest) */
/* show latest implicit stable version. Ex: tfswitch --latest 0.13 prints 0.15.5 */
case *showLatestStable != "":
preRelease := false
showLatestImplicitVersion(*showLatestStable, custBinPath, mirrorURL, preRelease)
Expand Down
14 changes: 7 additions & 7 deletions www/docs/Quick-Start.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,24 +35,24 @@ tfswitch #will automatically switch to terraform version 0.14.4
3. Hit **Enter** to install.
### Install latest implicit version for stable releases
1. Install the latest implicit stable version.
2. Ex: `tfswitch -s 0.13` or `tfswitch --latest-stable 0.13` downloads 0.13.6 (latest) version.
2. Ex: `tfswitch -s 0.13` or `tfswitch --latest-stable 0.13` downloads <1.1.0, >=1.0.0, 1.0 downloads <2.0.0, >=1.0.0.
3. Hit **Enter** to install.
### Install latest implicit version for beta, alpha and release candidates(rc)
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.
2. Ex: `tfswitch -p 0.13` or `tfswitch --latest-pre 0.13` downloads 0.13.0-rc1.
3. Hit **Enter** to install.
### 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.
3. Hit **Enter** to print.
### 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.
2. Ex: `tfswitch -S 0.13` or `tfswitch --show-latest-stable 0.13` prints 0.15.5.
3. Hit **Enter** to print.
### 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.
2. Ex: `tfswitch -P 0.13` or `tfswitch --show-latest-pre 0.13` prints 0.13.0-rc1.
3. Hit **Enter** to print.
### 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:
```
Expand Down