-
Notifications
You must be signed in to change notification settings - Fork 0
/
Terraform-Release.ps1
207 lines (181 loc) · 6.39 KB
/
Terraform-Release.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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
param(
[string]$VariablesInFile = "./variables.tf",
[string]$VariablesOutFile = "./variables.tf",
[string]$OutputsInFile = "./outputs.tf",
[string]$OutputsOutFile = "./outputs.tf",
[string]$GitTag = "2.0.1",
[string]$GitCommitMessage = "Update code",
[bool]$SortInputs = $true,
[bool]$SortOutputs = $true,
[bool]$GitRelease = $true,
[bool]$FormatTerraform = $true,
[bool]$GenerateNewReadme = $true
)
$CurrentDirectory = (Get-Location).Path
$ErrorOccurred = $false
function Format-Terraform {
try {
$terraformPath = Get-Command terraform -ErrorAction Stop
Write-Host "Success: Terraform found at: $($terraformPath.Source)" -ForegroundColor Green
terraform fmt -recursive
Write-Host "Success: Terraform formatted using terraform fmt" -ForegroundColor Green
}
catch {
Write-Error "Error: Terraform is not installed or not in PATH, or terraform fmt failed due to syntax rror. Exiting."
exit 1
}
}
function Git-Release {
param (
[string]$GitTag,
[string]$GitCommitMessage
)
try {
$gitPath = Get-Command git -ErrorAction Stop
Write-Host "Success: Git found at: $($gitPath.Source)" -ForegroundColor Green
Write-Information "Info: Attemting git release"
git add --all
git commit -m "${GitCommitMessage}"
git push
git tag $GitTag --force
git push --tags --force
}
catch {
Write-Error "Error: Git is not installed or not in PATH, or git release failed due to syntax rror. Exiting."
exit 1
}
}
function Read-TerraformFile {
param (
[string]$Filename
)
if (Test-Path $Filename) {
try {
return Get-Content $Filename -Raw
}
catch {
Write-Error "Error: Error reading file '$Filename': $_"
$Global:ErrorOccurred = $true
return $null
}
}
else {
Write-Error "File not found: $Filename"
$Global:ErrorOccurred = $true
return $null
}
}
function Write-TerraformFile {
param (
[string]$Filename,
[string]$FileContent
)
if ($null -ne $FileContent) {
try {
$FileContent | Set-Content $Filename
}
catch {
Write-Error "Error: Error writing to file '$Filename': $_"
$Global:ErrorOccurred = $true
}
}
else {
Write-Error "Error: No content to write to $Filename"
$Global:ErrorOccurred = $true
}
}
function Sort-TerraformOutputs {
param (
[string]$OutputsContent
)
try {
$pattern = 'output\s+"[^"]+"\s+\{[\s\S]*?\n\}'
$outputs = Select-String -Pattern $pattern -InputObject $OutputsContent -AllMatches | ForEach-Object { $_.Matches } | ForEach-Object { $_.Value }
return ($outputs | Sort-Object { [regex]::Match($_, 'output\s+"([^"]+)"').Groups[1].Value }) -join "`n`n"
}
catch {
Write-Error "Error: Error sorting Terraform outputs: $_"
$Global:ErrorOccurred = $true
return $null
}
}
function Sort-TerraformVariables {
param (
[string]$VariablesContent
)
try {
$pattern = 'variable\s+"[^"]+"\s+\{[\s\S]*?\n\}'
$variables = Select-String -Pattern $pattern -InputObject $VariablesContent -AllMatches | ForEach-Object { $_.Matches } | ForEach-Object { $_. Value }
return ($variables | Sort-Object { [regex]::Match($_, 'variable\s+"([^"]+)"').Groups[1].Value }) -join "`n`n"
}
catch {
Write-Error "Error: Error sorting Terraform variables: $_"
$Global:ErrorOccurred = $true
return $null
}
}
function Update-ReadmeWithTerraformDocs {
try {
$terraformDocsPath = Get-Command terraform-docs -ErrorAction Stop
Write-Host "Success: Terraform-docs found at: $($terraformDocsPath.Source)" -ForegroundColor Green
}
catch {
Write-Error "Error: Terraform-docs is not installed or not in PATH, Skipping README generation."
}
$buildFile = ""
if (Test-Path "./build.tf") {
$buildFile = "./build.tf"
Write-Host "Success: ${buildFile} found" -ForegroundColor Green
} elseif (Test-Path "./main.tf") {
$buildFile = "./main.tf"
Write-Host "Success: ${buildFile} found" -ForegroundColor Green
}
if ($buildFile -ne "") {
Set-Content "README.md" -Value '```hcl'
Get-Content $buildFile | Add-Content "README.md"
Add-Content "README.md" -Value '```'
try {
$terraformDocs = terraform-docs markdown .
$terraformDocs | Add-Content "README.md"
} catch {
Write-Error "Error: Failed to generate or append terraform-docs markdown. Make sure terraform-docs is installed and in PATH."
}
} else {
Write-Warning "Warning: Not a build directory, no build.tf or main.tf found"
}
}
if ($FormatTerraform) {
Format-Terraform
}
if ($SortInputs) {
$VariablesContent = Read-TerraformFile -Filename $VariablesInFile
if ($VariablesContent) {
$SortedVariablesContent = Sort-TerraformVariables -VariablesContent $VariablesContent
if ($SortedVariablesContent) {
Write-TerraformFile -Filename $VariablesOutFile -FileContent $SortedVariablesContent
Write-Host "Success: Sorted Terraform variables written to $VariablesOutFile" -ForegroundColor Green
}
}
}
if ($SortOutputs) {
$OutputsContent = Read-TerraformFile -Filename $OutputsInFile
if ($OutputsContent) {
$SortedOutputsContent = Sort-TerraformOutputs -OutputsContent $OutputsContent
if ($SortedOutputsContent) {
Write-TerraformFile -Filename $OutputsOutFile -FileContent $SortedOutputsContent
Write-Host "Success: Sorted Terraform outputs written to $OutputsOutFile" -ForegroundColor Green
}
}
}
if ($GenerateNewReadme) {
Update-ReadmeWithTerraformDocs
}
if ($GitRelease) {
Git-Release -GitTag "${GitTag}" -GitCommitMessage "${GitCommitMessage}"
}
if ($ErrorOccurred) {
Write-Error "Error: The script completed with errors. Check the error messages above."
}
else {
Write-Host "Success: The script completed successfully without errors." -ForegroundColor Green
}