Support ordered state dictionaries

This commit is contained in:
Dandaedre 2026-10-16 13:54:05 +02:00
parent 825fc5d3f0
commit fefc22c3ac
4 changed files with 10 additions and 10 deletions

2
cc.ps1
View File

@ -24,7 +24,7 @@ try {
$paths = Get-ConfigCentralPaths -DataRoot $DataRoot
Initialize-ConfigCentralData $paths
$settings = Read-JsonState -Path $paths.Settings -Default ([ordered]@{})
if ([string]::IsNullOrWhiteSpace($Machine) -and $settings.ContainsKey('machine')) { $Machine = [string]$settings.machine }
if ([string]::IsNullOrWhiteSpace($Machine) -and $settings.Contains('machine')) { $Machine = [string]$settings.machine }
if ($Command -ne 'doctor' -and $Command -ne 'menu' -and [string]::IsNullOrWhiteSpace($Machine)) {
throw 'No machine selected. Pass -Machine or run bootstrap.ps1.'
}

View File

@ -3,9 +3,9 @@ Set-StrictMode -Version Latest
function Get-UserPathMap {
param($Settings)
$map = [ordered]@{}
$stored = if ($null -ne $Settings -and $Settings.ContainsKey('userPaths')) { $Settings.userPaths } else { @{} }
$stored = if ($null -ne $Settings -and $Settings.Contains('userPaths')) { $Settings.userPaths } else { @{} }
foreach ($name in @('APPDATA', 'LOCALAPPDATA', 'USERPROFILE', 'PROGRAMDATA')) {
$value = if ($stored.ContainsKey($name)) { [string]$stored[$name] } else { [Environment]::GetEnvironmentVariable($name) }
$value = if ($stored.Contains($name)) { [string]$stored[$name] } else { [Environment]::GetEnvironmentVariable($name) }
if ([string]::IsNullOrWhiteSpace($value)) { throw "Required path variable %$name% is unavailable." }
$map[$name] = $value
}
@ -41,7 +41,7 @@ function Get-FileHashValue {
function Get-ConfigPlan {
param([Parameter(Mandatory)]$Configs, [Parameter(Mandatory)][string]$RepositoryRoot, [Parameter(Mandatory)]$State, [Parameter(Mandatory)]$Settings)
if (-not $State.ContainsKey('configs')) { $State.configs = [ordered]@{} }
if (-not $State.Contains('configs')) { $State.configs = [ordered]@{} }
$pathMap = Get-UserPathMap -Settings $Settings
$destinations = [System.Collections.Generic.HashSet[string]]::new([System.StringComparer]::OrdinalIgnoreCase)
$plan = [System.Collections.Generic.List[object]]::new()
@ -50,7 +50,7 @@ function Get-ConfigPlan {
$target = Resolve-ConfigTarget -Target $config.target -PathMap $pathMap
if (-not $destinations.Add($target)) { throw "Multiple configs resolve to '$target'." }
$sourceHash = Get-FileHashValue -Path $source
$record = if ($State.configs.ContainsKey($config.name)) { $State.configs[$config.name] } else { $null }
$record = if ($State.configs.Contains($config.name)) { $State.configs[$config.name] } else { $null }
$targetExists = Test-Path -LiteralPath $target -PathType Leaf
$targetHash = if ($targetExists) { Get-FileHashValue -Path $target } else { $null }
@ -95,7 +95,7 @@ function Invoke-ConfigOperation {
try {
Copy-Item -LiteralPath $Operation.source -Destination $temporary -Force
[System.IO.File]::Move($temporary, $Operation.target, $true)
if (-not $State.ContainsKey('configs')) { $State.configs = [ordered]@{} }
if (-not $State.Contains('configs')) { $State.configs = [ordered]@{} }
$State.configs[$Operation.name] = [ordered]@{
target = $Operation.target
deployedHash = $Operation.sourceHash
@ -117,7 +117,7 @@ function Restore-ConfigBackup {
$folder = Join-Path $Paths.Backups $Name
$backup = Get-ChildItem -LiteralPath $folder -Filter '*.bak' -File -ErrorAction SilentlyContinue | Sort-Object LastWriteTimeUtc -Descending | Select-Object -First 1
if ($null -eq $backup) { throw "No backup exists for config '$Name'." }
if (-not $State.ContainsKey('configs') -or -not $State.configs.ContainsKey($Name)) { throw "No managed state exists for config '$Name'." }
if (-not $State.Contains('configs') -or -not $State.configs.Contains($Name)) { throw "No managed state exists for config '$Name'." }
$target = [string]$State.configs[$Name].target
$current = [pscustomobject]@{ name = $Name; target = $target }
[void](Backup-ConfigTarget -Operation $current -Paths $Paths)

View File

@ -9,7 +9,7 @@ function Get-OperationPlan {
param([string]$Machine, [string]$RepositoryRoot, $Paths, [switch]$ConfigsOnly)
$settings = Read-JsonState -Path $Paths.Settings -Default ([ordered]@{})
$state = Read-JsonState -Path $Paths.State -Default ([ordered]@{ schemaVersion = 1; configs = [ordered]@{} })
if (-not $state.ContainsKey('configs')) { $state.configs = [ordered]@{} }
if (-not $state.Contains('configs')) { $state.configs = [ordered]@{} }
$effective = Resolve-MachineManifest -RepositoryRoot $RepositoryRoot -Machine $Machine
$packagePlan = @()
if (-not $ConfigsOnly) {
@ -143,7 +143,7 @@ function Invoke-ConfigCentral {
if ($Command -eq 'status') {
$drift = @($result.operations | Where-Object action -notin @('None', 'Warning'))
if ($drift.Count -gt 0) { $result.succeeded = $false; $result.exitCode = 4 }
$result | Add-Member -NotePropertyName lastRun -NotePropertyValue $(if ($plan.state.ContainsKey('lastRun')) { $plan.state.lastRun } else { $null })
$result | Add-Member -NotePropertyName lastRun -NotePropertyValue $(if ($plan.state.Contains('lastRun')) { $plan.state.lastRun } else { $null })
}
return [pscustomobject]$result
}

View File

@ -28,7 +28,7 @@ function Test-RepositoryDirty {
function Resolve-RemoteRevision {
param([Parameter(Mandatory)][string]$RepositoryRoot, [Parameter(Mandatory)]$Settings)
if (-not $Settings.ContainsKey('branch') -or [string]::IsNullOrWhiteSpace([string]$Settings.branch)) { throw 'No repository branch is configured.' }
if (-not $Settings.Contains('branch') -or [string]::IsNullOrWhiteSpace([string]$Settings.branch)) { throw 'No repository branch is configured.' }
$branch = [string]$Settings.branch
if ($branch -notmatch '^[A-Za-z0-9._/-]+$' -or $branch.Contains('..')) { throw "Unsafe branch name '$branch'." }
if (Test-RepositoryDirty -RepositoryRoot $RepositoryRoot) { throw 'Repository has local changes; commit or remove them before sync.' }