97 lines
3.8 KiB
PowerShell
97 lines
3.8 KiB
PowerShell
|
function Use-TerraformVersion {
|
||
|
<#
|
||
|
.SYNOPSIS
|
||
|
This function will ensure that the version of Terraform in your runtime is the version you want to run.
|
||
|
If you already have a version of terraform downloaded it will just use that version. If not it will download the version to your profile.
|
||
|
This function will ensure the version of terraform you have selected is in the path.
|
||
|
|
||
|
.PARAMETER Version
|
||
|
The version of terraform to use. Supplying a garbage value will print the list of available values.
|
||
|
|
||
|
.PARAMETER UseLatestVersion
|
||
|
Use the latest version.
|
||
|
#>
|
||
|
param (
|
||
|
[Parameter(Mandatory = $false)]
|
||
|
[string]$Version,
|
||
|
[switch]$UseLatestVersion
|
||
|
)
|
||
|
|
||
|
$terraformExe = "terraform.exe"
|
||
|
|
||
|
# Get the version list to verify what you can work with
|
||
|
|
||
|
$baseUrl = "https://releases.hashicorp.com/"
|
||
|
|
||
|
$releaseDocument = (Invoke-WebRequest -Uri (Join-UrlComponents -BaseUrl $baseUrl -Path "terraform/") -UseBasicParsing -Method Get)
|
||
|
|
||
|
$links = $releaseDocument.get_links()
|
||
|
|
||
|
$latestVersion = $null
|
||
|
$versions = @{}
|
||
|
foreach ($link in $links) {
|
||
|
$text = ($link.outerHtml -split '>' -split '<')[2]
|
||
|
$url = $link.href
|
||
|
$versionSplits = ($text -split '_' -split '-')
|
||
|
$packageVersion = $versionSplits[1]
|
||
|
if (($null -ne $url) -and ($url.IndexOf('terraform') -gt -1) -and ($versionSplits.Count -eq 2)) {
|
||
|
$entry = @{ url = (Join-UrlComponents -BaseUrl $baseUrl -Path $url,"terraform_$($packageVersion)_windows_amd64.zip"); text = $text; version = $packageVersion; }
|
||
|
if ($null -eq $latestVersion) {
|
||
|
$latestVersion = $entry
|
||
|
}
|
||
|
$versions[$packageVersion] = $entry
|
||
|
}
|
||
|
}
|
||
|
|
||
|
if ($UseLatestVersion) {
|
||
|
$Version = $latestVersion.version
|
||
|
}
|
||
|
|
||
|
$targetVersion = $versions[$Version]
|
||
|
|
||
|
if ($null -eq $targetVersion) {
|
||
|
Write-Warning "The specified version [$Version] does not exist in the lookup list."
|
||
|
Write-Host "The available versions are:"
|
||
|
|
||
|
Show-ListAsTable ($versions.Keys | Sort-Object -Descending)
|
||
|
|
||
|
throw "The specified version [$Version] does not exist in the lookup list."
|
||
|
}
|
||
|
|
||
|
$targetPath = (Expand-Path -Path "~/.terraform/$Version")
|
||
|
$terraformPath = (Join-Path $targetPath $terraformExe)
|
||
|
$downloadPath = (Join-Path $targetPath "terraform.zip")
|
||
|
if (!(Test-Path $targetPath)) {
|
||
|
(New-Directory -Path $targetPath) | Out-Null
|
||
|
}
|
||
|
|
||
|
if (!(Test-Path $terraformPath)) {
|
||
|
Write-Host "Downloading version [$($Version)] from [$(targetVersion.url)] to [$downloadPath]"
|
||
|
|
||
|
(Invoke-WebRequest -Uri $targetVersion.url -OutFile $downloadPath -Method Get)
|
||
|
Expand-Archive -Path $downloadPath -DestinationPath $targetPath
|
||
|
}
|
||
|
|
||
|
# Assert that the file exists
|
||
|
if (!(Test-Path $terraformPath)) {
|
||
|
throw "Something has gone wrong, and there is no $terraformExe @ [$terraformPath]"
|
||
|
}
|
||
|
|
||
|
# now we have a folder that contains our exe file, let's set the path to reference the one we want
|
||
|
$existingLocation = (Get-Command -Name $terraformExe -ErrorAction SilentlyContinue)
|
||
|
$folderToRemove = $null
|
||
|
if ($null -ne $existingLocation) {
|
||
|
# the terraform.exe path is already on our %PATH% so we want to remove it
|
||
|
# Find the folder from the path
|
||
|
$folderToRemove = (Split-Path -Path $existingLocation.Path -Parent)
|
||
|
}
|
||
|
|
||
|
Write-Host "Ensuring the correct path is set at the machine (removed if set), user and process levels so you can use the right version right away"
|
||
|
|
||
|
if (![string]::IsNullOrWhiteSpace($folderToRemove)) {
|
||
|
Set-PathVariable -StoreName Machine -Remove $folderToRemove
|
||
|
}
|
||
|
Set-PathVariable -StoreName User -Prepend $targetPath -Remove $folderToRemove
|
||
|
Set-PathVariable -StoreName 'Process' -Prepend $targetPath -Remove $folderToRemove
|
||
|
}
|