Last active
February 9, 2023 01:16
-
-
Save beau-witter/a2f87ed16a169f387bbe8b7fb8642632 to your computer and use it in GitHub Desktop.
Allows switching between different versions of Terraform on Windows, similar to TFSwitch or tfenv.
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
function Set-TerraformVersion { | |
param( | |
[string]$version | |
) | |
# Constants | |
$BaseUrl = "https://releases.hashicorp.com/terraform" | |
$UsageInfo = "Usage: Set-TerraformVersion <VERSION>`n Example: Set-TerraformVersion 0.14.11`n Or you can specify 'latest' to get the latest version" | |
if([string]::IsNullOrEmpty($version)) { | |
Write-Output $UsageInfo | |
return | |
} | |
$tfVersion | |
$response = Invoke-WebRequest $BaseUrl | |
$content = $response.Content.Split([Environment]::NewLine) | |
$versionMatches = ($content | Select-String -Pattern "/[.0-9]+/" -AllMatches).Matches | |
$versions = $versionMatches.Value -replace "/" | |
$sorted = $versions | Sort-Object {[Version] $_} -Descending | |
if($version.ToLower() -eq "latest") { | |
$tfVersion = $sorted[0] | |
} else { | |
$versionString = Get-NormalizedVersion($version) | |
$specificMatches = $sorted.where{$_ -match "^$versionString"} | |
if($specificMatches.Count -eq 0) { | |
Write-Output "No versions were found that match your input. Check that your input is not too specific.`nAlso make sure the version you are asking for either still exists or has been released." | |
return | |
} | |
$tfVersion = $specificMatches[0] | |
} | |
$tfPath = (Get-Command -Name terraform -ErrorAction SilentlyContinue) | |
if($null -eq $tfPath) { | |
Write-Output "Terraform is not currently installed, install a version first to continue." | |
return | |
} else { | |
$versionText = (terraform version)[0] | |
if ($versionText.Where{$_ -match "v$tfVersions$"}) { | |
Write-Output "This version is already installed. Exiting without downloading anything..." | |
return | |
} | |
} | |
$TargetUrl = "${BaseUrl}/${tfVersion}/terraform_${tfVersion}_windows_amd64.zip" | |
Write-Output "Downloading and switching to Terraform version ${tfVersion}" | |
Write-Output "Please wait..." | |
if(!(Test-Path ~/tmp)) { | |
New-Item -ItemType Directory -Path ~/tmp | Out-Null | |
} | |
Push-Location | |
Set-Location -Path ~/tmp | |
Invoke-WebRequest ${TargetUrl} -OutFile "terraform_${tfVersion}_windows_amd64.zip" | |
Expand-Archive -Path "terraform_${tfVersion}_windows_amd64.zip" | |
Move-Item -Path "terraform_${tfVersion}_windows_amd64/terraform.exe" -Destination $tfPath.Source -Force | |
$newVersion = (terraform version)[0] | |
Write-Output "You are now using $newVersion" | |
Pop-Location | |
Remove-Item -Path ~/tmp -Recurse -Force | |
} | |
function Get-NormalizedVersion { | |
param ( | |
[string] $versionString | |
) | |
if (-not $versionString.EndsWith(".")) { | |
$versionString = "$versionString." | |
} | |
return $versionString | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment