-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdot
executable file
·327 lines (265 loc) · 7.58 KB
/
dot
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
#!/usr/bin/env bash
# TODO: Make these dynamic as reading it from ENV
DOTFILES_DIR="$HOME/.dotfiles"
DOTFILES_DEFAULT_BRANCH="master"
DOTFILES_DEFAULT_REMOTE="origin"
DOTFILES_SSH_GIT_URL="[email protected]:epicmet/.dotfiles.git"
DOTFILES_HTTP_GIT_URL="https://github.com/epicmet/.dotfiles.git"
TEMP_EXECUTABLE="temp_dot_executable"
SSH_DIR="$HOME/.ssh"
ANSIBLE_ENTRY="$DOTFILES_DIR/ansible/local.yml"
# Exit codes. Ref -> https://www.cyberciti.biz/faq/linux-bash-exit-status-set-exit-statusin-bash/
EC_SUCCEESS=0
EC_FAILURE=1
EC_NOT_IMPLEMENTED=38
set -Eeuo pipefail
trap cleanup SIGINT SIGTERM ERR EXIT
cleanup_funcs=()
ran_cleanup=1
cleanup() {
if [[ $ran_cleanup == 1 ]]; then
#echo -e "${WARNING} Running cleanup ..."
# General cleanup
# NOT IMPLEMENTED
# Clean up funcs
for f in "${cleanup_funcs[@]}"; do
run_if_exists "$f" ""
done
#echo -e "${CHECK_MARK} Cleanup complete"
ran_cleanup=0
fi
}
setup_colors() {
if [[ -t 2 ]] && [[ $NO_COLOR == 1 ]] && [[ "${TERM-}" != "dumb" ]]; then
# Color codes
RED='\033[1;31m'
YELLOW='\033[1;33m'
BLUE="\\033[38;5;27m"
SEA="\\033[38;5;49m"
GREEN='\033[1;32m'
CYAN='\033[1;36m'
NC='\033[0m'
# Emoji codes
CHECK_MARK="${GREEN}\xE2\x9C\x94${NC}"
X_MARK="${RED}\xE2\x9C\x96${NC}"
PIN="${RED}\xF0\x9F\x93\x8C${NC}"
CLOCK="${GREEN}\xE2\x8C\x9B${NC}"
ARROW="${SEA}\xE2\x96\xB6${NC}"
BOOK="${RED}\xF0\x9F\x93\x8B${NC}"
WARNING="${GREEN}\xF0\x9F\x9A\xA8${NC}"
RIGHT_ANGLE="${GREEN}\xE2\x88\x9F${NC}"
else
RED='' YELLOW='' BLUE='' SEA='' GREEN='' CYAN='' NC='' CHECK_MARK='' X_MARK='' PIN='' CLOCK='' ARROW='' BOOK='' WARNING='' RIGHT_ANGLE=''
fi
}
abort() {
local msg="$1"
local exit_code="${2:-$EC_FAILURE}"
printf "${RED}%s\n${NC}" "$msg" >&2
exit $exit_code
}
run_if_exists() {
local func_to_run="$1"
local func_args="$2"
local failure_msg="${3-"Internal error. Run the script using \"--verbose\" to debug."}"
local failure_exit_code="${4-}"
if type "$func_to_run" >/dev/null 2>&1; then
$func_to_run $func_args
else
abort "$failure_msg" "$failure_exit_code"
fi
}
index_of() {
local -n arr=$1
local value=$2
for i in "${!arr[@]}"; do
if [[ "${arr[$i]}" = "${value}" ]]; then
echo "${i}"
fi
done
}
bring_top() {
local target="$1"
local target_idx=$(index_of supported_os "$target")
local temp="${supported_os[0]}"
supported_os[0]="$target"
supported_os[$target_idx]="$temp"
}
show_usage() {
local exit_code=${1:-$EC_SUCCEESS}
printf "%b\n" "$(
cat <<EOF
USAGE:
${GREEN}dot${NC} [<sub-command>]
DESCRIPTION:
Simple bash script to manage your dotfiles
FLAGS:
--help, -h Print this help message and exit
--no-color, -n Run script without colors or emojis
--no-trust Choose the operating system detection manually
--verbose Show more stuff in case of an error (set -x)
SUB COMMANDS:
<NO-SUB-COMMAND> No sub-command is equivalent \"dot stow\" && \"dot ans update\"
init This is equivalent \"dot ans init\" && \"dot hts\" && \"dot stow\"
pull Either clone dotfiles or pull latest commits from the master branch
sync Sync the current local \"dot\" script to the install path (/usr/local/bin)
stable Get & install the \"dot\" script in the latest commit from repo
stow Update the config files using stow
ans init Runs the \"initial\" tag of ansible playbook
ans update Run all ansible playbooks but \"initial\"
hts Change \".dotfiles\" remote from HTTP to SSH
Run "dot <sub-command> --help" to get more information about a command
EOF
)"
exit $exit_code
}
NO_COLOR=1
TRUST_OP_DETECTION=0
setup_colors
# Parse dot flags
while [[ $# -ge 1 && "$1" =~ ^- && ! "$1" == "--" ]]; do
case "$1" in
--verbose)
set -x
shift
;;
-n | --no-color)
NO_COLOR=0
setup_colors
shift
;;
--no-trust)
TRUST_OP_DETECTION=1
shift
;;
-h | --help)
show_usage
shift
;;
*)
abort "Unknown flag $1"
;;
esac
done
dot__pull() {
if [[ ! -d "$DOTFILES_DIR" ]]; then
clone_url="$DOTFILES_HTTP_GIT_URL"
# TODO: Better ssh validation?
if [[ -d "$SSH_DIR" ]]; then
clone_url="$DOTFILES_SSH_GIT_URL"
fi
gum spin -s line --title="Cloning the git repository into \"$HOME/.dotfiles\"..." -- \
git clone --quiet "$clone_url" "$DOTFILES_DIR" 2>&1 >/dev/null
else
gum spin -s line --title="Updating the git repository..." -- \
git -C "$DOTFILES_DIR" pull "$DOTFILES_DEFAULT_REMOTE" "$DOTFILES_DEFAULT_BRANCH" --quiet >/dev/null
fi
}
cleanup__update() {
echo "Clean up update"
if [[ -e "$TEMP_EXECUTABLE" ]]; then
rm $TEMP_EXECUTABLE
fi
}
dot__stable() {
cleanup_funcs+=("cleanup__update")
echo -e "${SEA}Getting latest \"dot\" script..."${NC}
curl -fsSL https://raw.githubusercontent.com/epicmet/.dotfiles/master/dot >"$TEMP_EXECUTABLE"
sudo mv $TEMP_EXECUTABLE /usr/local/bin/dot &&
chmod +x /usr/local/bin/dot
}
dot__sync() {
sudo cp "$DOTFILES_DIR/dot" /usr/local/bin/dot &&
chmod +x /usr/local/bin/dot
echo -e "${SEA}Local \"dot\" script synced with /usr/local/bin/dot."${NC}
exit $EC_SUCCEESS
}
dot__hts() {
local curr_remote=$(git -C "$DOTFILES_DIR" remote get-url "$DOTFILES_DEFAULT_REMOTE")
if [[ "${curr_remote::4}" == "http" ]]; then
git -C "$DOTFILES_DIR" remote set-url "$DOTFILES_DEFAULT_REMOTE" "$DOTFILES_SSH_GIT_URL"
echo -e "${CHECK_MARK} Remote successfully updated."
else
echo -e "${WARNING} Current \".dotfiles\" remote is not based on http: $curr_remote"
fi
}
dot__init() {
dot__pull
dot__ans__init
dot__hts
dot__stow
}
dot__stow() {
base_stow="bin,zsh,nvim,tmux,starship,wezterm,ghostty"
declare -A config_dirs
config_dirs=(
[macos]="$base_stow,yabai"
[arch]="$base_stow,libinput-gestures,betterlockscreen,i3,i3status"
[ubuntu]="$base_stow"
)
supported_os=(
"arch"
"macos"
"ubuntu"
)
OP=$(uname -s | tr '[:upper:]' '[:lower:]') || "$OSTYPE"
case $OP in
darwin*) bring_top "macos" ;;
linux*)
distro=$(awk -F= '/^NAME/{print $2}' /etc/os-release | tr -d '"' | tr '[:upper:]' '[:lower:]')
case $distro in
*arch*) bring_top "arch" ;;
*ubuntu*) bring_top "ubuntu" ;;
esac
;;
esac
if [[ $TRUST_OP_DETECTION == 0 ]]; then
OP="${supported_os[0]}"
else
OP=$(gum choose --header="Select your OS: " "${supported_os[@]}")
fi
stow_dir=$(echo ${config_dirs[$OP]} | sed "s/,/ /g")
pushd $DOTFILES_DIR 2>&1 >/dev/null
for folder in $stow_dir; do
stow -D $folder
stow $folder
done
popd 2>&1 >/dev/null
}
dot__ans() {
if [[ $# -gt 0 ]]; then
local ans_sub_cmd="$1"
shift
local ans_sub_cmd_args="${@-}"
if [[ $ans_sub_cmd == "--" ]]; then
abort "Not implemented support for \"--\"" $EC_NOT_IMPLEMENTED
fi
run_if_exists "dot__ans__$ans_sub_cmd" "$ans_sub_cmd_args" "The sub command \"ans\" does not have \"$ans_sub_cmd\" sub command!"
else
echo -e "${X_MARK} no sub command provided for \"ans\". See \"dot --help\""
exit $EC_FAILURE
fi
}
dot__ans__init() {
echo -e "${RIGHT_ANGLE} Running \"initial\" ansible tag..."
ansible-playbook -t initial --ask-vault-pass "$ANSIBLE_ENTRY"
}
dot__ans__update() {
echo -e "${RIGHT_ANGLE} Syncing packages using ansible..."
ansible-playbook --skip-tags initial "$ANSIBLE_ENTRY"
}
if [[ $# -gt 0 ]]; then
sub_cmd="$1"
shift
sub_cmd_args="${@-}"
if [[ $sub_cmd == "--" ]]; then
abort "Not implemented support for \"--\"" $EC_NOT_IMPLEMENTED
fi
run_if_exists "dot__$sub_cmd" "$sub_cmd_args" "The sub command \"$sub_cmd\" not found!"
exit $EC_SUCCEESS
else
### No sub command ###
dot__stow
dot__ans__update
fi
echo -e "${CHECK_MARK} All done >:) You may want to reload your terminal. ${CYAN}Happy hacking!${NC}"