Public/Terraform/Get-Terraform.ps1
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 |
function Get-Terraform { [CmdletBinding()] param ( # The version of Terraform to use. # Defaults to global:TerraformVersion but if that is not set then a default version of 1.0.7 is used [Parameter( Mandatory = $false, Position = 0 )] [version] $TerraformVersion = "$(if ($Global:RepoTerraformVersion){"$Global:RepoTerraformVersion"}else{'1.0.8'})", # The path to download the binary to [Parameter( Mandatory = $true, Position = 1 )] [Alias('path')] [string] $DownloadPath ) # Make sure the directory path is good try { $DownloadPathInfo = Get-Item $DownloadPath -Force if (!$DownloadPathInfo.PSIsContainer) { Write-Error "$DownloadPath does not appear to be a directory" } } catch { throw "Error with DownloadPath.`n$($_.Exception.Message)" } # If we have desktop PoSh we must be on Windows if ($PSVersionTable.PSEdition -eq 'Desktop') { $TerraformDownloadURI = "https://releases.hashicorp.com/terraform/$TerraformVersion/terraform_$($TerraformVersion)_windows_amd64.zip" $TerraformPath = Join-Path $DownloadPath -ChildPath 'terraform.exe' } else { switch -regex ($PSVersionTable.OS) { '^[M|m]icrosoft [W|w]indows' { $TerraformDownloadURI = "https://releases.hashicorp.com/terraform/$TerraformVersion/terraform_$($TerraformVersion)_windows_amd64.zip" $TerraformPath = Join-Path $DownloadPath -ChildPath 'terraform.exe' } '^[D|d]arwin' { $TerraformDownloadURI = "https://releases.hashicorp.com/terraform/$TerraformVersion/terraform_$($TerraformVersion)_darwin_amd64.zip" $Chmod = $true $TerraformPath = Join-Path $DownloadPath -ChildPath 'terraform' } '^[L|l]inux' { $TerraformDownloadURI = "https://releases.hashicorp.com/terraform/$TerraformVersion/terraform_$($TerraformVersion)_linux_amd64.zip" $Chmod = $true $TerraformPath = Join-Path $DownloadPath -ChildPath 'terraform' } Default { Write-Error "Unknown OS: $($PSVersionTable.OS)" } } } # Download and extract Terraform $TerraformZipFile = Join-Path $DownloadPath 'terraform.zip' # If the ZIP file already exists it seems it won't trigger another download so let's try removing it first if ((Test-Path $TerraformZipFile) -eq $true) { Write-Verbose 'Removing previously downloaded archive' try { Remove-Item $TerraformZipFile -Force -Confirm:$false } catch { # Ignore it and hope for the best using the old zip... } } Write-Verbose 'Downloading Terraform binary...' try { Invoke-DownloadMethod -DownloadURI $TerraformDownloadURI -OutFile $TerraformZipFile Expand-Archive -LiteralPath $TerraformZipFile -DestinationPath $DownloadPath -Force # Force for when we're running locally and want to overwrite old files if ($Chmod -eq $true) { $Output = & chmod +x $TerraformPath if ($LASTEXITCODE -ne 0) { $Output Write-Error 'Failed to make Terraform executable' } } } catch { Write-Error $_.Exception.Message } # Providing everything has completed ok, set the terraform path $env:TerraformPath = $TerraformPath try { Set-Alias -Name 'terraform' -Value $TerraformPath -Scope global } catch { Write-Error $_.Exception.Message } } |