-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathomt-manager.sh
245 lines (215 loc) · 7.13 KB
/
omt-manager.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
# Set default values for environment variables
: "${MANAGER_OMT_DIR_PATH:="${HOME}/.tmux"}"
: "${MANAGER_OMT_CONF_PATH:="${HOME}/.tmux.conf"}"
: "${MANAGER_OMT_LOCAL_CONF_PATH:="${HOME}/.tmux.conf.local"}"
: "${MANAGER_OMT_REPO_URL:="https://github.com/gpakosz/.tmux.git"}"
export MANAGER_OMT_DIR_PATH
export MANAGER_OMT_CONF_PATH
export MANAGER_OMT_LOCAL_CONF_PATH
export MANAGER_OMT_REPO_URL
_omt_help() {
echo "Usage: omt [install|remove|upgrade]"
echo "Use omt -h for more information"
}
omt() {
case "$1" in
install)
_omt_install "${@:2}"
return_code=$?
OPTIND=1
;;
remove)
_omt_remove "${@:2}"
return_code=$?
OPTIND=1
;;
upgrade)
_omt_upgrade "${@:2}"
return_code=$?
OPTIND=1
;;
-h)
_omt_help
return_code=0
;;
*)
_omt_help
return_code=1
;;
esac
return $return_code
}
_omt_install_help() {
echo "Usage: omt install [-s] [-f] [-o] [-h]"
echo "Options:"
echo " -s Silent mode, suppress output"
echo " -f Force install, remove existing Oh My Tmux and reinstall, backup and replace existing .tmux.conf"
echo " -o Overwrite user config, backup and replace existing .tmux.conf.local (must be used with -f)"
echo " -h Show help information"
}
_mkdir() {
if [[ -d "$1" ]]; then
return 0
fi
mkdir -p "$1" >/dev/null 2>&1
if [[ $? -ne 0 ]]; then
[ "$2" = false ] && echo "Failed to create directory $1."
return 1
fi
}
_omt_install() {
local silent=false force=false overwrite_usr_conf=false
while getopts "sfho" opt; do
case "$opt" in
s) silent=true ;;
f) force=true ;;
o) overwrite_usr_conf=true ;;
h)
_omt_install_help
return 0
;;
*)
_omt_install_help
return 1
;;
esac
done
# create directories
_mkdir "$(dirname "$MANAGER_OMT_DIR_PATH")" $silent || return 1
_mkdir "$(dirname "$MANAGER_OMT_CONF_PATH")" $silent || return 1
_mkdir "$(dirname "$MANAGER_OMT_LOCAL_CONF_PATH")" $silent || return 1
# check if -o is used with -f
if [[ "$overwrite_usr_conf" = true && "$force" = false ]]; then
[[ "$silent" = false ]] && echo "The -o option must be used with -f."
return 1
fi
# check if Oh My Tmux or .tmux.conf exists
if [[ -d "$MANAGER_OMT_DIR_PATH" || -f "$MANAGER_OMT_CONF_PATH" ]]; then
if [[ "$force" = true ]]; then
[[ -d "$MANAGER_OMT_DIR_PATH" ]] && rm -rf "$MANAGER_OMT_DIR_PATH"
else
[[ "$silent" = false ]] && echo "Oh My Tmux already exists or .tmux.conf is present. Use -f option to force install."
return 1
fi
fi
if [[ "$silent" = true ]]; then
git clone $MANAGER_OMT_REPO_URL "$MANAGER_OMT_DIR_PATH" >/dev/null 2>&1
else
git clone $MANAGER_OMT_REPO_URL "$MANAGER_OMT_DIR_PATH"
fi
if [[ $? -ne 0 ]]; then
[[ "$silent" = false ]] && echo "Failed to clone Oh My Tmux repository."
return 1
fi
# if .tmux.conf exists, force must be true here
[[ -f "$MANAGER_OMT_CONF_PATH" ]] && cp "$MANAGER_OMT_CONF_PATH" "${MANAGER_OMT_CONF_PATH}.bak"
cp "${MANAGER_OMT_DIR_PATH}/.tmux.conf" "$MANAGER_OMT_CONF_PATH"
# if .tmux.conf.local exists
if [[ -f "$MANAGER_OMT_LOCAL_CONF_PATH" ]]; then
if [[ "$force" = true && "$overwrite_usr_conf" = true ]]; then
cp "$MANAGER_OMT_LOCAL_CONF_PATH" "${MANAGER_OMT_LOCAL_CONF_PATH}.bak"
cp "${MANAGER_OMT_DIR_PATH}/.tmux.conf.local" "$MANAGER_OMT_LOCAL_CONF_PATH"
else
[[ "$silent" = false ]] && echo ".tmux.conf.local already exists. Use -fo options to to backup and overwrite it."
return 1
fi
else
cp "${MANAGER_OMT_DIR_PATH}/.tmux.conf.local" "$MANAGER_OMT_LOCAL_CONF_PATH"
fi
}
_omt_remove_help() {
echo "Remove Oh My Tmux and .tmux.conf"
echo "Usage: omt remove [-a] [-h]"
echo "Options:"
echo " -a Be careful, will remove .tmux.conf.local"
echo " -h Show help information"
}
_omt_remove() {
local remove_all=false
while getopts "ah" opt; do
case "$opt" in
a) remove_all=true ;;
h)
_omt_remove_help
return 0
;;
*)
_omt_remove_help
return 1
;;
esac
done
if [[ -d "$MANAGER_OMT_DIR_PATH" ]]; then
rm -rf "$MANAGER_OMT_DIR_PATH"
fi
if [[ -f "$MANAGER_OMT_CONF_PATH" ]]; then
rm -f "$MANAGER_OMT_CONF_PATH"
fi
if [[ -f "$MANAGER_OMT_LOCAL_CONF_PATH" ]] && [[ "$remove_all" = true ]]; then
rm -f "$MANAGER_OMT_LOCAL_CONF_PATH"
fi
return 0
}
_omt_upgrade_help() {
echo "Do not change the local Oh My Tmux repo, the local changes will be lost when upgrade."
echo "Usage: omt upgrade [-s] [-o] [-h]"
echo "Options:"
echo " -s Silent mode, suppress output"
echo " -o Overwrite user config, backup and replace existing .tmux.conf.local"
echo " -h Show help information"
}
_omt_upgrade() {
local silent=false overwrite_usr_conf=false
while getopts "sho" opt; do
case "$opt" in
s) silent=true ;;
o) overwrite_usr_conf=true ;;
h)
_omt_upgrade_help
return 0
;;
*)
_omt_upgrade_help
return 1
;;
esac
done
# check if omt installed
if [[ ! -d "$MANAGER_OMT_DIR_PATH" ]]; then
[[ "$silent" = false ]] && echo "Oh My Tmux is not installed."
return 1
fi
local branch_name
branch_name=$(git -C "$MANAGER_OMT_DIR_PATH" symbolic-ref --short HEAD)
if [[ -z "$branch_name" ]]; then
if [[ "$silent" = false ]]; then
echo "Failed to determine the current branch name for local Oh My Tmux."
echo "Please check if it is a valid git repository."
echo "You can use \"omt install -f\" to force re-install."
fi
return 1
fi
git -C "$MANAGER_OMT_DIR_PATH" fetch
if [[ $? -ne 0 ]]; then
[[ "$silent" = false ]] && echo "Failed to fetch updates."
return 1
fi
output=$(git -C "$MANAGER_OMT_DIR_PATH" status)
if [[ "$output" == *"Your branch is up to date"* ]]; then
[[ "$silent" = false ]] && echo "Oh My Tmux is already up to date."
return 0
fi
git -C "$MANAGER_OMT_DIR_PATH" reset --hard origin/"$branch_name" >/dev/null 2>&1
if [[ $? -ne 0 ]]; then
[[ "$silent" = false ]] && echo "Failed to merge updates."
return 1
fi
cp -f "${MANAGER_OMT_DIR_PATH}/.tmux.conf" "$MANAGER_OMT_CONF_PATH"
if [[ "$overwrite_usr_conf" = true ]]; then
[[ -f "$MANAGER_OMT_LOCAL_CONF_PATH" ]] && cp "$MANAGER_OMT_LOCAL_CONF_PATH" "${MANAGER_OMT_LOCAL_CONF_PATH}.bak"
cp -f "${MANAGER_OMT_DIR_PATH}/.tmux.conf.local" "$MANAGER_OMT_LOCAL_CONF_PATH"
fi
echo "Oh My Tmux has been upgraded to the latest version."
echo "Please use tmux source-file \"$MANAGER_OMT_CONF_PATH\" to reload the config."
return 0
}