gitlink-cli/workflows/lib/common.psm1

93 lines
2.9 KiB
PowerShell

# Common utilities for gitlink-cli workflow scripts (PowerShell 5.1+)
$Script:GL = "gitlink-cli"
# -- Logging --
function Log-Step { param([string]$Msg) Write-Host "[STEP] $Msg" -ForegroundColor Blue }
function Log-Ok { param([string]$Msg) Write-Host "[ OK] $Msg" -ForegroundColor Green }
function Log-Warn { param([string]$Msg) Write-Host "[WARN] $Msg" -ForegroundColor Yellow }
function Log-Err { param([string]$Msg) Write-Host "[ ERR] $Msg" -ForegroundColor Red }
function Log-Info { param([string]$Msg) Write-Host "[INFO] $Msg" -ForegroundColor Cyan }
function Log-Title { param([string]$Msg) Write-Host ""; Write-Host "====== $Msg ======" -ForegroundColor White; Write-Host "" }
function Divider { Write-Host "------------------------------------------------" -ForegroundColor Cyan }
# -- Auth Check --
function Check-Auth {
if ($env:GITLINK_TOKEN) {
Log-Ok "GITLINK_TOKEN is set"
return
}
$status = & $Script:GL auth status 2>&1
$statusStr = $status -join " "
if ($statusStr -match "logged in") {
Log-Ok "Authenticated"
return
}
Log-Err "Not authenticated. Please login first:"
Log-Info " gitlink-cli auth login"
Log-Info ' $env:GITLINK_TOKEN = "your-private-token"'
exit 1
}
# -- CLI Wrapper --
function Invoke-GL {
param([string[]]$Args)
$output = & $Script:GL @Args --format json 2>&1
return ($output -join "`n")
}
function Invoke-GLCheck {
param([string[]]$Args)
$output = Invoke-GL $Args
try {
$json = $output | ConvertFrom-Json
if (-not $json.ok) {
$errMsg = if ($json.error.message) { $json.error.message } else { "unknown error" }
Log-Err "Command failed: $Script:GL $($Args -join ' ')"
Log-Err $errMsg
return $null
}
return $json
} catch {
Log-Err "Command failed (non-JSON): $Script:GL $($Args -join ' ')"
Log-Err $output
return $null
}
}
# -- JSON Helpers --
function Get-JsonOk {
param($Json)
return ($Json.ok -eq $true)
}
# -- Owner/Repo Detection --
function Detect-OwnerRepo {
$remote = git remote get-url origin 2>$null
if (-not $remote) {
Log-Err "No git remote 'origin' found. Use -Owner and -Repo flags."
exit 1
}
if ($remote -match "gitlink\.org\.cn[:/]([^/]+)/([^/.]+)") {
return @{ Owner = $Matches[1]; Repo = $Matches[2] }
}
Log-Err "Cannot parse owner/repo from remote: $remote"
exit 1
}
function Resolve-OwnerRepo {
param([string]$Owner, [string]$Repo)
if (-not $Owner -or -not $Repo) {
$detected = Detect-OwnerRepo
if (-not $Owner) { $Owner = $detected.Owner }
if (-not $Repo) { $Repo = $detected.Repo }
}
Log-Info "Using: $Owner/$Repo"
return @{ Owner = $Owner; Repo = $Repo }
}
# -- Date Helpers --
function Get-DateToday { return (Get-Date -Format "yyyy-MM-dd") }
Export-ModuleMember -Function *