Skip to content
This repository was archived by the owner on Mar 29, 2026. It is now read-only.

Commit b11e782

Browse files
committed
The script has been optimized. Added additional checks to prevent accidents
1 parent b59bb81 commit b11e782

1 file changed

Lines changed: 69 additions & 44 deletions

File tree

dotload/bin/dotload

Lines changed: 69 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
#!/usr/bin/env bash
22

3-
INSTALLED_FROM=""
3+
# shellcheck disable=SC2164
4+
# shellcheck disable=SC2155
5+
# shellcheck disable=SC2181
6+
# shellcheck disable=SC2236
7+
8+
DOTFILES_DIR="$HOME/.dotload"
49
NOTES=""
510

611
if [[ $(echo "$0" | cut -c1-6) == "/snap/" ]]; then
@@ -17,8 +22,6 @@ if [[ $(echo "$0" | cut -c1-6) == "/snap/" ]]; then
1722
NOTES="Snap version "
1823
fi
1924

20-
DOTFILES_DIR="$HOME/.dotload"
21-
2225
hyperlink() {
2326
echo -e "\e]8;;$1\e\\$2\e]8;;\e\\"
2427
}
@@ -124,6 +127,41 @@ read_declaration() {
124127
echo "$value"
125128
}
126129

130+
prompt() {
131+
local attempts=0
132+
local question="$1"
133+
134+
while true; do
135+
if ((attempts == 4)); then
136+
echo -e "Too many invalid responses. Exiting program.\n"
137+
exit 1
138+
fi
139+
140+
# shellcheck disable=SC2059
141+
printf "$question \e[1m(Y/n)\e[0m: "
142+
read -r response
143+
if echo "$response" | grep -Eiq "^(y|yes)$"; then
144+
return 1
145+
146+
elif echo "$response" | grep -Eiq "^(n|no)$"; then
147+
# if [[ "$optional" == "optional" ]]; then
148+
# return 1
149+
# else
150+
# return 0
151+
# fi
152+
return 0
153+
154+
else
155+
if [ "$attempts" -lt 3 ]; then
156+
echo -e "\n\e[1;31mInvalid response\e[0m. Please enter '\e[1my\e[0m' or '\e[1mn\e[0m'."
157+
sleep "1"
158+
fi
159+
((attempts++))
160+
fi
161+
done
162+
unset attempts
163+
}
164+
127165
# Function to print informational messages
128166
print_info() {
129167
echo -e "\e[1;32mⓘ INFO:\e[0m $1"
@@ -202,11 +240,14 @@ dotload() {
202240
echo -e "╰ Commit hash: $hash\n"
203241
unset hash
204242

243+
# shellcheck disable=SC2086
205244
git clone "$repo_url" "$DOTFILES_DIR" --quiet --depth=1 $branch >/dev/null 2>&1
206245
fi
207246

208247
local custom_install_script
209248

249+
# FIXME:
250+
# shellcheck disable=SC2010
210251
custom_install_script=$(ls "$DOTFILES_DIR" |
211252
grep -E '^(((script\/)?(bootstrap|setup)|install)(\.sh|))$' |
212253
head -n 1)
@@ -226,58 +267,43 @@ dotload() {
226267
# Run the custom install script if available
227268
if [ -n "$custom_install_script" ]; then
228269
print_info "Custom install script available.\a"
229-
local attempts=0
230-
while true; do
231-
if ((attempts == 4)); then
232-
echo "Too many invalid responses. Exiting program."
233-
echo ""
234-
exit 1
235-
fi
236270

237-
printf "↪ Do you want to execute the install script?%s \e[1m(Y/n)\e[0m: " "$script_is_optional"
238-
read -r response
239-
if echo "$response" | grep -Eiq "^(y|yes)$"; then
240-
echo -e "Running install script ...\n"
241-
local current_dir=$(pwd)
242-
cd "$DOTFILES_DIR"
271+
prompt "↪ Do you want to execute the install script?$script_is_optional"
272+
if [[ $? -eq 1 ]]; then
273+
echo -e "Running install script ...\n"
274+
local current_dir=$(pwd)
275+
cd "$DOTFILES_DIR"
243276

244-
bash "$custom_install_script"
277+
bash "$custom_install_script"
245278

246-
cd "$current_dir"
247-
unset current_dir
279+
cd "$current_dir"
280+
unset current_dir
248281

249-
echo ""
250-
echo -e "\e[1;32mDone!\e[0m"
251-
echo ""
282+
echo ""
283+
echo -e "\e[1;32mDone!\e[0m"
284+
exit 0
285+
else
286+
echo -e "Canceled running of install script.\n"
287+
if [[ ! -n $script_is_optional ]]; then
252288
exit 0
253-
254-
elif echo "$response" | grep -Eiq "^(n|no)$"; then
255-
echo "Canceled running of install script."
256-
257-
if [ -n "$script_is_optional" ]; then
258-
break
259-
else
260-
echo ""
261-
exit 0
262-
fi
263-
264-
else
265-
if [ "$attempts" -lt 3 ]; then
266-
echo -e "\n\e[1;31mInvalid response\e[0m. Please enter '\e[1my\e[0m' or '\e[1mn\e[0m'."
267-
sleep "1"
268-
fi
269-
((attempts++))
270289
fi
271-
done
272-
unset attempts
290+
fi
291+
fi
292+
293+
echo -e "[\e[1mSymlinking dotfiles from ~/.dotload to \$HOME directory\e[0m]\n"
294+
295+
prompt "↪ You really want to symlink your dotfiles to a \e[1m\$HOME\e[0m directory?"
296+
if [[ $? -eq 0 ]]; then
297+
echo "Action cancelled."
298+
exit 0
273299
fi
274300

301+
echo ""
302+
275303
# Symlink new dotfiles
276304
local current_dir=$(pwd)
277305
cd "$DOTFILES_DIR"
278306

279-
echo ""
280-
echo -e "[\e[1mSymlinking dotfiles to home directory\e[0m]\n"
281307
for file in $(git ls-files "$DOTFILES_DIR" | grep -E '^\.'); do
282308
local dir=$(dirname "$HOME/$file")
283309

@@ -304,7 +330,6 @@ dotload() {
304330

305331
echo ""
306332
echo -e "\e[1;32mDone!\e[0m"
307-
echo ""
308333
}
309334

310335
# Function to print the usage information for the dotload script

0 commit comments

Comments
 (0)