Skip to content

Commit 606eba1

Browse files
authored
Refactor bashrc setup error handling
1 parent 67398ee commit 606eba1

File tree

1 file changed

+21
-6
lines changed

1 file changed

+21
-6
lines changed

du_setup.sh

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1055,8 +1055,9 @@ configure_custom_bashrc() {
10551055
local USERNAME="$2"
10561056
local BASHRC_PATH="$USER_HOME/.bashrc"
10571057
local temp_source_bashrc=""
1058+
local keep_temp_source_on_error=false
10581059

1059-
trap 'rm -f "$temp_source_bashrc" 2>/dev/null' RETURN ERR INT TERM
1060+
trap 'rm -f "$temp_source_bashrc" 2>/dev/null' INT TERM
10601061

10611062
if ! confirm "Replace default .bashrc for '$USERNAME' with a custom one?" "n"; then
10621063
print_info "Skipping custom .bashrc configuration."
@@ -1074,7 +1075,7 @@ configure_custom_bashrc() {
10741075
fi
10751076
chmod 600 "$temp_source_bashrc"
10761077

1077-
if ! cat > "$temp_source_bashrc" <<'EOF'; then
1078+
if ! cat > "$temp_source_bashrc" <<'EOF'
10781079
# shellcheck shell=bash
10791080
# ===================================================================
10801081
# Universal Portable .bashrc for Modern Terminals
@@ -2237,6 +2238,7 @@ alias commands='compgen -A function -A alias | grep -v "^_" | sort | column'
22372238
# - Consider moving rarely-used functions to separate files
22382239
# - Use 'time bash -i -c exit' to measure startup time
22392240
EOF
2241+
then
22402242
print_error "Failed to write .bashrc content to temporary file $temp_source_bashrc."
22412243
log "Critical error: Failed to write bashrc content to $temp_source_bashrc."
22422244
rm -f "$temp_source_bashrc" 2>/dev/null
@@ -2245,9 +2247,17 @@ EOF
22452247

22462248
log "Successfully created temporary .bashrc source at $temp_source_bashrc"
22472249

2250+
if [[ -f "$BASHRC_PATH" ]] && ! grep -q "generated by /usr/sbin/adduser" "$BASHRC_PATH" 2>/dev/null; then
2251+
local BASHRC_BACKUP="$BASHRC_PATH.backup_$(date +%Y%m%d_%H%M%S)"
2252+
print_info "Backing up existing non-default .bashrc to $BASHRC_BACKUP"
2253+
cp "$BASHRC_PATH" "$BASHRC_BACKUP"
2254+
log "Backed up existing .bashrc to $BASHRC_BACKUP"
2255+
fi
2256+
22482257
local temp_fallback_path="/tmp/custom_bashrc_for_${USERNAME}.txt"
22492258

2250-
if ! tee "$BASHRC_PATH" < "$temp_source_bashrc" > /dev/null; then
2259+
if ! tee "$BASHRC_PATH" < "$temp_source_bashrc" > /dev/null
2260+
then
22512261
print_error "Failed to automatically write custom .bashrc to $BASHRC_PATH."
22522262
log "Error writing custom .bashrc for $USERNAME to $BASHRC_PATH (likely permissions issue)."
22532263

@@ -2260,6 +2270,7 @@ EOF
22602270
print_info " sudo chown ${USERNAME}:${USERNAME} ${BASHRC_PATH}"
22612271
print_info " sudo chmod 644 ${BASHRC_PATH}"
22622272
log "Saved custom .bashrc content to $temp_fallback_path for manual installation."
2273+
keep_temp_source_on_error=true
22632274
else
22642275
print_error "Also failed to save custom .bashrc content to $temp_fallback_path."
22652276
log "Critical error: Failed both writing to $BASHRC_PATH and copying $temp_source_bashrc to $temp_fallback_path."
@@ -2269,18 +2280,22 @@ EOF
22692280
print_warning "Failed to set correct ownership/permissions on $BASHRC_PATH."
22702281
log "Failed to chown/chmod $BASHRC_PATH"
22712282
print_warning "ACTION REQUIRED: Please manually set ownership/permissions:"
2272-
print_info " sudo cp ${temp_source_bashrc} ${BASHRC_PATH} # You might need this if the file is corrupted"
22732283
print_info " sudo chown ${USERNAME}:${USERNAME} ${BASHRC_PATH}"
22742284
print_info " sudo chmod 644 ${BASHRC_PATH}"
2275-
temp_source_bashrc=""
2285+
print_info " (Source content is in ${temp_source_bashrc})"
2286+
keep_temp_source_on_error=true
22762287
else
22772288
print_success "Custom .bashrc created for '$USERNAME'."
22782289
log "Custom .bashrc configuration completed for $USERNAME."
22792290
rm -f "$temp_fallback_path" 2>/dev/null
22802291
fi
22812292
fi
22822293

2283-
rm -f "$temp_source_bashrc" 2>/dev/null
2294+
if [[ "$keep_temp_source_on_error" == false ]]; then
2295+
rm -f "$temp_source_bashrc" 2>/dev/null
2296+
fi
2297+
2298+
trap - INT TERM
22842299

22852300
return 0
22862301
}

0 commit comments

Comments
 (0)