-
Notifications
You must be signed in to change notification settings - Fork 51
Expand file tree
/
Copy pathRequest-SpecialInput.ps1
More file actions
95 lines (79 loc) · 4.33 KB
/
Request-SpecialInput.ps1
File metadata and controls
95 lines (79 loc) · 4.33 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
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
function Request-SpecialInput {
[CmdletBinding(SupportsShouldProcess = $true)]
param (
[Parameter(Mandatory = $false)]
[string] $type,
[Parameter(Mandatory = $false)]
[PSCustomObject] $starterConfig,
[Parameter(Mandatory = $false)]
[PSCustomObject] $bootstrapModules,
[Parameter(Mandatory = $false)]
[PSCustomObject] $userInputOverrides = $null
)
if ($PSCmdlet.ShouldProcess("ALZ-Terraform module configuration", "modify")) {
$result = ""
$options = @()
$aliasOptions = @()
$typeDescription = ""
if($type -eq "iac") {
$options += @{ key = "bicep"; name = "Bicep"; description = "Bicep" }
$options += @{ key = "terraform"; name = "Terraform"; description = "Terraform" }
$typeDescription = "Infrastructure as Code (IaC) language"
}
if($type -eq "bootstrap") {
if($bootstrapModules.PsObject.Properties.Name.Count -eq 0) {
$options += @{ key = "azuredevops"; name = "Azure DevOps"; description = "Azure DevOps" }
$options += @{ key = "github"; name = "GitHub"; description = "GitHub" }
$aliasOptions += @{ key = "alz_azuredevops"; name = "Azure DevOps"; description = "Azure DevOps" }
$aliasOptions += @{ key = "alz_github"; name = "GitHub"; description = "GitHub" }
} else {
foreach ($bootstrapModule in $bootstrapModules.PsObject.Properties) {
$options += @{ key = $bootstrapModule.Name; name = $bootstrapModule.Value.short_name; description = $bootstrapModule.Value.description }
foreach($alias in $bootstrapModule.Value.aliases) {
$aliasOptions += @{ key = $alias; name = $bootstrapModule.Value.short_name; description = $bootstrapModule.Value.description }
}
}
}
$typeDescription = "bootstrap module"
}
if($type -eq "starter") {
foreach($starter in $starterConfig.starter_modules.PsObject.Properties) {
if($starter.Name -eq $starterPipelineFolder) {
continue
}
$options += @{ key = $starter.Name; name = $starter.Value.short_name; description = $starter.Value.description }
}
$typeDescription = "starter module"
}
if($null -ne $userInputOverrides) {
$userInputOverride = $userInputOverrides.PSObject.Properties | Where-Object { $_.Name -eq $type }
if($null -ne $userInputOverride) {
$result = $userInputOverride.Value
if($options.key -notcontains $result -and $aliasOptions.key -notcontains $result) {
Write-InformationColored "The $typeDescription '$result' that you have selected does not exist. Please try again with a valid $typeDescription..." -ForegroundColor Red -InformationAction Continue
throw "The $typeDescription '$result' that you have selected does not exist. Please try again with a valid $typeDescription..."
}
return $result
}
}
# Add the options to the choices array
$choices = @()
$usedLetters = @()
foreach($option in $options) {
$letterIndex = 0
Write-Verbose "Checking for used letters in '$($option.name)'. Used letters: $usedLetters"
while($usedLetters -contains $option.name[$letterIndex].ToString().ToLower()) {
$letterIndex++
}
$usedLetters += $option.name[$letterIndex].ToString().ToLower()
$option.name = $option.name.Insert($letterIndex, "&")
$choices += New-Object System.Management.Automation.Host.ChoiceDescription $option.name, $option.description
}
$message = "Please select the $typeDescription you would like to use."
$title = "Choose $typeDescription"
$resultIndex = $host.ui.PromptForChoice($title, $message, $choices, 0)
$result = $options[$resultIndex].key
Write-InformationColored "You selected '$result'. Continuing with deployment..." -ForegroundColor Green -InformationAction Continue
return $result
}
}