Private/Invoke-CCGh.ps1
|
function Invoke-CCGh { <# .SYNOPSIS Runs the GitHub CLI in a repository and returns whether it succeeded, what it answered and what it said beside the answer. .NOTES A failure is returned, not thrown, so that the caller can say what to do about it. The answer and standard error are kept apart: a warning is not part of an answer. The tests replace this function, so that no test asks GitHub anything. #> param( [Parameter(Mandatory)][string]$Path, [Parameter(Mandatory)][string[]]$Arguments ) if (-not (Get-Command gh -ErrorAction SilentlyContinue)) { return [pscustomobject]@{ Ok = $false Output = '' Error = "The GitHub CLI (gh) is not on the path. Install gh and run 'gh auth login'." } } Push-Location -LiteralPath $Path try { $all = @(& gh @Arguments 2>&1) $code = $LASTEXITCODE } finally { Pop-Location } [pscustomobject]@{ Ok = ($code -eq 0) Output = (@($all | Where-Object { $_ -isnot [System.Management.Automation.ErrorRecord] }) -join "`n").Trim() Error = (@($all | Where-Object { $_ -is [System.Management.Automation.ErrorRecord] }) -join "`n").Trim() } } |