From fde35a661c80cd7ae8407be53af046a2cdd550c0 Mon Sep 17 00:00:00 2001 From: Dandaedre Date: Sat, 17 Oct 2026 11:25:55 +0200 Subject: [PATCH] Add one-command web config updates --- TASKS.md | 4 +++ install.ps1 | 2 +- tests/UpdateConfigs.Tests.ps1 | 53 +++++++++++++++++++++++++++++ update-configs.ps1 | 63 +++++++++++++++++++++++++++++++++++ 4 files changed, 121 insertions(+), 1 deletion(-) create mode 100644 tests/UpdateConfigs.Tests.ps1 create mode 100644 update-configs.ps1 diff --git a/TASKS.md b/TASKS.md index 69811bc..7b8235f 100644 --- a/TASKS.md +++ b/TASKS.md @@ -116,3 +116,7 @@ open until it is run on disposable machines with the required account setups. - [x] Add safe Nextcloud preconfiguration without committing account credentials. - [x] Manage a marked block in the Windows hosts file through the elevated worker. - [x] Document how to capture and customize the application-specific values. + +## Simple web workflow + +- [x] Add a one-command config-only updater using the saved machine and a fresh repository revision, with failure and cleanup tests. diff --git a/install.ps1 b/install.ps1 index 964bb13..d6d9583 100644 --- a/install.ps1 +++ b/install.ps1 @@ -132,7 +132,7 @@ try { Write-Host '' Write-Host "Config Central is ready for '$Machine'." - Write-Host 'Use cc sync for normal updates, or rerun this installer to repair the setup.' + Write-Host 'Run the web update-configs.ps1 command for config updates, or rerun this installer to repair setup.' } finally { Remove-Item -LiteralPath $bootstrapPath -Force -ErrorAction SilentlyContinue diff --git a/tests/UpdateConfigs.Tests.ps1 b/tests/UpdateConfigs.Tests.ps1 new file mode 100644 index 0000000..bd0f584 --- /dev/null +++ b/tests/UpdateConfigs.Tests.ps1 @@ -0,0 +1,53 @@ +BeforeAll { + $tokens = $null; $errors = $null + $ast = [System.Management.Automation.Language.Parser]::ParseFile("$PSScriptRoot/../update-configs.ps1", [ref]$tokens, [ref]$errors) + if ($errors.Count) { throw $errors[0] } + $fn = $ast.Find({ param($node) $node -is [System.Management.Automation.Language.FunctionDefinitionAst] -and $node.Name -eq 'Invoke-ConfigUpdate' }, $true) + . ([scriptblock]::Create($fn.Extent.Text)) + function git.exe { } + function pwsh.exe { } +} +Describe 'Web config updater' { + BeforeEach { + $script:oldTemp = $env:TEMP + $env:TEMP = $TestDrive + $script:gitFailure = $false + $script:applyFailure = $false + $script:childArguments = @() + '{"machine":"desktop","branch":"main","repositoryUrl":"https://example.test/config.git"}' | Set-Content "$TestDrive/settings.json" + Mock Get-Command { [pscustomobject]@{ Source = $Name } } -ParameterFilter { $Name -in @('git.exe', 'pwsh.exe') } + Mock Test-Path { $true } -ParameterFilter { $LiteralPath -in @('git.exe', 'pwsh.exe') } + Mock git.exe { + $global:LASTEXITCODE = 0 + if ($args[0] -eq 'clone') { + New-Item -ItemType Directory -Path $args[-1] -Force | Out-Null + if ($script:gitFailure) { $global:LASTEXITCODE = 1 } + } + if ($args -contains 'rev-parse') { 'a' * 40 } + } + Mock pwsh.exe { + $script:childArguments = $args + $global:LASTEXITCODE = if ($script:applyFailure) { 5 } else { 0 } + } + } + AfterEach { $env:TEMP = $script:oldTemp } + It 'applies only configs from the downloaded revision and removes its temporary checkout' { + Invoke-ConfigUpdate -DataRoot $TestDrive + $script:childArguments | Should -Contain '-ConfigsOnly' + $script:childArguments | Should -Contain ('a' * 40) + $script:childArguments | Should -Not -Contain '-ForceConfigs' + @(Get-ChildItem $TestDrive -Directory -Filter 'config-central-update-*').Count | Should -Be 0 + } + It 'does not apply after a failed download' { + $script:gitFailure = $true + { Invoke-ConfigUpdate -DataRoot $TestDrive } | Should -Throw '*download failed*' + Should -Invoke pwsh.exe -Times 0 + @(Get-ChildItem $TestDrive -Directory -Filter 'config-central-update-*').Count | Should -Be 0 + } + It 'surfaces partial failures and forwards only an explicit force request' { + $script:applyFailure = $true + { Invoke-ConfigUpdate -DataRoot $TestDrive -ForceConfigs } | Should -Throw '*exit 5*' + $script:childArguments | Should -Contain '-ForceConfigs' + @(Get-ChildItem $TestDrive -Directory -Filter 'config-central-update-*').Count | Should -Be 0 + } +} diff --git a/update-configs.ps1 b/update-configs.ps1 new file mode 100644 index 0000000..2194fc8 --- /dev/null +++ b/update-configs.ps1 @@ -0,0 +1,63 @@ +#requires -Version 5.1 +[CmdletBinding()] +param( + [switch]$ForceConfigs +) + +function Invoke-ConfigUpdate { + param([string]$DataRoot, [switch]$ForceConfigs) + $settingsPath = Join-Path $DataRoot 'settings.json' + if (-not (Test-Path -LiteralPath $settingsPath -PathType Leaf)) { + throw 'Run install.ps1 once on this computer before updating configs.' + } + $settings = Get-Content -LiteralPath $settingsPath -Raw | ConvertFrom-Json + if ($settings.machine -notmatch '^[a-z0-9][a-z0-9-]*$') { throw 'Saved machine selection is invalid. Rerun setup.' } + if ($settings.branch -notmatch '^[A-Za-z0-9][A-Za-z0-9._/-]*$' -or $settings.branch.Contains('..')) { + throw 'Saved repository branch is invalid. Rerun setup.' + } + $repositoryUri = $null + if (-not [Uri]::TryCreate([string]$settings.repositoryUrl, [UriKind]::Absolute, [ref]$repositoryUri) -or $repositoryUri.Scheme -ne 'https') { + throw 'The web updater requires an HTTPS repository URL in settings.json.' + } + $git = Get-Command git.exe -ErrorAction SilentlyContinue + $pwsh = Get-Command pwsh.exe -ErrorAction SilentlyContinue + $gitPath = if ($git) { $git.Source } else { Join-Path $env:ProgramFiles 'Git\cmd\git.exe' } + $pwshPath = if ($pwsh) { $pwsh.Source } else { Join-Path $env:ProgramFiles 'PowerShell\7\pwsh.exe' } + if (-not (Test-Path -LiteralPath $gitPath) -or -not (Test-Path -LiteralPath $pwshPath)) { + throw 'Git or PowerShell 7 is missing. Rerun install.ps1 to repair setup.' + } + + $temporary = Join-Path $env:TEMP "config-central-update-$([guid]::NewGuid().ToString('N'))" + $previousGitPrompt = $env:GIT_TERMINAL_PROMPT + try { + $env:GIT_TERMINAL_PROMPT = '0' + Write-Host "Downloading latest configs for '$($settings.machine)' from '$($settings.branch)'..." + & $gitPath clone --quiet --depth 1 --single-branch --no-checkout --branch $settings.branch -- $settings.repositoryUrl $temporary + if ($LASTEXITCODE -ne 0) { throw 'Config download failed. Check the repository URL, network, and Git credentials.' } + $revision = & $gitPath -C $temporary rev-parse --verify 'HEAD^{commit}' + if ($LASTEXITCODE -ne 0 -or $revision -notmatch '^[0-9a-f]{40,64}$') { throw 'Could not resolve the downloaded config revision.' } + & $gitPath -C $temporary checkout --quiet --detach $revision + if ($LASTEXITCODE -ne 0) { throw 'Could not prepare the downloaded configs.' } + $arguments = @('-NoLogo', '-NoProfile', '-ExecutionPolicy', 'Bypass', '-File', (Join-Path $temporary 'cc.ps1'), + 'apply', '-ConfigsOnly', '-Machine', $settings.machine, '-DataRoot', $DataRoot, + '-RepositoryRoot', $temporary, '-Revision', $revision) + if ($ForceConfigs) { $arguments += '-ForceConfigs' } + & $pwshPath @arguments + if ($LASTEXITCODE -ne 0) { + throw "Config update did not fully complete (exit $LASTEXITCODE). Review the results above; backups and logs remain in '$DataRoot'." + } + Write-Host 'Configs are up to date.' + } + finally { + $env:GIT_TERMINAL_PROMPT = $previousGitPrompt + if (Test-Path -LiteralPath $temporary) { Remove-Item -LiteralPath $temporary -Recurse -Force } + } +} + +# Keep strict mode and error preferences local to this invocation. +& { + Set-StrictMode -Version 2.0 + $ErrorActionPreference = 'Stop' + if ([Environment]::OSVersion.Platform -ne [PlatformID]::Win32NT) { throw 'Config updates run only on Windows.' } + Invoke-ConfigUpdate -DataRoot (Join-Path $env:LOCALAPPDATA 'ConfigCentral') -ForceConfigs:$ForceConfigs +}