Private/Read-RaidinessIntakeInteractive.ps1
|
<# The questionnaire in the console. Sixty-nine questions is a lot to face at once, so this goes section by section and every one of them is skippable: Enter keeps what is there, "s" leaves the rest of the section, "?" prints the explanation. Nothing here insists on an answer, because an unanswered question has an honest result already -- the check says "not measured". #> function Read-RaidinessIntakeInteractive { [CmdletBinding()] [OutputType([hashtable])] param( [Parameter(Mandatory = $true)] [object[]] $Questions, [Parameter(Mandatory = $true)] [hashtable] $Answers ) $result = @{} foreach ($key in $Answers.Keys) { $result[$key] = $Answers[$key] } Write-Host '' Write-Host 'Intake — Enter keeps the current answer, "s" skips the rest of a section, "?" explains.' -ForegroundColor Cyan $sections = $Questions | Group-Object -Property SectionTitle foreach ($section in $sections) { Write-Host '' Write-Host "── $($section.Name)" -ForegroundColor Cyan foreach ($question in $section.Group) { $current = $null foreach ($candidate in @($question.Key) + $question.Aliases) { if ($null -eq $current -and $result.ContainsKey($candidate)) { $current = $result[$candidate] } } $hint = switch ($question.Type) { 'boolean' { ' (yes/no)' } 'number' { ' (a number)' } 'choice' { " ($($question.Options -join ' / '))" } default { '' } } $shown = $null -ne $current ? " [$current]" : '' if ($question.Personal) { $hint += ' — a name; hashed in anything sent to a model' } $answer = Read-Host "$($question.Question)$hint$shown" $answer = "$answer".Trim() if ($answer -eq '') { continue } if ($answer -eq 's') { break } if ($answer -eq '?') { Write-Host " $($question.Help ? $question.Help : 'No further explanation recorded for this question.')" -ForegroundColor DarkGray $answer = "$(Read-Host "$($question.Question)$hint$shown")".Trim() if ($answer -eq '' -or $answer -eq 's') { continue } } try { $result[$question.Key] = Assert-RaidinessIntakeAnswer -Question $question -Value $answer } catch { Write-Warning $_.Exception.Message } } } $result } |