Refactor bashrc setup error handling

This commit is contained in:
buildplan
2025-10-20 16:46:47 +01:00
committed by GitHub
parent 67398ee8e0
commit 606eba1e57

View File

@@ -1055,8 +1055,9 @@ configure_custom_bashrc() {
local USERNAME="$2"
local BASHRC_PATH="$USER_HOME/.bashrc"
local temp_source_bashrc=""
local keep_temp_source_on_error=false
trap 'rm -f "$temp_source_bashrc" 2>/dev/null' RETURN ERR INT TERM
trap 'rm -f "$temp_source_bashrc" 2>/dev/null' INT TERM
if ! confirm "Replace default .bashrc for '$USERNAME' with a custom one?" "n"; then
print_info "Skipping custom .bashrc configuration."
@@ -1074,7 +1075,7 @@ configure_custom_bashrc() {
fi
chmod 600 "$temp_source_bashrc"
if ! cat > "$temp_source_bashrc" <<'EOF'; then
if ! cat > "$temp_source_bashrc" <<'EOF'
# shellcheck shell=bash
# ===================================================================
# Universal Portable .bashrc for Modern Terminals
@@ -2237,6 +2238,7 @@ alias commands='compgen -A function -A alias | grep -v "^_" | sort | column'
# - Consider moving rarely-used functions to separate files
# - Use 'time bash -i -c exit' to measure startup time
EOF
then
print_error "Failed to write .bashrc content to temporary file $temp_source_bashrc."
log "Critical error: Failed to write bashrc content to $temp_source_bashrc."
rm -f "$temp_source_bashrc" 2>/dev/null
@@ -2245,9 +2247,17 @@ EOF
log "Successfully created temporary .bashrc source at $temp_source_bashrc"
if [[ -f "$BASHRC_PATH" ]] && ! grep -q "generated by /usr/sbin/adduser" "$BASHRC_PATH" 2>/dev/null; then
local BASHRC_BACKUP="$BASHRC_PATH.backup_$(date +%Y%m%d_%H%M%S)"
print_info "Backing up existing non-default .bashrc to $BASHRC_BACKUP"
cp "$BASHRC_PATH" "$BASHRC_BACKUP"
log "Backed up existing .bashrc to $BASHRC_BACKUP"
fi
local temp_fallback_path="/tmp/custom_bashrc_for_${USERNAME}.txt"
if ! tee "$BASHRC_PATH" < "$temp_source_bashrc" > /dev/null; then
if ! tee "$BASHRC_PATH" < "$temp_source_bashrc" > /dev/null
then
print_error "Failed to automatically write custom .bashrc to $BASHRC_PATH."
log "Error writing custom .bashrc for $USERNAME to $BASHRC_PATH (likely permissions issue)."
@@ -2260,6 +2270,7 @@ EOF
print_info " sudo chown ${USERNAME}:${USERNAME} ${BASHRC_PATH}"
print_info " sudo chmod 644 ${BASHRC_PATH}"
log "Saved custom .bashrc content to $temp_fallback_path for manual installation."
keep_temp_source_on_error=true
else
print_error "Also failed to save custom .bashrc content to $temp_fallback_path."
log "Critical error: Failed both writing to $BASHRC_PATH and copying $temp_source_bashrc to $temp_fallback_path."
@@ -2269,10 +2280,10 @@ EOF
print_warning "Failed to set correct ownership/permissions on $BASHRC_PATH."
log "Failed to chown/chmod $BASHRC_PATH"
print_warning "ACTION REQUIRED: Please manually set ownership/permissions:"
print_info " sudo cp ${temp_source_bashrc} ${BASHRC_PATH} # You might need this if the file is corrupted"
print_info " sudo chown ${USERNAME}:${USERNAME} ${BASHRC_PATH}"
print_info " sudo chmod 644 ${BASHRC_PATH}"
temp_source_bashrc=""
print_info " (Source content is in ${temp_source_bashrc})"
keep_temp_source_on_error=true
else
print_success "Custom .bashrc created for '$USERNAME'."
log "Custom .bashrc configuration completed for $USERNAME."
@@ -2280,7 +2291,11 @@ EOF
fi
fi
rm -f "$temp_source_bashrc" 2>/dev/null
if [[ "$keep_temp_source_on_error" == false ]]; then
rm -f "$temp_source_bashrc" 2>/dev/null
fi
trap - INT TERM
return 0
}