From 05b45304a710c855a4c3d9e19e2ce4db35ddbd03 Mon Sep 17 00:00:00 2001 From: Dandaedre Date: Sat, 17 Oct 2026 09:53:32 +0200 Subject: [PATCH] Allow automatic installer machine selection --- install.ps1 | 14 +++++++++++--- tests/Installer.Tests.ps1 | 19 +++++++++++++++++++ 2 files changed, 30 insertions(+), 3 deletions(-) diff --git a/install.ps1 b/install.ps1 index f55c26a..964bb13 100644 --- a/install.ps1 +++ b/install.ps1 @@ -1,8 +1,8 @@ #requires -Version 5.1 [CmdletBinding()] param( - [ValidatePattern('^[a-z0-9][a-z0-9-]*$')][string]$Machine = $env:CONFIG_CENTRAL_MACHINE, - [ValidatePattern('^[A-Za-z0-9._/-]+$')][string]$Branch = $env:CONFIG_CENTRAL_BRANCH, + [string]$Machine = $env:CONFIG_CENTRAL_MACHINE, + [string]$Branch = $env:CONFIG_CENTRAL_BRANCH, [switch]$SkipApply ) @@ -20,7 +20,15 @@ if ([Environment]::OSVersion.Platform -ne [PlatformID]::Win32NT) { if ([string]::IsNullOrWhiteSpace($env:TEMP) -or [string]::IsNullOrWhiteSpace($env:LOCALAPPDATA)) { throw 'The interactive Windows user profile is unavailable.' } -if ([string]::IsNullOrWhiteSpace($Branch)) { $Branch = 'main' } +if ([string]::IsNullOrWhiteSpace($Branch)) { + $Branch = 'main' +} +elseif ($Branch -notmatch '^[A-Za-z0-9._/-]+$') { + throw "Invalid branch name '$Branch'." +} +if (-not [string]::IsNullOrWhiteSpace($Machine) -and $Machine -notmatch '^[a-z0-9][a-z0-9-]*$') { + throw "Invalid machine name '$Machine'." +} if ($env:CONFIG_CENTRAL_SKIP_APPLY -eq '1') { $SkipApply = $true } function Get-SavedMachine { diff --git a/tests/Installer.Tests.ps1 b/tests/Installer.Tests.ps1 index e2a05ef..859766e 100644 --- a/tests/Installer.Tests.ps1 +++ b/tests/Installer.Tests.ps1 @@ -39,4 +39,23 @@ Describe 'One-stop installer machine selection' { Select-ConfigCentralMachine -Requested '' -Saved '' -Available @('example-desktop') | Should -Be 'example-desktop' } + + It 'allows unset environment defaults through parameter binding' { + $previousMachine = $env:CONFIG_CENTRAL_MACHINE + $previousBranch = $env:CONFIG_CENTRAL_BRANCH + try { + $env:CONFIG_CENTRAL_MACHINE = $null + $env:CONFIG_CENTRAL_BRANCH = $null + $parameterProbe = [scriptblock]::Create( + $installerAst.ParamBlock.Extent.Text + "`n@{ Machine = `$Machine; Branch = `$Branch }" + ) + $bound = & $parameterProbe + $bound.Machine | Should -BeNullOrEmpty + $bound.Branch | Should -BeNullOrEmpty + } + finally { + $env:CONFIG_CENTRAL_MACHINE = $previousMachine + $env:CONFIG_CENTRAL_BRANCH = $previousBranch + } + } }