Skip to content

Commit e5a0bd0

Browse files
committed
Refactor shell scripts for style and robustness
Standardized spacing and formatting in restore.sh for improved readability. Enhanced test_restore.sh with safer directory changes and consistent heredoc formatting. These changes improve code clarity and reliability without altering core functionality.
1 parent 9d7283f commit e5a0bd0

File tree

2 files changed

+18
-17
lines changed

2 files changed

+18
-17
lines changed

restore.sh

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
#
2121
# Author: supermarsx
2222
# Repository: https://github.com/supermarsx/opencore-restore
23-
# License: MIT
23+
# License: MIT
2424
#
2525
# ==============================================================================
2626

@@ -46,16 +46,16 @@ NC=$(printf '\033[0m') # No Color
4646
# Helper functions to print standardized status messages.
4747

4848
# log_info: Prints an informational message in blue.
49-
log_info() { printf "${BLUE}[INFO]${NC} %s\n" "$1"; }
49+
log_info() { printf "${BLUE}[INFO]${NC} %s\n" "$1"; }
5050

5151
# log_success: Prints a success message in green.
5252
log_success() { printf "${GREEN}[ OK ]${NC} %s\n" "$1"; }
5353

5454
# log_warn: Prints a warning message in yellow.
55-
log_warn() { printf "${YELLOW}[WARN]${NC} %s\n" "$1"; }
55+
log_warn() { printf "${YELLOW}[WARN]${NC} %s\n" "$1"; }
5656

5757
# log_error: Prints an error message in red.
58-
log_error() { printf "${RED}[FAIL]${NC} %s\n" "$1"; }
58+
log_error() { printf "${RED}[FAIL]${NC} %s\n" "$1"; }
5959

6060
# header: Clears the screen and prints the script banner.
6161
header() {
@@ -111,16 +111,16 @@ else
111111
# Note: diskutil info might fail in minimal sh environments, but usually present in Recovery.
112112
NODE_INFO=$(diskutil info "$disk" | grep "Device Node" | awk '{print $3}')
113113
printf " [%d] ${YELLOW}%s${NC} (%s)\n" "$i" "$disk" "$NODE_INFO"
114-
i=$((i+1))
114+
i=$((i + 1))
115115
done
116116

117117
printf "\n"
118-
printf "Select partition number [0-$((COUNT-1))]: "
118+
printf "Select partition number [0-$((COUNT - 1))]: "
119119
read -r SELECTION
120120

121121
# Validate selection is a number
122122
case "$SELECTION" in
123-
''|*[!0-9]*)
123+
'' | *[!0-9]*)
124124
log_error "Invalid selection."
125125
exit 1
126126
;;
@@ -138,7 +138,7 @@ else
138138
TARGET_DISK=$disk
139139
break
140140
fi
141-
i=$((i+1))
141+
i=$((i + 1))
142142
done
143143
fi
144144

@@ -236,6 +236,7 @@ printf "Please perform a COLD BOOT (Power button) after shutdown.\n"
236236
printf "\n"
237237
printf "Press Enter to clear NVRAM and Shutdown..."
238238
read -r DUMMY
239+
: "$DUMMY"
239240

240241
log_info "Clearing NVRAM..."
241242
nvram -c

tests/test_restore.sh

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
# ==============================================================================
1616

1717
# Ensure we are running from the tests directory
18-
cd "$(dirname "$0")"
18+
cd "$(dirname "$0")" || exit 1
1919

2020
# --- Setup Mock Environment ---
2121
TEST_DIR="./test_env"
@@ -41,7 +41,7 @@ export PATH="$PWD/$MOCK_BIN:$PATH"
4141
# --- Mock Commands ---
4242

4343
# Mock diskutil
44-
cat << 'EOF' > "$MOCK_BIN/diskutil"
44+
cat <<'EOF' >"$MOCK_BIN/diskutil"
4545
#!/bin/sh
4646
if [ "$1" = "list" ]; then
4747
echo "/dev/disk0 (GUID Partition Scheme)"
@@ -59,21 +59,21 @@ chmod +x "$MOCK_BIN/diskutil"
5959

