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

Allow fallback version #150

Closed
johannes-mathes opened this issue Mar 11, 2021 · 6 comments · Fixed by #275
Closed

Allow fallback version #150

johannes-mathes opened this issue Mar 11, 2021 · 6 comments · Fixed by #275
Assignees

Comments

@johannes-mathes
Copy link

johannes-mathes commented Mar 11, 2021

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
Copy link
Owner

warrensbox commented Mar 12, 2021

@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.

@warrensbox warrensbox self-assigned this Mar 12, 2021
@johannes-mathes
Copy link
Author

Yes, you are right. I would think a switch -q <FALLBACK_DEFAULT_VERSION>.
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
Copy link

+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
Copy link

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
Copy link
Owner

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
Copy link

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

@yermulnik yermulnik linked a pull request Oct 13, 2022 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants