Skip to content

Commit

Permalink
Fix docs and help strings
Browse files Browse the repository at this point in the history
  • Loading branch information
ArronaxKP committed Apr 22, 2024
1 parent 68a9e50 commit b460ffc
Show file tree
Hide file tree
Showing 7 changed files with 29 additions and 15 deletions.
2 changes: 1 addition & 1 deletion lib/defaults.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ const (
DefaultMirror = "https://releases.hashicorp.com/terraform"
DefaultLatest = ""
installFile = "terraform"
installDir = ".terraform.versions"
InstallDir = ".terraform.versions"
recentFile = "RECENT"
tfDarwinArm64StartVersion = "1.0.2"
VersionPrefix = "terraform_"
Expand Down
2 changes: 1 addition & 1 deletion lib/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ func initialize(binPath string) {
// will create the installDir if it does not exist
func getInstallLocation(installPath string) string {
/* set installation location */
installLocation = filepath.Join(installPath, installDir)
installLocation = filepath.Join(installPath, InstallDir)

/* Create local installation directory if it does not exist */
createDirIfNotExist(installLocation)
Expand Down
2 changes: 1 addition & 1 deletion lib/param_parsing/parameters.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func GetParameters() Params {
getopt.StringVarLong(&params.CustomBinaryPath, "bin", 'b', "Custom binary path. Ex: tfswitch -b "+lib.ConvertExecutableExt("/Users/username/bin/terraform"))
getopt.StringVarLong(&params.DefaultVersion, "default", 'd', "Default to this version in case no other versions could be detected. Ex: tfswitch --default 1.2.4")
getopt.BoolVarLong(&params.HelpFlag, "help", 'h', "Displays help message")
getopt.StringVarLong(&params.InstallPath, "install", 'i', "Custom install path. Ex: tfswitch -b "+lib.ConvertExecutableExt("/Users/username"))
getopt.StringVarLong(&params.InstallPath, "install", 'i', "Custom install path. Ex: tfswitch -i /Users/username. The binaries will be in the sub installDir directory e.g. /Users/username/"+lib.InstallDir)
getopt.BoolVarLong(&params.LatestFlag, "latest", 'u', "Get latest stable version")
getopt.StringVarLong(&params.LatestPre, "latest-pre", 'p', "Latest pre-release implicit version. Ex: tfswitch --latest-pre 0.13 downloads 0.13.0-rc1 (latest)")
getopt.StringVarLong(&params.LatestStable, "latest-stable", 's', "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)")
Expand Down
4 changes: 4 additions & 0 deletions www/docs/usage/ci-cd.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
## Set a default TF version for CI/CD pipeline

1. When using a CI/CD pipeline, you may want a default or fallback version to avoid the pipeline from hanging.
2. Ex: `tfswitch -d 1.2.3` or `tfswitch --default 1.2.3` installs version `1.2.3` when no other versions could be detected.
[Also, see CICD example](../continuous-integration)
Expand Down Expand Up @@ -34,12 +35,14 @@ load-tfswitch() {
add-zsh-hook chpwd load-tfswitch
load-tfswitch
```

> NOTE: if you see an error like this: `command not found: add-zsh-hook`, then you might be on an older version of zsh (see below), or you simply need to load `add-zsh-hook` by adding this to your `.zshrc`:
> ```
> autoload -U add-zsh-hook
> ```
### Older version of zsh

```sh
cd(){
builtin cd "$@";
Expand All @@ -49,6 +52,7 @@ cd(){
fi
}
```

## Automatically switch with fish shell

Add the following to the end of your `~/.config/fish/config.fish` file:
Expand Down
5 changes: 2 additions & 3 deletions www/docs/usage/commandline.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,7 @@ The most recently selected versions are presented at the top of the dropdown.

## Use environment variables

You can also set the `TF_VERSION` environment variable to your desired terraform version.
For example:
You can also set the `TF_VERSION` environment variable to your desired terraform version. For example:

```bash
export TF_VERSION=0.14.4
Expand Down Expand Up @@ -77,7 +76,7 @@ tfswitch --mirror https://example.jfrog.io/artifactory/hashicorp`

## Install to non-default location

By default `tfswitch` will download the Terraform binary to the user home directory under this path: `/Users/warrenveerasingam/.terraform.versions`
By default `tfswitch` will download the Terraform binary to the user home directory under this path: `$HOME/.terraform.versions`

If you want to install the binaries outside of the home directory then you can provide the `-i` or `--install` to install Terraform binaries to a non-standard path. Useful if you want to install versions of Terraform that can be shared with multiple users.

Expand Down
27 changes: 19 additions & 8 deletions www/docs/usage/config-files.md
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
## Get the version from a subdirectory
While using the file configuration it might be necessary to change the working directory. You can do that with the `--chdir` or `-c` parameter.

While using the file configuration it might be necessary to change the working directory. You can do that with the `--chdir` or `-c` parameter.

```bash
tfswitch --chdir terraform_dir
tfswitch -c terraform_dir
```

## Use version.tf file

If a .tf file with the terraform constraints 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:

```
terraform {
required_version = ">= 0.12.9"
Expand All @@ -22,6 +26,7 @@ terraform {
![versiontf](../static/versiontf.gif "Use version.tf")

## Use .tfswitchrc file

![tfswitchrc](../static/tfswitch-v6.gif)

1. Create a `.tfswitchrc` file containing the desired version
Expand All @@ -31,23 +36,27 @@ terraform {
*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 .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

![toml1](../static/tfswitch-v7.gif)
![toml2](../static/tfswitch-v8.gif)

1. Create a custom binary path. Ex: `mkdir /Users/warrenveerasingam/bin` (replace warrenveerasingam with your username)
2. Add the path to your PATH. Ex: `export PATH=$PATH:/Users/warrenveerasingam/bin` (add this to your bash profile or zsh profile)
3. Pass -b or --bin parameter with your custom path to install terraform. Ex: `tfswitch -b /Users/warrenveerasingam/bin/terraform 0.10.8 `
1. Create a custom binary path. Ex: `mkdir $HOME/bin`
2. Add the path to your PATH. Ex: `export PATH=$PATH:$HOME/bin` (add this to your bash profile or zsh profile)
3. Pass -b or --bin parameter with your custom path to install terraform. Ex: `tfswitch -b $HOME/bin/terraform 0.10.8 `
4. Optionally, you can create a `.tfswitch.toml` file in your terraform directory(current directory) OR in your home directory(~/.tfswitch.toml). The toml file in the current directory has a higher precedence than toml file in the home directory
5. Your `.tfswitch.toml` file should look like this:

```ruby
bin = "/Users/warrenveerasingam/bin/terraform"
bin = "$HOME/bin/terraform"
version = "0.11.3"
```
4. Run `tfswitch` and it should automatically install the required terraform version in the specified binary path

**NOTE**
6. Run `tfswitch` and it should automatically install the required terraform version in the specified binary path

**NOTE**

1. For linux users that do not have write permission to `/usr/local/bin/`, `tfswitch` will attempt to install terraform at `$HOME/bin`. Run `export PATH=$PATH:$HOME/bin` to append bin to PATH
2. For windows host, `tfswitch` need to be run under `Administrator` mode, and `$HOME/.tfswitch.toml` with `bin` must be defined (with a valid path) as minimum, below is an example for `$HOME/.tfswitch.toml` on windows

Expand All @@ -56,8 +65,10 @@ bin = "C:\\Users\\<%USRNAME%>\\bin\\terraform.exe"
```

## Use terragrunt.hcl file

If a terragrunt.hcl file with the terraform constraint 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:
For example, the following should automatically switch terraform to the lastest version 0.13:

```ruby
terragrunt_version_constraint = ">= 0.26, < 0.27"
terraform_version_constraint = ">= 0.13, < 0.14"
Expand Down
2 changes: 1 addition & 1 deletion www/docs/usage/general.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@
| 3 | .terraform-version |
| 4 | Environment variable |

With 1 being the highest precedence and 4 the lowest
With 1 being the highest precedence and 4 the lowest
*(If you disagree with this order of precedence, please open an issue)*

0 comments on commit b460ffc

Please sign in to comment.