From d69b7622a3bd9743355a043c2a29c1c006db1ac9 Mon Sep 17 00:00:00 2001 From: Matteo Cherubini Date: Sat, 9 May 2026 17:03:15 +0200 Subject: [PATCH] refactor: Improve script robustness by returning from functions --- lib/deps.sh | 8 ++++---- lib/git-crypt.sh | 4 ++-- lib/scaffold.sh | 4 ++-- providers/forgejo.sh | 4 ++-- providers/github.sh | 2 +- 5 files changed, 11 insertions(+), 11 deletions(-) diff --git a/lib/deps.sh b/lib/deps.sh index 078f1af..5e46a86 100644 --- a/lib/deps.sh +++ b/lib/deps.sh @@ -16,10 +16,10 @@ check_deps() { if [[ ${#missing[@]} -gt 0 ]]; then error "Missing required tools: ${missing[*]}" - echo -e "\nInstall them using your package manager:" - echo " Debian/Ubuntu: sudo apt install ${missing[*]}" - echo " MacOS: brew install ${missing[*]}" - exit 1 + printf "\nInstall them using your package manager:\n" + printf " Debian/Ubuntu: sudo apt install %s\n" "${missing[*]}" + printf " MacOS: brew install %s\n" "${missing[*]}" + return 1 fi success "Environment check passed: all required tools found." diff --git a/lib/git-crypt.sh b/lib/git-crypt.sh index 874d35e..3417f05 100644 --- a/lib/git-crypt.sh +++ b/lib/git-crypt.sh @@ -83,7 +83,7 @@ gcrypt_rotate_key() { else error "Old key not found at: ${old_key_path}" error "Unlock manually before rotating: git-crypt unlock /path/to/${genome_name}.key" - exit 1 + return 1 fi else info "Repository is already unlocked — proceeding." @@ -92,7 +92,7 @@ gcrypt_rotate_key() { # 2. Ensure working tree is clean (private files excluded — they will be re-staged) if ! git diff --quiet -- ':!raw/private' ':!wiki/private' 2>/dev/null; then error "Working tree has uncommitted changes outside private/. Commit or stash them first." - exit 1 + return 1 fi # 3. Remove old key material only (preserves .git/git-crypt/ structure) diff --git a/lib/scaffold.sh b/lib/scaffold.sh index 0bea0bd..b9dd870 100644 --- a/lib/scaffold.sh +++ b/lib/scaffold.sh @@ -8,10 +8,10 @@ render_template() { local template_file="$1" local output_file="$2" - [[ ! -f "$template_file" ]] && { error "Template not found: ${template_file}"; exit 1; } + [[ ! -f "$template_file" ]] && { error "Template not found: ${template_file}"; return 1; } local content - content=$(cat "$template_file") + content=$(<"$template_file") # Placeholder replacement content="${content//\{\{GENOME_NAME\}\}/${GENOME_NAME}}" diff --git a/providers/forgejo.sh b/providers/forgejo.sh index d18b5be..bc2bcd2 100644 --- a/providers/forgejo.sh +++ b/providers/forgejo.sh @@ -31,8 +31,8 @@ provider_create_repo() { case "$http_code" in 201) success "Repository '${name}' created successfully." ;; 409) info "Repository '${name}' already exists - skipping." ;; - 401) error "Unauthorized. Check your FORGEJO_TOKEN."; exit 1 ;; - *) error "Forgejo API returned HTTP ${http_code}. Check connectivity."; exit 1 ;; + 401) error "Unauthorized. Check your FORGEJO_TOKEN."; return 1 ;; + *) error "Forgejo API returned HTTP ${http_code}. Check connectivity."; return 1 ;; esac } diff --git a/providers/github.sh b/providers/github.sh index 14193ae..f7f1f45 100644 --- a/providers/github.sh +++ b/providers/github.sh @@ -37,7 +37,7 @@ provider_create_repo() { case "$http_code" in 201) success "Repository '${name}' created on GitHub." ;; 422) info "Repository '${name}' already exists - skipping." ;; - *) error "GitHub API returned HTTP ${http_code}. check token/permissions."; exit 1 ;; + *) error "GitHub API returned HTTP ${http_code}. Check token/permissions."; return 1 ;; esac }