config-central/cc.ps1

71 lines
3.2 KiB
PowerShell

[CmdletBinding()]
param(
[Parameter(Position = 0)][ValidateSet('doctor', 'plan', 'apply', 'sync', 'update-software', 'software-update', 'status', 'configs', 'capture', 'restore', 'schedule', 'unschedule', 'menu')][string]$Command = 'menu',
[string]$Machine,
[string]$Config,
[string]$OutputPath,
[switch]$ConfigsOnly,
[switch]$ForceConfigs,
[switch]$Json,
[string]$DataRoot,
[string]$RepositoryRoot = $PSScriptRoot,
[switch]$FromSnapshot,
[string]$Revision
)
# Kept identical in standalone entrypoints; verified by Output.Tests.ps1.
function Write-ConfigCentralMessage {
param([string]$Message, [ValidateSet('Info', 'Success', 'Warning', 'Error')][string]$Level = 'Info')
$color = switch ($Level) {
'Success' { 'Green' }
'Warning' { 'Yellow' }
'Error' { 'Red' }
default { 'Cyan' }
}
$redirected = if ($Level -eq 'Error') { [Console]::IsErrorRedirected } else { [Console]::IsOutputRedirected }
$useColor = -not $redirected -and $null -eq [Environment]::GetEnvironmentVariable('NO_COLOR')
if ($Level -eq 'Error') {
$previous = $null
try {
if ($useColor) { $previous = [Console]::ForegroundColor; [Console]::ForegroundColor = $color }
[Console]::Error.WriteLine($Message)
}
finally { if ($null -ne $previous) { [Console]::ForegroundColor = $previous } }
}
elseif ($useColor) { Write-Host $Message -ForegroundColor $color }
else { Write-Host $Message }
}
Set-StrictMode -Version Latest
$ErrorActionPreference = 'Stop'
try {
$Command = if ($Command -eq 'software-update') { 'update-software' } else { $Command }
foreach ($module in @('State', 'Manifest', 'Repository', 'Packages', 'Adapters', 'Configs', 'Elevation', 'Scheduling', 'Core')) {
$path = Join-Path $PSScriptRoot "src/$module.psm1"
if (Test-Path -LiteralPath $path) { Import-Module $path -Force }
}
$paths = Get-ConfigCentralPaths -DataRoot $DataRoot
Initialize-ConfigCentralData $paths
$settings = Read-JsonState -Path $paths.Settings -Default ([ordered]@{})
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.'
}
if ($Command -eq 'menu') {
Show-ConfigCentralMenu -Machine $Machine -DataRoot $DataRoot -RepositoryRoot $RepositoryRoot
exit 0
}
$result = Invoke-ConfigCentral -Command $Command -Machine $Machine -Config $Config -ConfigsOnly:$ConfigsOnly -ForceConfigs:$ForceConfigs -OutputPath $OutputPath -Paths $paths -RepositoryRoot $RepositoryRoot -FromSnapshot:$FromSnapshot -Revision $Revision -ShowProgress:(-not $Json)
if ($Json) { $result | ConvertTo-Json -Depth 32 }
else { Write-ConfigCentralResult $result }
exit ([int]$result.exitCode)
}
catch {
$errorResult = [ordered]@{ command = $Command; succeeded = $false; exitCode = 2; error = $_.Exception.Message }
if ($Json) { $errorResult | ConvertTo-Json -Depth 8 } else { Write-ConfigCentralMessage -Level Error -Message ("Config Central could not complete: " + $errorResult.error) }
exit 2
}