Migrate host config from flat key-value to YAML format

This commit is contained in:
2026-05-13 15:11:05 +08:00
parent 1904288e9b
commit c810f4ef6c
6 changed files with 172 additions and 104 deletions
+67 -29
View File
@@ -28,34 +28,68 @@ Examples:
"@
}
# ============================================================
# Minimal YAML parser (handles nested key: value, no arrays)
# ============================================================
function Parse-Yaml($Path) {
$result = @{}
$stack = New-Object System.Collections.ArrayList
$null = $stack.Add(@{ node = $result; indent = -1; key = $null })
foreach ($line in (Get-Content $Path -Encoding UTF8)) {
$trimmed = $line.TrimEnd()
if ($trimmed -match '^\s*#' -or $trimmed -match '^\s*$') { continue }
if ($trimmed -notmatch '^(\s*)([^:]+):\s*(.*)') { continue }
$indent = $matches[1].Length
$key = $matches[2].Trim()
$value = $matches[3].Trim()
# Pop stack until we find the parent with smaller indent
while ($stack.Count -gt 1 -and $stack[-1].indent -ge $indent) {
$null = $stack.RemoveAt($stack.Count - 1)
}
$parent = $stack[-1].node
if ($value) {
$parent[$key] = $value
} else {
$child = @{}
$parent[$key] = $child
$null = $stack.Add(@{ node = $child; indent = $indent; key = $key })
}
}
return $result
}
# ============================================================
# Load host config
# ============================================================
function Load-Host($Name) {
$HostFile = Join-Path $ClawRoot "hosts\$Name"
$HostFile = Join-Path $ClawRoot "hosts\$Name.yml"
if (-not (Test-Path $HostFile)) {
Write-Host "ERROR: host '$Name' not found (hosts\$Name)" -ForegroundColor Red
Write-Host "Available hosts:" -ForegroundColor Yellow
List-Hosts
# fallback: try without .yml extension (old format)
$HostFile = Join-Path $ClawRoot "hosts\$Name"
if (-not (Test-Path $HostFile)) {
Write-Host "ERROR: host '$Name' not found (hosts\$Name.yml)" -ForegroundColor Red
Write-Host "Available hosts:" -ForegroundColor Yellow
List-Hosts
exit 1
}
}
$cfg = Parse-Yaml $HostFile
$h = $cfg.host
if (-not $h) {
Write-Host "ERROR: hosts\$Name missing 'host:' section" -ForegroundColor Red
exit 1
}
$script:HostIP = $null
$script:HostUser = "root"
$script:HostPort = "22"
$script:HostKey = $null
Get-Content $HostFile | ForEach-Object {
$line = $_.Trim()
if ($line -match '^\s*#' -or $line -eq '') { return }
if ($line -match '^HOST=(.+)$') { $script:HostIP = $matches[1].Trim() }
if ($line -match '^USER=(.+)$') { $script:HostUser = $matches[1].Trim() }
if ($line -match '^PORT=(.+)$') { $script:HostPort = $matches[1].Trim() }
if ($line -match '^KEY=(.+)$') { $script:HostKey = $matches[1].Trim() }
}
$script:HostIP = if ($h.ip) { $h.ip } else { $null }
$script:HostUser = if ($h.user) { $h.user } else { "root" }
$script:HostPort = if ($h.port) { $h.port } else { "22" }
$script:HostKey = if ($h.key) { $h.key } else { $null }
if (-not $HostIP) {
Write-Host "ERROR: hosts\$Name missing HOST=" -ForegroundColor Red
Write-Host "ERROR: hosts\${Name}: host.ip is required" -ForegroundColor Red
exit 1
}
@@ -86,19 +120,23 @@ function Build-SshArgs {
function List-Hosts {
$hostDir = Join-Path $ClawRoot "hosts"
if (-not (Test-Path $hostDir) -or @(Get-ChildItem $hostDir).Count -eq 0) {
Write-Host " (no hosts yet, create a file in hosts\)"
Write-Host " (no hosts yet, create a .yml file in hosts\)"
return
}
Get-ChildItem $hostDir | ForEach-Object {
$name = $_.Name
$h = ""; $u = "root"; $p = "22"
Get-Content $_.FullName | ForEach-Object {
$line = $_.Trim()
if ($line -match '^HOST=(.+)$') { $h = $matches[1].Trim() }
if ($line -match '^USER=(.+)$') { $u = $matches[1].Trim() }
if ($line -match '^PORT=(.+)$') { $p = $matches[1].Trim() }
Get-ChildItem $hostDir -Filter *.yml | ForEach-Object {
$name = $_.BaseName
try {
$cfg = Parse-Yaml $_.FullName
$h = $cfg.host
if ($h) {
$ip = if ($h.ip) { $h.ip } else { "?" }
$u = if ($h.user) { $h.user } else { "root" }
$p = if ($h.port) { $h.port } else { "22" }
Write-Host (" {0,-25} {1}@{2}:{3}" -f $name, $u, $ip, $p)
}
} catch {
Write-Host (" {0,-25} (parse error)" -f $name)
}
Write-Host (" {0,-20} {1}@{2}:{3}" -f $name, $u, $h, $p)
}
}
@@ -169,7 +207,7 @@ function Invoke-Key($Action, $Name, $Src) {
$dest = Join-Path $ClawRoot "keys\$Name.pem"
Copy-Item $Src $dest -Force
Write-Host ">>> Key saved: keys\$Name.pem" -ForegroundColor Green
Write-Host ">>> In host file, use: KEY=keys\$Name.pem"
Write-Host ">>> In host file, set: host.key: keys\$Name.pem"
}
"list" {
Write-Host "Registered keys:" -ForegroundColor Yellow