refactor: Improve script robustness by returning from functions

This commit is contained in:
Matteo Cherubini 2026-05-09 17:03:15 +02:00
parent a78ae843a7
commit d69b7622a3
5 changed files with 11 additions and 11 deletions

View file

@ -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."

View file

@ -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)

View file

@ -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}}"

View file

@ -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
}

View file

@ -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
}