terraform-switcher (tfswitch)

repository·master·Indexed 23 days ago

https://github.com/warrensbox/terraform-switcher

A command-line tool for managing and switching between different versions of Terraform and OpenTofu. It automates the downloading and verification of binaries, offering interactive version selection or direct command-line arguments. Supports installation on Windows, macOS, and Linux (including Arch Linux via AUR), and provides integration guides for CI/CD environments like Jenkins and CircleCI.

Tokens
7.5K
Snippets
36
Records
60
Agent score
81%

What's inside terraform-switcher

  1. Understand Terraform version definition precedence

    master

    When using terraform-switcher, the tool determines which Terraform version to use based on a specific hierarchy of configuration sources. If multiple versions are defined, the method with the highest precedence wins.

    Precedence order (from lowest to highest):

    1. Global Config: The version parameter in $HOME/.tfswitch.toml.
    2. Local Config File: A .tfswitchrc file containing the version as a string.
    3. Version File: A .terraform-version file containing the version as a string.
    4. Terraform Module: The required_version constraint within your Terraform root module.
    5. Terragrunt/Root Config: The terraform_version_constraint parameter in terragrunt.hcl or root.hcl.
    6. Environment Variable: The TF_VERSION environment variable.
    7. CLI Argument: The version explicitly provided as a command line argument to the terraform-switcher command.
    | Order | Method | |
    | ----- | ----- | |
    | 1 | `$HOME/.tfswitch.toml` (`version` parameter) | |
    | 2 | `.tfswitchrc` (version as a string) | |
    | 3 | `.terraform-version` (version as a string) | |
    | 4 | Terraform root module (`required_version` constraint) | |
    | 5 | `terragrunt.hcl` or `root.hcl` (`terraform_version_constraint` parameter) | |
    | 6 | Environment variable (`TF_VERSION`) | |
    | 7 | Version provided as command line argument |
  2. Switch version using terragrunt.hcl or root.hcl

    master

    tfswitch automatically detects Terraform version constraints defined in Terragrunt configuration files. It first looks for terragrunt.hcl; if not found or if the constraint is missing, it looks for root.hcl in the same directory.

    Example terragrunt.hcl constraint:

    terraform_version_constraint = ">= 0.13, < 0.14"
  3. Note on tfswitch version string changes

    master

    The tfswitch version identifier has changed to align with standard Go package versioning. Version numbers are now prefixed with a v.

    Important for automation: If you have automated scripts or implementations that rely on parsing the tfswitch version string, you must update them to handle the new format.

    • Old format: 0.1.2412
    • New format: v1.0.0 (e.g., v1.0.3)
  4. Set up a development environment for terraform-switcher

    master

    To contribute to terraform-switcher, ensure you have Go version 1.23 installed. Follow these steps to prepare your local workspace:

    1. Set GOPATH: If you are not using a GitHub go workspace, export your GOPATH to your current directory:
      export GOPATH=`pwd`
    2. Clone the repository:
      git clone git@github.com:warrensbox/terraform-switcher.git
    3. Download dependencies: Use go mod download and go get to fetch required modules:
      go mod download
      go get -v -t -d ./...
    export GOPATH=`pwd`
    
    git clone git@github.com:warrensbox/terraform-switcher.git
    
    go mod download
    go get -v -t -d ./...