6060
# Mock mount (for check)
6161
# Will be overwritten later with correct path
62-
cat << 'EOF' > "$MOCK_BIN/mount"
62+
cat <<'EOF' >"$MOCK_BIN/mount"
6363
#!/bin/sh
6464
echo "mount mock"
6565
EOF
6666
chmod +x "$MOCK_BIN/mount"
6767

6868
# Mock nvram
69-
cat << 'EOF' > "$MOCK_BIN/nvram"
69+
cat <<'EOF' >"$MOCK_BIN/nvram"
7070
#!/bin/sh
7171
echo "nvram: clearing..."
7272
EOF
7373
chmod +x "$MOCK_BIN/nvram"
7474

7575
# Mock shutdown
76-
cat << 'EOF' > "$MOCK_BIN/shutdown"
76+
cat <<'EOF' >"$MOCK_BIN/shutdown"
7777
#!/bin/sh
7878
echo "shutdown: system halting..."
7979
EOF
@@ -112,27 +112,27 @@ RUNTIME_MOCK_VOLUMES="./Volumes"
112112
# Replace /Volumes/EFI with our mock path
113113
# Use | as delimiter to avoid escaping slashes
114114
# We replace the whole line to be safe
115-
sed "s|EFI_MOUNT_POINT=\"/Volumes/EFI\"|EFI_MOUNT_POINT=\"$RUNTIME_MOCK_VOLUMES/EFI\"|g" "$TEST_DIR/restore.sh" > "$TEST_DIR/restore.sh.tmp"
115+
sed "s|EFI_MOUNT_POINT=\"/Volumes/EFI\"|EFI_MOUNT_POINT=\"$RUNTIME_MOCK_VOLUMES/EFI\"|g" "$TEST_DIR/restore.sh" >"$TEST_DIR/restore.sh.tmp"
116116
mv "$TEST_DIR/restore.sh.tmp" "$TEST_DIR/restore.sh"
117117

118118
# Replace /Volumes/EFI_BACKUP with our mock path
119119
# We replace the whole line prefix
120-
sed "s|BACKUP_DIR=\"/Volumes/EFI_BACKUP_|BACKUP_DIR=\"$RUNTIME_MOCK_VOLUMES/EFI_BACKUP_|g" "$TEST_DIR/restore.sh" > "$TEST_DIR/restore.sh.tmp"
120+
sed "s|BACKUP_DIR=\"/Volumes/EFI_BACKUP_|BACKUP_DIR=\"$RUNTIME_MOCK_VOLUMES/EFI_BACKUP_|g" "$TEST_DIR/restore.sh" >"$TEST_DIR/restore.sh.tmp"
121121
mv "$TEST_DIR/restore.sh.tmp" "$TEST_DIR/restore.sh"
122122

123123
chmod +x "$TEST_DIR/restore.sh"
124124

125125
# Also update the mock mount script to return the path relative to CWD or absolute
126126
# Since we are inside TEST_DIR, ./Volumes/EFI is correct.
127-
cat << EOF > "$MOCK_BIN/mount"
127+
cat <<EOF >"$MOCK_BIN/mount"
128128
#!/bin/sh
129129
echo "/dev/disk0s1 on $RUNTIME_MOCK_VOLUMES/EFI (msdos, local, nodev, nosuid, noowners)"
130130
EOF
131131
chmod +x "$MOCK_BIN/mount"
132132

133133
# Run the script
134134
# We pipe "y" to confirm the prompt, and "enter" for the final prompt
135-
cd "$TEST_DIR"
135+
cd "$TEST_DIR" || exit 1
136136
printf "y\n\n" | ./restore.sh
137137

138138
EXIT_CODE=$?

0 commit comments

Comments
 (0)