warrensbox / terraform-switcher

A command line tool to switch between different versions of terraform (install with homebrew and more)
https://tfswitch.warrensbox.com
MIT License
1.35k stars 135 forks source link

Allow fallback version #150

Closed johannes-mathes closed 1 year ago

johannes-mathes commented 3 years ago

We define a version requirement in most terraform modules, because we want the full control about updates. We use tfswitch also in a pipeline, without any parameter as we want to use the feature that tfswitch parses the version constraint and uses this. However, if it is not defined, tfswitch asks interactively and fails. We would like to specify a fallback version for such a case -> no interactive questions, as this does not work in a scripted environment

warrensbox commented 3 years ago

@johannes-mathes. Thanks for opening this issue.
You have some parts where you have terraform version defined in the module, like this:

terraform {
  required_version = ">= 0.12.9"

  required_providers {
    aws        = ">= 2.52.0"
    kubernetes = ">= 1.11.1"
  }
}

And you have some places where terraform is not defined, am I correct?

If the terraform version is not defined, what would you suggest tfswitch to do? Someone else suggested to have a -q argument here.

johannes-mathes commented 3 years ago

Yes, you are right. I would think a switch -q . Behaviour: If available, take terraform version specified by required_version. Otherwise, take FALLBACK_DEFAULT_VERSION.

And by the way, your tool is very great. We use it allover our project.

calebplum commented 3 years ago

+1 for this, our use case is also running tfswitch within a pipeline. We like to specify required_version in our templates but need the ability to fallback to the latest stable version if a template is missing required_version.

Being that the order of precedence has environment variables prioritised last it would make sense to be able to specify 'latest' or 'latest-stable' in the TF_VERSION environment variable and have it work in the same way as the -u and -s command-line flags. This would allow us to specify required_version in some projects and default to the latest stable when that's missing.

anherrera commented 3 years ago

In addition to this, I'd like the option to return an error if no valid version was found. Currently, tfswitch doesn't return 1 when you specify an invalid version, only if you specify an invalid flag. I'd like a way of using tfswitch in an automated fashion but also enforce that developers are setting versions either in their tf files or in env vars, and then error out if not.

╰─$ tfswitch butts              
Invalid terraform version format. Format should be #.#.# or #.#.#-@# where # are numbers and @ are word characters. For example, 0.11.7 and 0.11.9-beta1 are valid versions
Args must be a valid terraform version

Usage: tfswitch [-hluv] [-b value] [-p value] [-s value] [parameters ...]
 -b, --bin=value  Custom binary path. Ex: /Users/username/bin/terraform
 -h, --help       Displays help message
 -l, --list-all   List all versions of terraform - including beta and rc
 -p, --latest-pre=value
                  Latest pre-release implicit version. Ex: tfswitch
                  --latest-pre 0.13 downloads 0.13.0-rc1 (latest)
 -s, --latest-stable=value
                  Latest implicit version. Ex: tfswitch --latest 0.13
                  downloads 0.13.5 (latest)
 -u, --latest     Get latest stable version
 -v, --version    Displays the version of tfswitch
Supply the terraform version as an argument, or choose from a menu

╰─$ echo $?
0

or

╰─$ tfswitch 16.4328.4
The provided terraform version does not exist. Try `tfswitch -l` to see all available versions.

╰─$ echo $?           
0
warrensbox commented 3 years ago

I am still working on the Allow fallback version request.

Meanwhile @anherrera, I fixed your issue with release 0.12.1092: tfswitch butts and tfswitch 16.4328.4 should exit with 1 😄 https://tfswitch.warrensbox.com/Upgrade-or-Uninstall/

Again, I am still working on @johannes-mathes request.

anherrera commented 3 years ago

@warrensbox appreciate your swift response on this! Thank you! I'll keep an eye on this issue for the other bits :)