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

Commit 1b28692

Browse files
committed
Extract all the linking logic to link_file
1 parent d1cfc72 commit 1b28692

1 file changed

Lines changed: 59 additions & 56 deletions

File tree

‎script/bootstrap‎

Lines changed: 59 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -49,74 +49,77 @@ setup_gitconfig () {
4949
fi
5050
}
5151

52-
install_dotfiles () {
53-
info 'installing dotfiles'
52+
53+
link_file () {
54+
local src=$1 dst=$2
5455

5556
local overwrite= backup= skip=
56-
local overwrite_all=false backup_all=false skip_all=false
57+
local action=
5758

58-
for src in $(find "$DOTFILES_ROOT" -maxdepth 2 -name '*.symlink')
59-
do
60-
dst="$HOME/.$(basename "${src%.*}")"
59+
if [ -f "$dst" ] || [ -d "$dst" ]
60+
then
61+
62+
if [ "$overwrite_all" == "false" ] && [ "$backup_all" == "false" ] && [ "$skip_all" == "false" ]
63+
then
64+
user "File already exists: $(basename "$src"), what do you want to do? [s]kip, [S]kip all, [o]verwrite, [O]verwrite all, [b]ackup, [B]ackup all?"
65+
read -n 1 action
66+
67+
case "$action" in
68+
o )
69+
overwrite=true;;
70+
O )
71+
overwrite_all=true;;
72+
b )
73+
backup=true;;
74+
B )
75+
backup_all=true;;
76+
s )
77+
skip=true;;
78+
S )
79+
skip_all=true;;
80+
* )
81+
;;
82+
esac
83+
fi
6184

62-
overwrite=
63-
backup=
64-
skip=
85+
overwrite=${overwrite:-$overwrite_all}
86+
backup=${backup:-$backup_all}
87+
skip=${skip:-$skip_all}
6588

66-
if [ -f "$dst" ] || [ -d "$dst" ]
89+
if [ "$overwrite" == "true" ]
6790
then
91+
rm -rf "$dst"
92+
success "removed $dst"
93+
fi
6894

69-
if [ "$overwrite_all" == "false" ] && [ "$backup_all" == "false" ] && [ "$skip_all" == "false" ]
70-
then
71-
user "File already exists: $(basename "$src"), what do you want to do? [s]kip, [S]kip all, [o]verwrite, [O]verwrite all, [b]ackup, [B]ackup all?"
72-
read -n 1 action
73-
74-
case "$action" in
75-
o )
76-
overwrite=true;;
77-
O )
78-
overwrite_all=true;;
79-
b )
80-
backup=true;;
81-
B )
82-
backup_all=true;;
83-
s )
84-
skip=true;;
85-
S )
86-
skip_all=true;;
87-
* )
88-
;;
89-
esac
90-
fi
91-
92-
overwrite=${overwrite:-$overwrite_all}
93-
backup=${backup:-$backup_all}
94-
skip=${skip:-$skip_all}
95-
96-
if [ "$overwrite" == "true" ]
97-
then
98-
rm -rf "$dst"
99-
success "removed $dst"
100-
fi
101-
102-
if [ "$backup" == "true" ]
103-
then
104-
mv "$dst" "${dst}.backup"
105-
success "moved $dst to ${dst}.backup"
106-
fi
107-
108-
if [ "$skip" == "true" ]
109-
then
110-
success "skipped $src"
111-
fi
95+
if [ "$backup" == "true" ]
96+
then
97+
mv "$dst" "${dst}.backup"
98+
success "moved $dst to ${dst}.backup"
11299
fi
113100

114-
if [ "$skip" != "true" ] # "false" or empty
101+
if [ "$skip" == "true" ]
115102
then
116-
ln -s "$1" "$2"
117-
success "linked $1 to $2"
103+
success "skipped $src"
118104
fi
105+
fi
119106

107+
if [ "$skip" != "true" ] # "false" or empty
108+
then
109+
ln -s "$1" "$2"
110+
success "linked $1 to $2"
111+
fi
112+
}
113+
114+
install_dotfiles () {
115+
info 'installing dotfiles'
116+
117+
local overwrite_all=false backup_all=false skip_all=false
118+
119+
for src in $(find "$DOTFILES_ROOT" -maxdepth 2 -name '*.symlink')
120+
do
121+
dst="$HOME/.$(basename "${src%.*}")"
122+
link_file "$src" "$dst"
120123
done
121124
}
122125

0 commit comments

Comments
 (0)