### Install tfswitch on Linux to a custom path Source: https://tfswitch.warrensbox.com/Installation Install tfswitch on Linux to a custom binary path using the install script with the -b flag. ```bash curl -L https://raw.githubusercontent.com/warrensbox/terraform-switcher/master/install.sh | bash -s -- -b $HOME/.local/bin ``` -------------------------------- ### Install tfswitch with Homebrew on macOS Source: https://tfswitch.warrensbox.com/Installation Use Homebrew to install tfswitch on macOS. Ensure Homebrew is installed first. ```bash brew install warrensbox/tap/tfswitch ``` -------------------------------- ### Install tfswitch on Linux to a custom path and specific version Source: https://tfswitch.warrensbox.com/Installation Install tfswitch on Linux to a custom binary path and a specific version by combining script options. ```bash curl -L https://raw.githubusercontent.com/warrensbox/terraform-switcher/master/install.sh | bash -s -- -b $HOME/.local/bin 1.1.1 ``` -------------------------------- ### Install tfswitch with Custom Directory Source: https://tfswitch.warrensbox.com/Troubleshoot Use this workaround when encountering permission denied errors during tfswitch installation. It involves downloading the script, making it executable, installing to a user-writable directory, and updating the PATH. ```bash wget https://raw.githubusercontent.com/warrensbox/terraform-switcher/master/install.sh #Get the installer on to your machine: ``` ```bash chmod 755 install.sh #Make installer executable ``` ```bash ./install.sh -b $HOME/.bin #Install tfswitch in a location you have permission: ``` ```bash $HOME/.bin/tfswitch #test ``` ```bash export PATH=$PATH:$HOME/.bin #Export your .bin into your path ``` ```bash #You should probably add step 4 in your `.bash_profile` in your $HOME directory. ``` ```bash #Next, try: tfswitch -b $HOME/.bin/terraform 0.11.7 ``` ```bash #or simply tfswitch -b $HOME/.bin/terraform ``` -------------------------------- ### Set product in .tfswitch.toml Source: https://tfswitch.warrensbox.com/usage/config-files Configure the `product` parameter in `.tfswitch.toml` to set the default tool to install, either Terraform or OpenTofu. ```toml product = "opentofu" ``` ```toml product = "terraform" ``` -------------------------------- ### Install a specific version of tfswitch on Linux Source: https://tfswitch.warrensbox.com/Installation Install a specific version of tfswitch on Linux by providing the version number to the install script. ```bash curl -L https://raw.githubusercontent.com/warrensbox/terraform-switcher/master/install.sh | bash -s -- 1.1.1 ``` -------------------------------- ### Install Latest Implicit Pre-release Version Source: https://tfswitch.warrensbox.com/usage/commandline Install the latest version, including pre-releases, within a specified branch using `-p` or `--latest-pre`. For example, `tfswitch -p 0.13` installs the latest pre-release on the `0.13` branch. ```bash tfswitch -p 0.13 ``` ```bash tfswitch --latest-pre 0.13 ``` -------------------------------- ### Install tfswitch on Arch Linux via AUR Source: https://tfswitch.warrensbox.com/Installation Install tfswitch on Arch Linux using an AUR helper like yay. Options include installing from source or using a precompiled binary. ```bash # compiled from source yay tfswitch # precompiled yay tfswitch-bin ``` -------------------------------- ### Set TF_PRODUCT Environment Variable Source: https://tfswitch.warrensbox.com/usage/environment-variables Specify the product to install, either `terraform` or `opentofu`. ```bash export TF_PRODUCT="opentofu" tfswitch # Will install opentofu instead of terraform ``` -------------------------------- ### Jenkins Setup for tfswitch Source: https://tfswitch.warrensbox.com/Continuous-Integration This script installs tfswitch locally on a Jenkins agent, sets the custom binary path, and then uses tfswitch to manage Terraform versions. It's useful for ensuring consistent Terraform versions in your Jenkins builds. ```bash #!/bin/bash echo "Installing tfswitch locally" wget https://raw.githubusercontent.com/warrensbox/terraform-switcher/master/install.sh #Get the installer on to your machine chmod 755 install.sh #Make installer executable ./install.sh -b `pwd`/.bin #Install tfswitch in a location you have permission CUSTOMBIN=`pwd`/.bin #set custom bin path export PATH=$PATH:$CUSTOMBIN #Add custom bin path to PATH environment $CUSTOMBIN/tfswitch -b $CUSTOMBIN/terraform 0.11.7 #or simply tfswitch -b $CUSTOMBIN/terraform 0.11.7 #OR $CUSTOMBIN/tfswitch -d 0.11.7 -b $CUSTOMBIN/terraform #or simply tfswitch -d 0.11.7 -b $CUSTOMBIN/terraform terraform -v #testing version ``` -------------------------------- ### Install Latest Implicit Stable Version Source: https://tfswitch.warrensbox.com/usage/commandline Install the latest stable version within a specified branch using `-s` or `--latest-stable`. For example, `tfswitch -s 0.13` installs the latest on the `0.*` branch. ```bash tfswitch -s 0.13 ``` ```bash tfswitch --latest-stable 0.13 ``` ```bash tfswitch -s 0.13.5 ``` ```bash tfswitch --latest-stable 0.13.5 ``` ```bash tfswitch -s 0 ``` ```bash tfswitch --latest-stable 0 ``` -------------------------------- ### Install Binary for a Different CPU Architecture Source: https://tfswitch.warrensbox.com/usage/commandline Download Terraform binaries for a CPU architecture that doesn't match the host machine using the `-A` or `--arch` command-line argument. ```bash tfswitch --arch amd64 ``` -------------------------------- ### Install Terraform Binaries to Non-Default Location Source: https://tfswitch.warrensbox.com/usage/commandline Use the `-i` or `--install` flag to specify a custom directory for installing Terraform binaries, allowing them to be shared across users. ```bash tfswitch -i /opt/terraform ``` -------------------------------- ### Specify custom installation path in .tfswitch.toml Source: https://tfswitch.warrensbox.com/usage/config-files Configure the `.tfswitch.toml` file to specify a custom installation path for the Terraform binary, useful for non-admin users. ```toml bin = "C:\\Users\\<%USERNAME%>\\bin\\terraform.exe" ``` -------------------------------- ### Get Go Dependencies with Verbose Output Source: https://tfswitch.warrensbox.com/How-to-Contribute Fetch all dependencies, including test dependencies, and display verbose output. This is useful for debugging dependency issues. ```go go get -v -t -d ./... ``` -------------------------------- ### CircleCI Setup for tfswitch Source: https://tfswitch.warrensbox.com/Continuous-Integration This CircleCI configuration installs tfswitch within the build environment, sets up the necessary paths, and then uses tfswitch to manage Terraform versions. It ensures that your CircleCI jobs use the correct Terraform version for your project. ```yaml version: 2 jobs: build: docker: - image: ubuntu working_directory: /go/src/github.com/warrensbox/terraform-switcher steps: - checkout - run: command: | set +e apt-get update apt-get install -y wget rm -rf /var/lib/apt/lists/* echo "Installing tfswitch locally" wget https://raw.githubusercontent.com/warrensbox/terraform-switcher/master/install.sh #Get the installer on to your machine chmod 755 install.sh #Make installer executable ./install.sh -b `pwd`/.bin #Install tfswitch in a location you have permission CUSTOMBIN=`pwd`/.bin #set custom bin path export PATH=$PATH:$CUSTOMBIN #Add custom bin path to PATH environment $CUSTOMBIN/tfswitch -b $CUSTOMBIN/terraform 0.11.7 #or simply tfswitch -b $CUSTOMBIN/terraform 0.11.7 #OR $CUSTOMBIN/tfswitch -d 0.11.7 -b $CUSTOMBIN/terraform #or simply tfswitch -d 0.11.7 -b $CUSTOMBIN/terraform terraform -v #testing version ``` -------------------------------- ### Install Latest Stable Terraform Version Source: https://tfswitch.warrensbox.com/usage/commandline Use the `-u` or `--latest` flag to install only the latest stable version of Terraform. ```bash tfswitch -u ``` ```bash tfswitch --latest ``` -------------------------------- ### Upgrade tfswitch using install script (Linux) Source: https://tfswitch.warrensbox.com/Upgrade-or-Uninstall Re-run the installation script to upgrade tfswitch on Linux. This method is used if tfswitch was initially installed via the curl script. ```bash curl -L https://raw.githubusercontent.com/warrensbox/terraform-switcher/master/install.sh | bash ``` -------------------------------- ### Override tfswitch Binary Installation Directory Source: https://tfswitch.warrensbox.com/usage/config-files Configure the `install` parameter in `.tfswitch.toml` to specify a custom parent directory for downloaded Terraform binaries. Ensure the current user has write permissions to the target directory; tfswitch will create it if it doesn't exist. ```toml install = "/var/cache" ``` -------------------------------- ### Set TF_BINARY_PATH Environment Variable Source: https://tfswitch.warrensbox.com/usage/environment-variables Specify the full installation path, including the filename, for the binary. If the directory doesn't exist, tfswitch falls back to `$HOME/bin/`. ```bash export TF_BINARY_PATH="$HOME/bin/terraform" # Path to the file tfswitch # Will install binary as $HOME/bin/terraform ``` -------------------------------- ### Upgrade tfswitch using Homebrew Source: https://tfswitch.warrensbox.com/Upgrade-or-Uninstall Use this command to upgrade tfswitch if it was installed via Homebrew. ```bash brew upgrade warrensbox/tap/tfswitch ``` -------------------------------- ### Uninstall tfswitch using Homebrew Source: https://tfswitch.warrensbox.com/Upgrade-or-Uninstall Use this command to uninstall tfswitch if it was installed via Homebrew. ```bash brew uninstall warrensbox/tap/tfswitch ``` -------------------------------- ### Define Terraform version in version.tf Source: https://tfswitch.warrensbox.com/usage/config-files Place a `version.tf` file in the current directory to automatically set the Terraform version. This example sets the version to be newer than 0.12.8. ```hcl terraform { required_version = ">= 0.12.9" required_providers { aws = ">= 2.52.0" kubernetes = ">= 1.11.1" } } ``` -------------------------------- ### Show Latest Terraform Version Source: https://tfswitch.warrensbox.com/usage/commandline Use the `-U` or `--show-latest` flag to display only the latest stable version of Terraform without installing it. ```bash tfswitch -U ``` ```bash tfswitch --show-latest ``` -------------------------------- ### Uninstall tfswitch manually (Linux) Source: https://tfswitch.warrensbox.com/Upgrade-or-Uninstall Remove the tfswitch executable file manually. Replace `/usr/local/bin` if tfswitch was installed to a custom location. ```bash rm /usr/local/bin/tfswitch ``` -------------------------------- ### Set Default TF Version for CI/CD Source: https://tfswitch.warrensbox.com/usage/ci-cd Use the `-d` or `--default` flag to install a specific Terraform version when no other versions can be detected in your CI/CD pipeline. ```bash tfswitch -d 1.2.3 ``` ```bash tfswitch --default 1.2.3 ``` -------------------------------- ### Show Latest Implicit Stable Version Source: https://tfswitch.warrensbox.com/usage/commandline Display the latest stable version within a specified branch using `-S` or `--show-latest-stable`. For example, `tfswitch -S 0.13` shows the latest on the `0.*` branch. ```bash tfswitch -S 0.13 ``` ```bash tfswitch --show-latest-stable 0.13 ``` ```bash tfswitch -S 0.13.5 ``` ```bash tfswitch --show-latest-stable 0.13.5 ``` -------------------------------- ### List All Terraform Versions (including pre-releases) Source: https://tfswitch.warrensbox.com/usage/commandline Use the `-l` or `--list-all` flag to display all available Terraform versions, including beta, alpha, and release candidates. ```bash tfswitch -l ``` ```bash tfswitch --list-all ``` -------------------------------- ### Download Go Module Dependencies Source: https://tfswitch.warrensbox.com/How-to-Contribute Download all the necessary dependencies for the Go modules used in the project. This command ensures that all required external packages are available. ```go go mod download ``` -------------------------------- ### Specify Go Version Source: https://tfswitch.warrensbox.com/How-to-Contribute Ensure you are using Go version 1.23 or higher for development. ```shell go version 1.23 ``` -------------------------------- ### Set TF_INSTALL_PATH Environment Variable Source: https://tfswitch.warrensbox.com/usage/environment-variables Specify the parent directory where the `.terraform.versions` directory will be created. The current user must have write permissions. ```bash export TF_INSTALL_PATH="/var/cache" # Path to the directory where `.terraform.versions` directory resides tfswitch # Will download actual binary to /var/cache/.terraform.versions/ ``` -------------------------------- ### Select Terraform Version via Dropdown Source: https://tfswitch.warrensbox.com/usage/commandline Run `tfswitch` to open an interactive dropdown menu for selecting Terraform versions. Use arrow keys to navigate and Enter to select. ```bash tfswitch ``` -------------------------------- ### Build tfswitch Executable Source: https://tfswitch.warrensbox.com/How-to-Contribute Build the tfswitch executable and name the output file 'test-tfswitch'. This command compiles the Go source code into a runnable binary. ```go go build -o test-tfswitch ``` -------------------------------- ### Set TF_DEFAULT_VERSION Environment Variable Source: https://tfswitch.warrensbox.com/usage/environment-variables Set a fallback version for the product if no other version sources are found. ```bash export TF_DEFAULT_VERSION="0.14.4" tfswitch # Will automatically switch to terraform version 0.14.4 ``` -------------------------------- ### Use Custom Mirror for Terraform Binaries Source: https://tfswitch.warrensbox.com/usage/commandline Specify a custom mirror URL for downloading Terraform binaries using the `-m` or `--mirror` parameter. ```bash tfswitch --mirror https://example.jfrog.io/artifactory/hashicorp ``` -------------------------------- ### Run Go Tests Source: https://tfswitch.warrensbox.com/How-to-Contribute Execute all tests within the project with verbose output. This helps ensure that the code changes have not introduced regressions. ```go go test -v ./ ``` -------------------------------- ### Test Go Code with Vet Source: https://tfswitch.warrensbox.com/How-to-Contribute Run the 'go vet' command to statically analyze the Go code and identify potential errors or suspicious constructs. The '-tests=false' flag excludes test files from analysis. ```go go vet -tests=false ./ ``` -------------------------------- ### Check Product Version Against Requirement Source: https://tfswitch.warrensbox.com/usage/commandline Use the `--match-version-requirement` flag to check if a specific product version satisfies a given requirement. The exit code indicates success (0), mismatch (2), or error (1). ```bash tfswitch --chdir=/tmp/test/ --match-version-requirement=1.10.5; echo $? ``` ```bash tfswitch --chdir=/tmp/test/ --match-version-requirement=1.10.1; echo $? ``` ```bash tfswitch --chdir=/tmp/test/ --match-version-requirement=string; echo $? ``` ```bash tfswitch --match-version-requirement=1.10.5; echo $? ``` -------------------------------- ### Override CPU architecture in .tfswitch.toml Source: https://tfswitch.warrensbox.com/usage/config-files Use the `arch` parameter in `.tfswitch.toml` to specify a CPU architecture different from the host for downloading binaries. ```toml arch = "arm64" ``` -------------------------------- ### Set TF_ARCH Environment Variable Source: https://tfswitch.warrensbox.com/usage/environment-variables Override the default CPU architecture for downloaded binaries. Ensure the value is a supported architecture string. ```bash export TF_ARCH="amd64" tfswitch # Will install binary for amd64 architecture ``` -------------------------------- ### Set Terraform version with .tfswitchrc Source: https://tfswitch.warrensbox.com/usage/config-files Create a `.tfswitchrc` file with the desired version to have tfswitch use it automatically. ```bash echo "0.10.5" >> .tfswitchrc ``` -------------------------------- ### Set default version in .tfswitch.toml Source: https://tfswitch.warrensbox.com/usage/config-files Use the `default-version` parameter in `.tfswitch.toml` to specify a fallback version if no other version sources are found. ```toml default-version = "1.5.4" ``` -------------------------------- ### Specify Terraform Version on Command Line Source: https://tfswitch.warrensbox.com/usage/commandline Provide the desired Terraform version directly as an argument to `tfswitch`. ```bash tfswitch 0.10.5 ``` -------------------------------- ### Fish Shell: Automatically Switch Terraform Version Source: https://tfswitch.warrensbox.com/usage/ci-cd Add this function to your `~/.config/fish/config.fish` to automatically execute `tfswitch` after a `cd` command if Terraform files are present and specify a `required_version`. ```fish function switch_terraform --on-event fish_postexec string match --regex '^cd\s' "$argv" > /dev/null set --local is_command_cd $status if test $is_command_cd -eq 0 if count *.tf > /dev/null grep -c "required_version" *.tf > /dev/null set --local tf_contains_version $status if test $tf_contains_version -eq 0 command tfswitch end end end end ``` -------------------------------- ### Set log level in .tfswitch.toml Source: https://tfswitch.warrensbox.com/usage/config-files Configure the `log-level` parameter in `.tfswitch.toml` to control the verbosity of tfswitch logging. ```toml log-level = "INFO" ``` -------------------------------- ### Show Latest Implicit Pre-release Version Source: https://tfswitch.warrensbox.com/usage/commandline Display the latest version, including pre-releases, within a specified branch using `-P` or `--show-latest-pre`. ```bash tfswitch -P 0.13 ``` ```bash tfswitch --show-latest-pre 0.13 ``` -------------------------------- ### Bash: Automatically Switch Terraform Version Source: https://tfswitch.warrensbox.com/usage/ci-cd Add this function to your `~/.bashrc` to automatically run `tfswitch` when changing directories if a `.tfswitchrc` file exists. ```bash cdtfswitch(){ builtin cd "$@"; cdir=$PWD; if [ -e "$cdir/.tfswitchrc" ]; then tfswitch fi } alias cd='cdtfswitch' ``` -------------------------------- ### Zsh (Older Versions): Automatically Switch Terraform Version Source: https://tfswitch.warrensbox.com/usage/ci-cd For older Zsh versions lacking `add-zsh-hook`, redefine the `cd` command in your `.zshrc` to check for `.tfswitchrc` and run `tfswitch`. ```zsh cd(){ builtin cd "$@"; cdir=$PWD; if [ -e "$cdir/.tfswitchrc" ]; then tfswitch fi } ``` -------------------------------- ### Zsh: Automatically Switch Terraform Version Source: https://tfswitch.warrensbox.com/usage/ci-cd Configure your `~/.zshrc` with this function to trigger `tfswitch` on directory changes if a `.tfswitchrc` is present. Ensure `add-zsh-hook` is loaded. ```zsh load-tfswitch() { local tfswitchrc_path=".tfswitchrc" if [ -f "$tfswitchrc_path" ]; then tfswitch fi } add-zsh-hook chpwd load-tfswitch load-tfswitch ``` -------------------------------- ### Set GOPATH Environment Variable Source: https://tfswitch.warrensbox.com/How-to-Contribute Export your GOPATH environment variable to point to your GitHub Go workspace directory. This is necessary for Go to locate your project files. ```shell export GOPATH=`pwd` ``` -------------------------------- ### Create a New Feature Branch Source: https://tfswitch.warrensbox.com/How-to-Contribute Create a new Git branch for your feature development. Replace 'put-your-branch-name-here' with a descriptive name for your branch. ```git git checkout -b feature/put-your-branch-name-here ``` -------------------------------- ### Clone tfswitch Repository Source: https://tfswitch.warrensbox.com/How-to-Contribute Clone the tfswitch repository from GitHub using the provided SSH URL. This command downloads the project's source code to your local machine. ```git git clone git@github.com:warrensbox/terraform-switcher.git ``` -------------------------------- ### Show Required (or Explicitly Requested) Version Source: https://tfswitch.warrensbox.com/usage/commandline Use the `-R` or `--show-required` flag to display the Terraform version that satisfies current constraints (from modules, config files, env vars, etc.). Defaults to the latest version if no constraints are found. ```bash tfswitch -R ``` ```bash tfswitch --show-required ``` -------------------------------- ### Change working directory with tfswitch Source: https://tfswitch.warrensbox.com/usage/config-files Use the `--chdir` or `-c` parameter to specify a subdirectory for Terraform configuration. ```bash tfswitch --chdir terraform_dir ``` ```bash tfswitch -c terraform_dir ``` -------------------------------- ### Set TF_TERRAGRUNT_CONFIG_FILE_NAME Environment Variable Source: https://tfswitch.warrensbox.com/usage/environment-variables Set a custom filename for Terragrunt configuration files. This affects how `terraform_version_constraint` is resolved. ```bash export TF_TERRAGRUNT_CONFIG_FILE_NAME="tgconfig.hcl" tfswitch # Will look up `terraform_version_constraint` from `tgconfig.hcl` file ``` -------------------------------- ### Set TF_LOG_LEVEL Environment Variable Source: https://tfswitch.warrensbox.com/usage/environment-variables Override the default logging level. Supported levels range from `OFF` to `TRACE`. ```bash export TF_LOG_LEVEL="DEBUG" tfswitch # Will output debug logs ``` -------------------------------- ### Set TF_VERSION Environment Variable Source: https://tfswitch.warrensbox.com/usage/environment-variables Set the `TF_VERSION` environment variable to specify the exact Terraform version you want tfswitch to use. tfswitch will then automatically switch to that version. ```bash export TF_VERSION="0.14.4" tfswitch # Will automatically switch to terraform version 0.14.4 ``` -------------------------------- ### Specify Terraform Version Constraint in Terragrunt Source: https://tfswitch.warrensbox.com/usage/config-files Define the `terraform_version_constraint` in `terragrunt.hcl` (or `root.hcl`) to automatically switch to a specific Terraform version. This is useful for managing Terraform versions in projects using Terragrunt. ```hcl terraform_version_constraint = ">= 0.13, < 0.14" ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.