-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathhubget
executable file
·292 lines (251 loc) · 8.52 KB
/
hubget
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
#!/bin/bash
# Created by: WestleyK
# Email: westley@sylabs.io
# version-1.4.1
# Date: Feb 2, 2018
# https://github.com/WestleyK/easy-clone
#
# The Clear BSD License
#
# Copyright (c) 2018 WestleyK
# All rights reserved.
#
# This software is licensed under a Clear BSD License.
#
set -e
VERSION_HUBGET="version-1.4.1"
DATE_HUBGET="Feb 2, 2018"
BUG_FILE="/tmp/debug_hubget_$VERSION_HUBGET.txt"
UP_URL="https://raw.githubusercontent.com/WestleyK/easy-clone/master/script-version.txt"
VERB="false"
if ! [ -x "$(command -v ecgo)" ]; then
echo "ERROR: need: ecgo"
echo "Please install ecgo"
echo "https://github.com/WestleyK/ecgo"
exit 1
fi
if ! [ -x "$(command -v strcomp)" ]; then
ecgo --fatal "\`strcomp' not installed!"
ecgo "Please install strcomp version 1.0.0-beta-10 or greater"
ecgo "https://github.com/WestleyR/strcomp"
exit 1
fi
help_menu() {
ecgo -o "%{white}%{bold}DESCRIPTION:%{r}
%{teal} simple, fast repo cloner, with %{bold}%{underline}<TAB>%{r}%{teal} autocomplete.%{r}
%{teal} automatlicy add repos to %{bold}%{underline}<TAB>%{r}%{teal} complete by typing it manualy,%{r}
%{teal} then next time you start typing that repo, %{bold}%{underline}<TAB>%{r}%{teal} will work.%{r}
%{white}%{bold}USAGE:%{r}
%{green}%{bold}$0 %{r}%{blue}%{underline}[OPTION]%{r} %{teal}%{underline}[USERNAME/REPO_NAME]%{r}
%{white}%{bold}OPTIONS:%{r}
%{yellow}%{bold}-h, --help %{r}:%{r} %{teal}print help menu.%{r}
%{yellow}%{bold}-c, --check_update %{r}:%{r} %{teal}check for update.%{r}
%{yellow}%{bold}-v, --verbose %{r}:%{r} %{teal}print more info.%{r}
%{yellow}%{bold}-n, --no-update %{r}:%{r} %{teal}don't check for update.%{r}
%{yellow}%{bold} --clear-bug %{r}:%{r} %{teal}clear debug file, run --info to see.%{r}
%{yellow}%{bold}-i, --info %{r}:%{r} %{teal}print script version.%{r}
%{yellow}%{bold} --version %{r}:%{r} %{teal}print script version.%{r}
%{blue}%{bold}This software is licensed under a Clear BSD License.%{r}
%{green}source code: https://github.con/WestleyK/easy-clone%{r}"
exit 0
}
print_version() {
ecgo -teal "${VERSION_HUBGET}, " -r -blue "${DATE_HUBGET}"
exit 0
}
print_info() {
ecgo -a "source code : https://github.com/WestleyK/easy-clone
debug file : ${BUG_FILE}
autocomplete file : ~/easy-clone/option-url.txt
autocomplete script : ~/easy-clone/auto-complete.sh"
exit 0
}
verb() {
if [ "$VERB" = "true" ]; then
ecgo --info "$@"
fi
}
debug() {
if [ $(strcomp -c "$DEBUG" "true") == "true" ]; then
ecgo --debug "$@"
fi
}
debug_output() {
DATE_RUN=` date `
ecgo "" >> $BUG_FILE
ecgo "" >> $BUG_FILE
ecgo "" >> $BUG_FILE
ecgo "" >> $BUG_FILE
ecgo $DATE_RUN >> $BUG_FILE
ecgo "expecting: $ERROR_EX" >> $BUG_FILE
ecgo "actral: $ERROR_BUG" >> $BUG_FILE
}
only_one_arg() {
if [ ! -z "$2" ]; then
ecgo --fatal "$OPTION flag only needs one argument!"
ecgo " eg. $ ${0} $OPTION"
exit 1
fi
}
check_update() {
verb "Checking for one argument..."
only_one_arg
ecgo --info "Checking for update..."
NEW_V=` curl -s $UP_URL `
verb "New version: ${NEW_V}"
verb "Checking if: ${NEW_V} starts with: version"
if [[ "$NEW_V" != "version"* ]]; then
verb "An error has occurred"
ERROR_EX="version*"
ERROR_BUG=$NEW_V
debug_output
ecgo --fatal "An error has occurred"
ecgo -red " Unable to check for update."
ecgo
ecgo --fatal -red "FAIL: check for update failed!"
ecgo " Try: "
ecgo " >> $ cat $BUG_FILE (for debug)"
exit 1
fi
# exclude the `version-.` from the string
verb "Excluding: version-. from: ${NEW_V} (the new version)"
NEW_VERSION=` ecgo -a "$NEW_V" | strcomp -e="version-." `
verb "Excluding: version-. from: ${VERSION_HUBGET} (the current version)"
CUR_VERSION=` ecgo -a "$VERSION_HUBGET" | strcomp -e="version-." `
# test to make sure its a int
verb "Testing if: ${NEW_VERSION} is a int..."
if [[ $(ecgo -a "$NEW_VERSION" | strcomp -i) != 0 ]]; then
ecgo -f "Unable to detecte version"
ecgo -d "${NEW_VERSION}: Not a int"
exit 255
fi
verb "${NEW_VERSION}: is a int"
verb "Checking if current version (${CUR_VERSION}) is a int"
if [[ $(ecgo -a "$CUR_VERSION" | strcomp -i) != 0 ]]; then
ecgo -f "Unable to detecte version"
ecgo -d "${CUR_VERSION}: Not a int"
exit 255
fi
verb "Checking if NEW_VERSION (${NEW_VERSION}), is greater then CUR_VERSION (${CUR_VERSION})"
if [[ "$NEW_VERSION" -gt "$CUR_VERSION" ]]; then
ecgo --info"An update is avalable:"
ecgo --info"Your hubget version: $VERSION_HUBGET"
ecgo --info"New version: $NEW_V"
ecgo -green -bold "Do:"
ecgo -green -bold " $ " -r -green "cd ~/easy-clone/ " -r -teal "# or wherever it is"
ecgo -green -bold " $ " -r -green "./update.sh " -r -teal "# to update hubget"
ecgo -green -bold " $ " -r -green "sudo cp hubget /usr/local/bin/ " -r -teal "# to install"
exit 0
elif [[ "$NEW_VERSION" -eq "$CUR_VERSION" ]]; then
ecgo --info "hubget is at latest version: $NEW_V"
else
ecgo --warning "Your version is greater then the current version! you should update this!"
fi
exit 0
}
clear_bug() {
only_one_arg
ecgo --info "Clearing: " -teal "$BUG_FILE"
if [ ! -f $BUG_FILE ]; then
ecgo --warning "$BUG_FILE does not exist!"
ecgo " no bugs reported yet."
exit 0
fi
cat /dev/null > $BUG_FILE
ecgo -blue "=> File cleared: $BUG_FILE"
exit 0
}
source ~/easy-clone/auto-complete.sh
if [ -z "$1" ]; then
help_menu
fi
check_arg() {
verb "Checking if: ${OPTION} is a valid option..."
if [ "$OPTION" == "-h" ] || [ "$OPTION" == "--help" ]; then
help_menu
return
elif [ "$OPTION" == "-c" ] || [ "$OPTION" == "--check-update" ]; then
check_update
return
elif [ "$OPTION" == "-v" ] || [ "$OPTION" == "--verbose" ]; then
VERB="true"
return
elif [ "$OPTION" == "-n" ] || [ "$OPTION" == "--no-update" ]; then
NO_UP="true"
return
elif [ "$OPTION" == "--clearbug" ] || [ "$OPTION" == "--clear-bug" ]; then
clear_bug
return
elif [ "$OPTION" == "-i" ] || [ "$OPTION" == "--info" ]; then
print_info
return
elif [ "$OPTION" == "--version" ]; then
print_version
return
# elif [[ "$OPTION" == "-"* ]]; then
elif [[ $(ecgo -a "$OPTION" | strcomp -s="-") == 0 ]]; then
verb "Option (${OPTION}) starts with a -"
verb "Exiting"
ecgo --fatal "Option not found! :P ${OPTION}"
# ecgo -red "Option not found! :P $OPTION"
exit 1
else
verb "Option: (${OPTION}) must be the repo name"
REPO="$OPTION"
fi
}
for OPTION in "$@"; do
verb "OPTION = ${OPTION}"
check_arg
done
if [ "$VERB" = "true" ] && [ "$REPO" = "" ]; then
ecgo --fatal "Not enough arguments!"
exit 100
fi
verb "Repo name must be: ${REPO}"
if [ "$NO_UP" == "true" ]; then
if [ -z $2 ]; then
ecgo --fatal "$OPTION flag needs at least two arguments!"
ecgo -red " only revived one!"
ecgo -red " eg. >> $ hubget $OPTION WestleyK/easy-clone"
exit 1
fi
fi
ecgo --info "Adding (${REPO}) to github URL."
URL="$( ecgo -a "https://github.com/"$REPO )"
if [[ $(ecgo -a "$URL" | strcomp -c=".git") == 0 ]]; then
ecgo --info "Removing '.git' from url..."
URL=` ecgo -a "$URL" | rev | strcomp --cut-start=4 | rev `
fi
NAME=` ecgo "$URL" | rev | cut -f1 -d"/" | rev `
ecgo --info "Repo name: " -yellow "'$NAME'"
if [ -d $NAME ]; then
ecgo --fatal "ERROR: $NAME: already exist!"
exit 1
fi
ecgo --info "Checking URL: " -blue "${URL}"
IF_URL=` curl -s --head $URL | head -1 `
#if [[ "$IF_URL" != *"OK"* ]]; then
if [[ $(ecgo -a "$IF_URL" | strcomp -c="OK") != 0 ]]; then
ecgo --fatal "ERROR: URL not found: ${URL}"
exit 1
fi
ecgo --info "Cloning repo..."
ecgo -n -green -s
git clone $URL
ecgo -n -r
IS_FILE=` cat ~/easy-clone/option-url.txt `
if [[ "$IS_FILE" != *"$REPO"* ]]; then
ecgo --info "Adding $REPO to: ~/easy-clone/option-url.txt"
ecgo $REPO >> ~/easy-clone/option-url.txt
fi
if [ "$NO_UP" != "true" ]; then
ecgo --info "Checking for update, because no '-nu' or '-no_update' flag was specified."
check_update
exit 0
fi
ecgo -green "Done."
exit 0
#
# End script
#