Giter Site home page Giter Site logo

Comments (14)

boxrick avatar boxrick commented on September 26, 2024 1

Hey sorry for the slow response, I appreciate your reply! This is more along the lines of:

Run tfswitch 0.14.2 then as a net result of that an environment variable is potentially set showing the current version

So for example in this case something like CURRENT_TF_VERSION=0.14.2

This would then allow us to consume this variable for usage in shell integration and such.

from terraform-switcher.

warrensbox avatar warrensbox commented on September 26, 2024 1

@yermulnik it looks like you have a .tfswitch.toml file in your home directory. In that .tfswitch.toml file, you probably have set set a version constrain for the terraform version. The environment variable that you've set has a lower precedent than the configuration in the .toml file.
See:

Order of precedence

Order Method
1 .tfswitch.toml
2 .tfswitchrc
3 .terraform-version
4 Environment variable

https://github.com/warrensbox/terraform-switcher/blob/master/README.md#order-of-precedence.

.tfswitch.toml > Environment variable

I did this because some users set their default bin path in their toml file (located in the home directory).

bin = "/Users/<username>/bin/terraform"
version = "0.11.3" ///DO NOT set this if you want to define version as environment variable

from terraform-switcher.

yermulnik avatar yermulnik commented on September 26, 2024 1

Oh, I see. Thanks.
Is there a way to sort of force precedence?
So in my use case I wouldn't want to use the most recent version of terraform which is 1.0.1 but would prefer to use the one which fits the constraint (0.13.5). I do understand that this is not what tfswitch was designed for though, but I do need to override its logic and use specific TF version w/o changing the constraint from version.tf.

Thanks for helping! And the BIG THANK YOU for this marvelous tool! 🙇‍♂️

from terraform-switcher.

boxrick avatar boxrick commented on September 26, 2024 1

Hello! Thanks again for this great tool, use it almost every day.

I wonder if precedent should be instead set by ENV vars first, this is the usual method most tools take ( Ansible / Terraform / AWS-cli etc ... rather than config files first.

from terraform-switcher.

warrensbox avatar warrensbox commented on September 26, 2024

@boxrick something like this?
//user set TF version
export TF=0.13.0

//if the $TF environment variable is set, users would be able to simply run tfswitch command to switch to their desired terraform version?
tfswitch

from terraform-switcher.

warrensbox avatar warrensbox commented on September 26, 2024

Use environment variable

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

export TF_VERSION=0.14.4
tfswitch #will automatically switch to terraform version 0.14.4

@boxrick Can you please test this feature?
Upgrade to the latest tfswitch for this feature:
https://tfswitch.warrensbox.com/Upgrade-or-Uninstall/

from terraform-switcher.

boxrick avatar boxrick commented on September 26, 2024

Hello! Thanks for the amazing software it works really nicely. I think this feature may be the inverse of what I meant.

I want tfswitch to export to the environment the 'current' running version rather than tfswitch change based on the environment if that makes sense?

The idea is then I can then take this env var and use it on the console to show a prompt with the current version we are switched to.

from terraform-switcher.

warrensbox avatar warrensbox commented on September 26, 2024

@boxrick I don't think we can export the version to the environment:

When tfswitch runs, it is executing in its own shell. This shell(child) gets a copy of its parent shell's environment when it is forked. When tfswitch completes running, the child is removed. There is no way to pass the environment variable to the parent.

Environment variables can only be passed from parent to child (as part of environment export/inheritance), not the other way around.

https://unix.stackexchange.com/a/372628
https://askubuntu.com/a/784805
https://golangcode.com/get-and-set-environment-variables/ (won't work).

from terraform-switcher.

yermulnik avatar yermulnik commented on September 26, 2024

Am I doing something wrong? 😕

> export TF_VERSION=0.13.5

> echo $TF_VERSION
0.13.5

> tfswitch
Reading configuration from home directory for .tfswitch.toml
Reading required version from terraform file
Reading required version from constraint: >= 0.13
Matched version: 1.0.1
Switched terraform to version "1.0.1"

> tfswitch --version

Version: 0.12.1119

from terraform-switcher.

yermulnik avatar yermulnik commented on September 26, 2024

Hey @warrensbox
Thanks for looking into this.
Here's the content of my conf file:

> cat ~/.tfswitch.toml
bin = "$HOME/bin/terraform"

No version constrain.
Is there any option/way to investigate why tfswitch doesn't pick env var?

Here's some more info if it may help to help me 😄 :

> strace tfswitch 2>&1 | egrep "\.tfswitch|\.terraform-version"
newfstatat(AT_FDCWD, "/home/giermulnik/data/XXX/XXXr/github.com/XXX/aws_infrastructure/wafv2/fe_api_apps/.tfswitch.toml", 0xc00013d968, 0) = -1 ENOENT (No such file or directory)
newfstatat(AT_FDCWD, "/home/giermulnik/.tfswitch.toml", {st_mode=S_IFREG|0600, st_size=28, ...}, 0) = 0
newfstatat(AT_FDCWD, "/home/giermulnik/data/XXX/XXXr/github.com/XXX/aws_infrastructure/wafv2/fe_api_apps/.tfswitch.toml", 0xc00013db08, 0) = -1 ENOENT (No such file or directory)
write(1, "Reading configuration from home "..., 61Reading configuration from home directory for .tfswitch.toml
newfstatat(AT_FDCWD, "/home/giermulnik/.tfswitch.json", 0xc00013dbd8, 0) = -1 ENOENT (No such file or directory)
newfstatat(AT_FDCWD, "/home/giermulnik/.tfswitch.toml", {st_mode=S_IFREG|0600, st_size=28, ...}, 0) = 0
openat(AT_FDCWD, "/home/giermulnik/.tfswitch.toml", O_RDONLY|O_CLOEXEC) = 3
newfstatat(AT_FDCWD, "/home/giermulnik/data/XXX/XXXr/github.com/XXX/aws_infrastructure/wafv2/fe_api_apps/.tfswitchrc", 0xc00013de48, 0) = -1 ENOENT (No such file or directory)
newfstatat(AT_FDCWD, "/home/giermulnik/data/XXX/XXXr/github.com/XXX/aws_infrastructure/wafv2/fe_api_apps/.terraform-version", 0xc00013df18, 0) = -1 ENOENT (No such file or directory)

> locate .tfswitch.toml .tfswitchrc .terraform-version
/home/giermulnik/.tfswitch.toml
/home/giermulnik/RCS/.tfswitch.toml,v

from terraform-switcher.

warrensbox avatar warrensbox commented on September 26, 2024

In your directory, where you run tfswitch, do you have the version specified in the .tf file?
For example, do you have:

terraform {
  required_version = ">= 0.13"

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

in your terraform file? @yermulnik

from terraform-switcher.

yermulnik avatar yermulnik commented on September 26, 2024

@warrensbox Yep, I do. This can be seen from tfswitch output:

> tfswitch
Reading configuration from home directory for .tfswitch.toml
Reading required version from terraform file            <- THIS
Reading required version from constraint: >= 0.13  <- and THIS
Matched version: 1.0.1
Switched terraform to version "1.0.1"

And here's the TF contraint:

> cat version.tf
terraform {
  required_version = ">= 0.13"
}

from terraform-switcher.

warrensbox avatar warrensbox commented on September 26, 2024

The version.tf has higher precedent compared to env vars.
version.tf > Environment variable
I should have documented that in the readme

from terraform-switcher.

warrensbox avatar warrensbox commented on September 26, 2024

Yes, I can look into that.

from terraform-switcher.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.