-
Notifications
You must be signed in to change notification settings - Fork 652
fix(cloud): add fail-visible sync wrappers #714
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weβll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
barbatdev
wants to merge
6
commits into
Gentleman-Programming:main
Choose a base branch
from
barbatdev:fix/cloud-sync-wrapper-visibility
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
0d0a5b5
fix(cloud): add fail-visible sync wrappers
innitdev 022dc24
fix(cloud): preserve PowerShell sync exit status
innitdev 2fb4e4a
fix(cloud): scope wrapper support to PowerShell 7
innitdev bdd5c51
fix(cloud): make wrapper help successful
innitdev 783815c
test(cloud): cover wrapper configuration paths
innitdev f4e88fc
test(cloud): fail fast on missing prerequisites
innitdev File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,82 @@ | ||
| [CmdletBinding()] | ||
| param( | ||
| [string]$LogPath, | ||
| [Parameter(Position = 0, ValueFromRemainingArguments = $true)] | ||
| [string[]]$Projects | ||
| ) | ||
|
|
||
| $ErrorActionPreference = 'Stop' | ||
| $defaultLogName = 'cloud-sync-projects.log' | ||
|
|
||
| if ($PSVersionTable.PSVersion.Major -lt 7) { | ||
| [Console]::Error.WriteLine('cloud-sync-projects.ps1: error: PowerShell 7 (pwsh) is required. 5.1 is not supported.') | ||
| exit 2 | ||
| } | ||
|
|
||
| function Write-Usage { | ||
| @' | ||
| Usage: cloud-sync-projects.ps1 [-LogPath <path>] <project> [<project> ...] | ||
| Run `engram sync --cloud --project <project>` once per explicitly named project. | ||
| Exit 0 if all succeed, 1 if any project/log op fails, 2 on usage error. | ||
| -LogPath <path> Overrides default and ENGRAM_CLOUD_SYNC_LOG. | ||
| -Help Show this help. | ||
| Requires PowerShell 7 (pwsh); 5.1 is not supported. | ||
| '@ | Out-Host | ||
| } | ||
|
|
||
| $helpRequested = $false | ||
| $cleanProjects = @() | ||
| foreach ($a in $Projects) { if ($a -in @('-Help', '--help', '-h')) { $helpRequested = $true } else { $cleanProjects += $a } } | ||
| $Projects = $cleanProjects | ||
| if ($helpRequested) { Write-Usage; exit 0 } | ||
| if ($Projects.Count -eq 0) { | ||
| [Console]::Error.WriteLine('cloud-sync-projects.ps1: error: at least one project is required'); exit 2 | ||
| } | ||
|
|
||
| $resolvedLog = $LogPath | ||
| if ([string]::IsNullOrEmpty($resolvedLog)) { $resolvedLog = $env:ENGRAM_CLOUD_SYNC_LOG } | ||
| if ([string]::IsNullOrEmpty($resolvedLog)) { | ||
| $dataDir = if ($env:ENGRAM_DATA_DIR) { $env:ENGRAM_DATA_DIR } else { (Join-Path $HOME '.engram') } | ||
| $resolvedLog = Join-Path $dataDir $defaultLogName | ||
| } | ||
| $resolvedLog = [System.IO.Path]::GetFullPath($resolvedLog) | ||
| if (-not (Test-Path -LiteralPath ([System.IO.Path]::GetDirectoryName($resolvedLog)) -PathType Container)) { | ||
| [Console]::Error.WriteLine("cloud-sync-projects.ps1: error: log directory does not exist: $resolvedLog"); exit 2 | ||
| } | ||
|
|
||
| function Write-LogLine { | ||
| param([string]$Message) | ||
| $line = "[$(Get-Date -Format 'yyyy-MM-ddTHH:mm:sszzz')] $Message" | ||
| try { Add-Content -LiteralPath $resolvedLog -Value $line -Encoding UTF8 -ErrorAction Stop } | ||
| catch { [Console]::Error.WriteLine("cloud-sync-projects.ps1: error: failed to append to log: $resolvedLog"); return $false } | ||
| Write-Host $line | ||
| return $true | ||
| } | ||
|
|
||
| function Invoke-Project { | ||
| param([string]$Project) | ||
| if (-not (Write-LogLine "project START project=$Project")) { return -1 } | ||
| $exitCode = 0 | ||
| $prevPref = $ErrorActionPreference | ||
| try { | ||
| $ErrorActionPreference = 'Continue' | ||
| & engram sync --cloud --project $Project 2>&1 | Tee-Object -FilePath $resolvedLog -Append -ErrorAction Stop | ForEach-Object { Write-Host $_ } | ||
| $exitCode = $LASTEXITCODE | ||
| if ($null -eq $exitCode) { $exitCode = 0 } | ||
| } catch { | ||
| [Console]::Error.WriteLine("cloud-sync-projects.ps1: error: invoke/tee failed for '$Project': $($_.Exception.Message)") | ||
| return -1 | ||
| } finally { | ||
| $ErrorActionPreference = $prevPref | ||
| } | ||
| if ($exitCode -eq 0) { if (-not (Write-LogLine "project SUCCESS project=$Project exit=0")) { return -1 } } | ||
| else { if (-not (Write-LogLine "project FAILURE project=$Project exit=$exitCode")) { return -1 } } | ||
| return $exitCode | ||
| } | ||
|
|
||
| $overall = 0 | ||
| if (-not (Write-LogLine "wrapper START projects=$($Projects.Count) log=$resolvedLog")) { $overall = 1 } | ||
| foreach ($proj in $Projects) { if ((Invoke-Project -Project $proj) -ne 0) { $overall = 1 } } | ||
| if ($overall -eq 0) { if (-not (Write-LogLine 'wrapper END result=success')) { $overall = 1 } } | ||
| else { if (-not (Write-LogLine "wrapper END result=failure overall=$overall")) { $overall = 1 } } | ||
| exit $overall |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,75 @@ | ||
| #!/usr/bin/env bash | ||
| set -uo pipefail | ||
|
|
||
| PROG_NAME="cloud-sync-projects.sh" | ||
| DEFAULT_LOG_NAME="cloud-sync-projects.log" | ||
|
|
||
| usage() { | ||
| cat <<'USAGE' | ||
| Usage: cloud-sync-projects.sh [--log <path>] <project> [<project> ...] | ||
| Run `engram sync --cloud --project <project>` once per explicitly named project. | ||
| Exit 0 if all succeed, 1 if any project/log op fails, 2 on usage error. | ||
| --log <path> Overrides default and ENGRAM_CLOUD_SYNC_LOG. | ||
| -h, --help Show this help. | ||
| Env: ENGRAM_DATA_DIR (defaults to ~/.engram); ENGRAM_CLOUD_SYNC_LOG (log override). | ||
| USAGE | ||
| } | ||
|
|
||
| die_usage() { printf '%s: error: %s\n' "$PROG_NAME" "$*" >&2; exit 2; } | ||
| log_path="" | ||
| projects=() | ||
| while [ $# -gt 0 ]; do | ||
| case "$1" in | ||
| -h|--help) usage; exit 0 ;; | ||
| --log) [ $# -ge 2 ] || die_usage "--log requires a path argument"; log_path="$2"; shift 2 ;; | ||
| --log=*) log_path="${1#--log=}"; [ -n "$log_path" ] || die_usage "--log requires a non-empty path"; shift ;; | ||
| --) shift; while [ $# -gt 0 ]; do projects+=("$1"); shift; done ;; | ||
| -*) die_usage "unknown option: $1" ;; | ||
| *) projects+=("$1"); shift ;; | ||
| esac | ||
| done | ||
|
|
||
| [ "${#projects[@]}" -gt 0 ] || die_usage "at least one project is required" | ||
|
|
||
| [ -z "$log_path" ] && log_path="${ENGRAM_CLOUD_SYNC_LOG:-}" | ||
| if [ -z "$log_path" ]; then | ||
| log_path="${ENGRAM_DATA_DIR:-$HOME/.engram}/$DEFAULT_LOG_NAME" | ||
| fi | ||
| case "$log_path" in /*) ;; *) log_path="$PWD/$log_path" ;; esac # absolute | ||
|
|
||
| log_dir="$(dirname "$log_path")" | ||
| [ -d "$log_dir" ] || { printf '%s: error: log directory does not exist: %s\n' "$PROG_NAME" "$log_dir" >&2; exit 2; } | ||
|
|
||
| logline() { | ||
| local ts; ts="$(date '+%Y-%m-%dT%H:%M:%S%z')" || return 1 | ||
| printf '[%s] %s\n' "$ts" "$*" >>"$log_path" || return 1 | ||
| printf '[%s] %s\n' "$ts" "$*" | ||
| } | ||
|
|
||
| run_project() { | ||
| local proj="$1" rc tee_rc | ||
| local -a statuses | ||
| logline "project START project=$proj" || return 1 | ||
| engram sync --cloud --project "$proj" 2>&1 | tee -a "$log_path" | ||
| statuses=("${PIPESTATUS[@]}") # snapshot before any other command mutates it | ||
| rc=${statuses[0]:-1}; tee_rc=${statuses[1]:-1} | ||
| if [ "$rc" -eq 0 ]; then | ||
| logline "project SUCCESS project=$proj exit=0" || return 1 | ||
| else | ||
| logline "project FAILURE project=$proj exit=$rc" || return 1 | ||
| fi | ||
| [ "$tee_rc" -ne 0 ] && [ "$rc" -eq 0 ] && return 1 # tee/log failed | ||
| return "$rc" | ||
| } | ||
|
|
||
| overall=0 | ||
| logline "wrapper START projects=${#projects[@]} log=$log_path" || overall=1 | ||
| for proj in "${projects[@]}"; do | ||
| run_project "$proj" || overall=1 | ||
| done | ||
| if [ "$overall" -eq 0 ]; then | ||
| logline "wrapper END result=success" || overall=1 | ||
| else | ||
| logline "wrapper END result=failure overall=$overall" || overall=1 | ||
| fi | ||
| exit "$overall" |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.