-
-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathdotman.sh
executable file
·266 lines (231 loc) · 7.33 KB
/
dotman.sh
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
#!/usr/bin/env bash
# (.dot)file (man)ager
IFS=$'\n'
VERSION="v0.3.0"
# check if tput exists
if ! command -v tput &> /dev/null; then
# tput could not be found
BOLD=""
RESET=""
FG_SKYBLUE=""
FG_ORANGE=""
BG_AQUA=""
FG_BLACK=""
FG_ORANGE=""
UL=""
RUL=""
else
BOLD=$(tput bold)
RESET=$(tput sgr0)
FG_SKYBLUE=$(tput setaf 122)
FG_ORANGE=$(tput setaf 208)
BG_AQUA=$(tput setab 45)
FG_BLACK=$(tput setaf 16)
FG_ORANGE=$(tput setaf 214)
UL=$(tput smul)
RUL=$(tput rmul)
fi
logo() {
# print dotman logo
printf "${BOLD}${FG_SKYBLUE}%s\n" ""
printf "%s\n" " _ _ "
printf "%s\n" " | | | | "
printf "%s\n" " __| | ___ | |_ _ __ ___ __ _ _ __ "
printf "%s\n" " / _\` |/ _ \| __| \`_ ' _ \ / _' | '_ \ "
printf "%s\n" " | (_| | (_) | |_| | | | | | (_| | | | |"
printf "%s\n" " \__,_|\___/ \__|_| |_| |_|\__,_|_| |_|"
printf "${RESET}\n%s" ""
}
# check if git exists
if ! command -v git &> /dev/null; then
printf "%s\n\n" "${BOLD}${FG_SKYBLUE}${DOTMAN_LOGO}${RESET}"
echo "Can't work without Git 😞"
exit 1
fi
# function called by trap
catch_ctrl+c() {
goodbye
exit
}
trap 'catch_ctrl+c' SIGINT
repo_check(){
# check if dotfile repo is present inside DOT_DEST
DOT_REPO_NAME=$(basename "${DOT_REPO}")
# all paths are relative to HOME
if [[ -d ${HOME}/${DOT_DEST}/${DOT_REPO_NAME} ]]; then
printf "\n%s\n" "Found ${BOLD}${DOT_REPO_NAME}${RESET} as dotfile repo in ${BOLD}~/${DOT_DEST}/${RESET}"
else
printf "\n\n%s\n" "[❌] ${BOLD}${DOT_REPO_NAME}${RESET} not present inside path ${BOLD}${HOME}/${DOT_DEST}${RESET}"
read -p "Should I clone it ? [Y/n]: " -n 1 -r USER_INPUT
USER_INPUT=${USER_INPUT:-y}
case $USER_INPUT in
[y/Y]* ) clone_dotrepo "$DOT_DEST" "$DOT_REPO" ;;
[n/N]* ) printf "\n%s" "${BOLD}${DOT_REPO_NAME}${RESET} not found";;
* ) printf "\n%s\n" "[❌] Invalid Input 🙄, Try Again";;
esac
fi
}
find_dotfiles() {
printf "\n"
# while read -r value; do
# dotfiles+=($value)
# done < <( find "${HOME}" -maxdepth 1 -name ".*" -type f )
readarray -t dotfiles < <( find "${HOME}" -maxdepth 1 -name ".*" -type f )
printf '%s\n' "${dotfiles[@]}"
}
add_env() {
[[ "$DOT_DEST" && "$DOT_REPO" ]] && return
# export environment variables
printf "\n%s\n" "Exporting env variables DOT_DEST & DOT_REPO ..."
current_shell=$(basename "$SHELL")
if [[ $current_shell == "zsh" ]]; then
echo "export DOT_REPO=$1 DOT_DEST=$2" >> "$HOME"/.zshrc
elif [[ $current_shell == "bash" ]]; then
# assume we have a fallback to bash
echo "export DOT_REPO=$1 DOT_DEST=$2" >> "$HOME"/.bashrc
else
echo "Couldn't export ${BOLD}DOT_REPO=$1${RESET} and ${BOLD}DOT_DEST=$2${RESET}"
echo "Consider exporting them manually."
exit 1
fi
printf "\n%s" "Configuration for SHELL: ${BOLD}$current_shell${RESET} has been updated."
}
goodbye() {
printf "\a\n\n%s\n" "${BOLD}Thanks for using dotman 🖖.${RESET}"
printf "\n%s%s" "${BOLD}Follow ${BG_AQUA}${FG_BLACK}@bhupeshimself${RESET}" "${BOLD} on Twitter "
printf "%s\n" "for more updates.${RESET}"
printf "%s\n" "${BOLD}Report Bugs 🐛 @ ${UL}https://github.com/Bhupesh-V/dotman/issues${RUL}${RESET}"
}
dot_pull() {
# pull changes (if any) from the remote repo
printf "\n%s\n" "${BOLD}Pulling dotfiles ...${RESET}"
dot_repo="${HOME}/${DOT_DEST}/$(basename "${DOT_REPO}")"
printf "\n%s\n" "Pulling changes in $dot_repo"
GET_BRANCH=$(git remote show origin | awk '/HEAD/ {print $3}')
printf "\n%s\n" "Pulling from ${BOLD}${GET_BRANCH}"
git -C "$dot_repo" pull origin "${GET_BRANCH}"
}
diff_check() {
[[ -z $1 ]] && local file_arr
# dotfiles in repository
readarray -t dotfiles_repo < <( find "${HOME}/${DOT_DEST}/$(basename "${DOT_REPO}")" -maxdepth 1 -name ".*" -type f )
# check length here ?
for i in "${!dotfiles_repo[@]}"
do
dotfile_name=$(basename "${dotfiles_repo[$i]}")
# compare the HOME version of dotfile to that of repo
diff=$(diff -u --suppress-common-lines --color=always "${dotfiles_repo[$i]}" "${HOME}/${dotfile_name}")
if [[ $diff != "" ]]; then
if [[ $1 == "show" ]]; then
printf "\n\n%s" "Running diff between ${BOLD} ${FG_ORANGE}${HOME}/${dotfile_name}${RESET} and "
printf "%s\n" "${BOLD}${FG_ORANGE}${dotfiles_repo[$i]}${RESET}"
printf "%s\n\n" "$diff"
fi
file_arr+=("${dotfile_name}")
fi
done
[ ${#file_arr} == 0 ] && printf "\n%s\n" "${BOLD}No Changes in dotfiles.${RESET}";return
}
show_diff_check() {
diff_check "show"
}
dot_push() {
diff_check
if [[ ${#file_arr} != 0 ]]; then
printf "\n%s\n" "${BOLD}Following dotfiles changed${RESET}"
for file in "${file_arr[@]}"; do
echo "$file"
cp "${HOME}/$file" "${HOME}/${DOT_DEST}/$(basename "${DOT_REPO}")"
done
dot_repo="${HOME}/${DOT_DEST}/$(basename "${DOT_REPO}")"
git -C "$dot_repo" add -A
echo "${BOLD}Enter Commit Message (Ctrl + d to save): ${RESET}"
commit=$(</dev/stdin)
printf "\n"
git -C "$dot_repo" commit -m "$commit"
# Run Git Push
git -C "$dot_repo" push
else
return
fi
}
clone_dotrepo (){
# clone the repo in the destination directory
DOT_DEST=$1
DOT_REPO=$2
if git -C "${HOME}/${DOT_DEST}" clone "${DOT_REPO}"; then
if [[ $DOT_REPO && $DOT_DEST ]]; then
add_env "$DOT_REPO" "$DOT_DEST"
fi
printf "\n%s" "[✔️ ] dotman successfully configured"
else
# invalid arguments to exit, Repository Not Found
printf "\n%s" "[❌] $DOT_REPO Unavailable. Exiting !"
exit 1
fi
}
initial_setup() {
printf "\n\n%s\n" "First time use 🔥, Set Up ${BOLD}dotman${RESET}"
printf "%s\n" "...................................."
read -p "➤ Enter dotfiles repository URL : " -r DOT_REPO
read -p "➤ Where should I clone ${BOLD}$(basename "${DOT_REPO}")${RESET} (${HOME}/..): " -r DOT_DEST
DOT_DEST=${DOT_DEST:-$HOME}
if [[ -d "$HOME/$DOT_DEST" ]]; then
printf "\n%s\r\n" "${BOLD}Calling 📞 Git ... ${RESET}"
clone_dotrepo "$DOT_DEST" "$DOT_REPO"
printf "\n%s\n" "Open a new terminal or source your shell config"
else
printf "\n%s" "[❌]${BOLD}$DOT_DEST${RESET} Not a Valid directory"
exit 1
fi
}
manage() {
while :
do
printf "\n%s" "[${BOLD}1${RESET}] Show diff"
printf "\n%s" "[${BOLD}2${RESET}] Push changed dotfiles"
printf "\n%s" "[${BOLD}3${RESET}] Pull latest changes"
printf "\n%s" "[${BOLD}4${RESET}] List all dotfiles"
printf "\n%s\n" "[${BOLD}q/Q${RESET}] Quit Session"
read -p "What do you want me to do ? [${BOLD}1${RESET}]: " -n 1 -r USER_INPUT
# Default choice is [1], See Parameter Expansion
USER_INPUT=${USER_INPUT:-1}
case $USER_INPUT in
[1]* ) show_diff_check;;
[2]* ) dot_push;;
[3]* ) dot_pull;;
[4]* ) find_dotfiles;;
[q/Q]* ) goodbye
exit;;
* ) printf "\n%s\n" "[❌]Invalid Input 🙄, Try Again";;
esac
done
}
intro() {
BOSS_NAME=$LOGNAME
printf "\n\a%s" "Hi ${BOLD}${FG_ORANGE}$BOSS_NAME${RESET} 👋"
logo
}
init_check() {
# Check wether its a first time use or not
if [[ -z ${DOT_REPO} && -z ${DOT_DEST} ]]; then
initial_setup
goodbye
else
repo_check
manage
fi
}
if [[ $1 == "version" || $1 == "--version" || $1 == "-v" ]]; then
if [[ -d "$HOME/dotman" ]]; then
latest_tag=$(git -C "$HOME/dotman" describe --tags --abbrev=0)
latest_tag_push=$(git -C "$HOME/dotman" log -1 --format=%ai "${latest_tag}")
echo "${BOLD}dotman ${latest_tag} ${RESET}"
echo "Released on: ${BOLD}${latest_tag_push}${RESET}"
else
echo "${BOLD}dotman ${VERSION}${RESET}"
fi
exit
fi
intro
init_check