Skip to content

Instantly share code, notes, and snippets.

@asizikov
Created March 21, 2020 21:06
Get-ChildItem -Path %teamcity.build.checkoutDir%\infra\ -Filter terraform.tfvars |
Foreach-Object -Begin { Write-Host 'Processing tfvars files in directory'} {
$file_name = $_.FullName
Write-Host "Going to substitute variables in $file_name"
$content = Get-Content $file_name
for ($i = 0; $i -lt $content.Count; $i++) {
$line = $content[$i].Split("#") #allow comments in a line
if(![string]::IsNullOrEmpty($line[0])) { #do not process empty strings
$var_definition = $line[0].Split("=")
$var_name = $var_definition[0].Trim()
$new_line ="$var_name = `"#{$var_name}`"" #format the updated line
$content[$i] = $new_line
}
}
$content | Set-Content -Path "$file_name" #write the result back to the same file
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment