Last active
January 3, 2024 11:46
-
-
Save lkurzyniec/da61b9b5cec71730c3b89cdfe31f37ff to your computer and use it in GitHub Desktop.
tf version switcher
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@echo off | |
IF "%~1"=="" ( | |
call ECHO No version specified. Call with '?' parameter to get a list of available versions | |
) ELSE ( | |
IF "%~1"=="?" ( | |
call ECHO Available versions: | |
call ECHO ------------------- | |
call dir %PROGRAMDATA%\chocolatey\lib /b | findstr /i "terraform" | |
) ELSE ( | |
call ECHO %~1>"%UserProfile%/.terraform-version" | |
call ECHO Selected version: | |
call more "%UserProfile%/.terraform-version" | |
call ECHO Performing reload of PowerShell profile. After that, you are ready to go! | |
call ECHO ------------------- | |
call powershell & $profile | |
) | |
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Create file: | |
C:\Users\[user]\.terraform-version | |
with: | |
0.11.14 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
choco install terraform --version 1.1.5 -my | |
choco install terraform --version 1.1.9 -my |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# in your user profile file add below code, to get path to your user profile location execute: $PROFILE | |
function Set-TfEnv { | |
$tfenvfile = "$($env:UserProfile)/.terraform-version" | |
if (!(Test-Path $tfenvfile)) { | |
Remove-Alias -Name tf | |
Write-Host "No .terraform-version file" | |
return | |
} | |
$terraformVersion = Get-Content -Path $tfenvfile | |
$terraformVersion = $terraformVersion -replace "terraform.", "" | |
Set-Alias -Name tf -Value "C:\ProgramData\chocolatey\lib\terraform.$($terraformVersion)\tools\terraform.exe" -Scope Global | |
} | |
Set-TfEnv |
I also added a function to list installed terraform versions
function tfversions {
Get-ChildItem -Path 'C:\ProgramData\chocolatey\lib\' -Filter "terraform.*"
}
good idea! I refreshed !tf-switch.bat
file and added dir
command with findstr
pipe.
Side-by-side installs were removed by Chocolatey from version 2.0.0
https://docs.chocolatey.org/en-us/guides/upgrading-to-chocolatey-v2-v6#side-by-side-installs-have-been-removed-1
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Remember to use
tf
instead ofterraform
.