-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathFormat-Terraform.ps1
More file actions
46 lines (40 loc) · 1.76 KB
/
Format-Terraform.ps1
File metadata and controls
46 lines (40 loc) · 1.76 KB
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
Function Format-Terraform {
<#
.SYNOPSIS
Runs terraform fmt on the current directory and subdirectories.
.DESCRIPTION
Runs terraform fmt on the current directory and subdirectories.
.PARAMETER Path
Path to the directory containing the Terraform files. Defaults to the current directory.
.PARAMETER Recurse
Switch parameter. If specified, runs terraform fmt recursively on all subdirectories.
.EXAMPLE
Format-Terraform -Path C:\Code\Terraform -Recurse
#>
Param (
[Parameter(Mandatory=$false,
ValueFromPipeline=$true,
Position=0,
HelpMessage="Path containing Terraform files.")]
[ValidateNotNullOrEmpty()] # Specifies that the parameter value cannot be $null and cannot be an empty string "".
[string]$Path=$pwd.Path,
# Don't forget a comma between parameters.
[Parameter(Mandatory=$false,
ValueFromPipeline=$true,
Position=1,
HelpMessage="Recurse.")]
[switch]$Recurse
)
if ( Test-Path -Path $Path -PathType Container ) {
$TerraformDirectories = Get-ChildItem -Path $Path -Attributes Directory -Recurse:$Recurse | Where-Object -FilterScript { $_.FullName -notlike "*.terraform*" }
Write-Host "Formatting Terraform files in $Path..."
Start-Process terraform -ArgumentList "fmt" -WorkingDirectory $Path -WindowStyle Hidden
ForEach ($TFDir in $TerraformDirectories) {
Write-Host "Formatting Terraform files in $($TFDir.FullName)..."
Start-Process terraform -ArgumentList "fmt" -WorkingDirectory $TFDir.FullName -WindowStyle Hidden
}
Write-Host "Terraform files formatted successfully." -ForegroundColor Green
} else {
Write-Host "The specified path does not exist: $Path" -ForegroundColor Red
}
}