Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix global Terraform #19

Merged
merged 8 commits into from
May 2, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](http://keepachangelog.com/)
and this project adheres to [Semantic Versioning](http://semver.org/).

## [0.1.1]

Fix global terraform version by fallback to `terraform.exe` from path and reload path.

## [0.1.0]

Added option to set a global terraform version by adding to user's path. This should work with external applications like VSCode.
Expand Down
23 changes: 14 additions & 9 deletions WTerraform/Public/Invoke-WTerraform.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,22 @@ Invoke-WTerraform -version
Throws error if no Terraform Version was specified earlier. Otherwise runs command 'terraform -version'
#>
function Invoke-WTerraform {
$version = Get-WTerraformVersion
if ($version) {
$path = Join-Path -Path $env:LOCALAPPDATA -ChildPath "WTerraform" | Join-Path -ChildPath $version | Join-Path -ChildPath "terraform.exe"
if (Test-Path -LiteralPath $path) {
& $path $args
} else {
#TODO: Redownload terraform
throw "$version is not found"
$pwdVersion = Get-WTerraformVersion
$wTerraformPath = Join-Path -Path $env:LOCALAPPDATA -ChildPath "WTerraform"
if ($pwdVersion) {
$path = Join-Path -Path $wTerraformPath -ChildPath $pwdVersion | Join-Path -ChildPath "terraform.exe"
} elseif ($terraformCommand = Get-Command -Name "terraform.exe" -CommandType Application -ErrorAction SilentlyContinue) {
$path = $terraformCommand.Source
if (-not $path.StartsWith($wTerraformPath)) {
Write-Warning -Message "Using terraform in $path. This is not installed by WTerraform!"
}

} else {
throw "No Version for $pwd specified. Please Run Set-WTerraformVersion."
}
if (Test-Path -LiteralPath $path) {
& $path $args
} else {
#TODO: Redownload terraform
throw "$pwdVersion is not found"
}
}
1 change: 1 addition & 0 deletions WTerraform/Public/Set-WTerraformVersion.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ function Set-WTerraformVersion {
}
$path = $path + ";" + $versionPath
[System.Environment]::SetEnvironmentVariable("Path", $path, [System.EnvironmentVariableTarget]::User)
$env:Path = [System.Environment]::GetEnvironmentVariable("Path", [System.EnvironmentVariableTarget]::Machine) + ";"+ [System.Environment]::GetEnvironmentVariable("Path", [System.EnvironmentVariableTarget]::User)
} else {
$versionMap = Get-WTerraformVersionMap
$oldVersion = Get-WTerraformversion
Expand Down
2 changes: 1 addition & 1 deletion WTerraform/WTerraform.psd1
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
RootModule = 'WTerraform.psm1'

# Version number of this module.
ModuleVersion = '0.1.0'
ModuleVersion = '0.1.1'

# Supported PSEditions
#CompatiblePSEditions = @("Desktop")
Expand Down
19 changes: 18 additions & 1 deletion tests/Invoke-WTerraform.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ Describe "Invoke-WTerraform" {
Move-Item -LiteralPath $tempcachePath -Destination $cachePath
}
}
Context "Configured Folders" {
Context "Version for current Folder configured" {
BeforeAll {
$path = "TestDrive:\1"
New-Item -Path $path -ItemType Directory
Expand All @@ -49,4 +49,21 @@ Describe "Invoke-WTerraform" {
Pop-Location -ErrorAction SilentlyContinue
}
}

Context "Global Version configured" {
BeforeAll {
Set-WTerraformVersion -Version "0.14.0" -Global
$path = "TestDrive:\global"
New-Item -Path $path -ItemType Directory
Push-Location -Path $path
}
It "Should fallback to global version" {
$actualVersion = Invoke-WTerraform -version
$actualVersion[0] | Should -Be "Terraform v0.14.0"
}
AfterAll {
Pop-Location
Pop-Location -ErrorAction SilentlyContinue
}
}
}
3 changes: 3 additions & 0 deletions tests/Set-WTerraformVersion.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -87,5 +87,8 @@ Describe "Set-WTerraformVersion global" {
It "Should remove old Version Folder from Path" {
[System.Environment]::GetEnvironmentVariable("Path", [System.EnvironmentVariableTarget]::User) | Should -Not -BeLike "*$($versionPath)*"
}
It "Should reload Path" {
$env:Path | Should -BeLike "*$([System.Environment]::GetEnvironmentVariable("Path", [System.EnvironmentVariableTarget]::User))"
}
}
}