Comments (14)
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.
@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.
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.
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.
@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.
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.
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.
@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.
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.
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.
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.
@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.
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.
Yes, I can look into that.
from terraform-switcher.
Related Issues (20)
- tfswitch should not require sudo by default HOT 7
- Error: symlink /home/circleci/.terraform.versions/terraform_1.3.6 /home/circleci/bin: file exists
- Use default branch for releases instead of release branch HOT 2
- Offline/local mode HOT 1
- -u -s arguments ignore .tfswitch.toml bin config HOT 1
- Env var should be the highest order of precedence, not lowest HOT 5
- Does tfswitch verifies the sha256sums? HOT 2
- Add ability to show version (and not download) when parsing version from version.tf file
- Using versions.tf instead version.tf HOT 2
- Clarify the logic of display-only flags when the .tfswitch.toml config file provided HOT 1
- /usr/local/bin is hardcoded as default installation directory HOT 3
- Add ability for .tfswitch.toml file to specify alternative terraform mirror URL. HOT 2
- New macOS security feature prevents terraform to run (installed via tfswitch) HOT 2
- Killed: 9 Terraform on Mac Big Sur HOT 1
- "terraform_x.y.z" will damage your computer. You should move it to the Bin HOT 4
- Issue when configuring Artifactory mirror: cannot get list from mirror HOT 12
- OpenTofu Support HOT 15
- Terraform command not found HOT 2
- Ability to specify the terraform architecture to install on darwin platform HOT 1
- [Error] : Binary path does not exist: /usr/local/bin/terraform HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from terraform-switcher.