config-central/cc.ps1

46 lines
1.9 KiB
PowerShell

[CmdletBinding()]
param(
[Parameter(Position = 0)][ValidateSet('doctor', 'plan', 'apply', 'sync', 'status', 'configs', 'restore', 'schedule', 'unschedule', 'menu')][string]$Command = 'menu',
[string]$Machine,
[string]$Config,
[switch]$ConfigsOnly,
[switch]$ForceConfigs,
[switch]$Json,
[string]$DataRoot,
[string]$RepositoryRoot = $PSScriptRoot,
[switch]$FromSnapshot,
[string]$Revision
)
Set-StrictMode -Version Latest
$ErrorActionPreference = 'Stop'
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 }
}
try {
$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 -Paths $paths -RepositoryRoot $RepositoryRoot -FromSnapshot:$FromSnapshot -Revision $Revision
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-Error $errorResult.error }
exit 2
}