Skip to content

Commit 6380e3d

Browse files
authored
Merge pull request #36 from Akianonymus/master
Resolve #35 | Default installation from latest releases
2 parents 631622e + e960aa0 commit 6380e3d

File tree

3 files changed

+62
-30
lines changed

3 files changed

+62
-30
lines changed

README.md

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -32,15 +32,17 @@ This script does not have very many dependencies. Most of the dependencies are a
3232

3333
### Default values set by installation script
3434

35-
Default branch: `labbots/google-drive-upload`
35+
Repo: `labbots/google-drive-upload`
3636

37-
Default command name: `gupload`
37+
Command name: `gupload`
3838

39-
Default installation path: `$HOME/.google-drive-upload`
39+
Installation path: `$HOME/.google-drive-upload`
4040

41-
Default branch: `master`
41+
Source: `release` ( can be `branch` )
4242

43-
Default shell file: `.bashrc` or `.zshrc` or `.profile`
43+
Source value: `latest` ( can be `branchname` )
44+
45+
Shell file: `.bashrc` or `.zshrc` or `.profile`
4446

4547
For custom command names, repo, shell file, etc, see advanced installation method.
4648

@@ -72,7 +74,9 @@ These are the flags that are available in the install.sh script:
7274

7375
-r | --repo <Username/reponame> - Upload script from your custom repo,e.g --repo labbots/google-drive-upload, make sure your repo file structure is same as official repo.
7476

75-
-b | --branch <branch_name> - Specify branch name for the github repo, applies to custom and default repo both.
77+
-B | --branch <branch_name> - Specify branch name for the github repo, applies to custom and default repo both.
78+
79+
-R | --release <tag/release_tag> - Specify tag name for the github repo, applies to custom and default repo both.
7680

7781
-s | --shell-rc <shell_file> - Specify custom rc file, where PATH is appended, by default script detects .zshrc and .bashrc.
7882

@@ -82,7 +86,7 @@ These are the flags that are available in the install.sh script:
8286

8387
Now, run the script and use flags according to your usecase.
8488

85-
E.g: `bash install.sh -r username/reponame -p somepath -s shell_file -c command_name -b branch_name`
89+
E.g: `bash install.sh -r username/reponame -p somepath -s shell_file -c command_name -B branch_name`
8690

8791
## Updation
8892

install.sh

Lines changed: 38 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -70,10 +70,17 @@ detectProfile() {
7070
}
7171

7272
# scrape latest commit from a repo on github
73-
# Usage: getLatestSHA username/reponame branchname
7473
getLatestSHA() {
75-
declare REPO="$1" BRANCH="${2:-master}" LATEST_SHA
76-
LATEST_SHA="$(hash="$(curl --compressed -s https://github.com/"$REPO"/commits/"$BRANCH".atom -r 0-1000 | grep "Commit\\/")" && read -r firstline <<< "$hash" && regex="(/.*<)" && [[ $firstline =~ $regex ]] && echo "${BASH_REMATCH[1]:1:-1}")"
74+
declare LATEST_SHA
75+
case "$TYPE" in
76+
branch)
77+
LATEST_SHA="$(hash="$(curl --compressed -s https://github.com/"$REPO"/commits/"$TYPE_VALUE".atom -r 0-1000 | grep "Commit\\/")" && read -r firstline <<< "$hash" && regex="(/.*<)" && [[ $firstline =~ $regex ]] && echo "${BASH_REMATCH[1]:1:-1}")"
78+
;;
79+
release)
80+
LATEST_SHA="$(hash="$(curl -L --compressed -s https://github.com/"$REPO"/releases/"$TYPE_VALUE" | grep "=\"/"$REPO"/commit")" && read -r firstline <<< "$hash" && : "${hash/*commit\//}" && printf "%s\n" "${_/\"*/}")"
81+
;;
82+
esac
83+
7784
echo "$LATEST_SHA"
7885
}
7986

@@ -90,7 +97,8 @@ variables() {
9097
COMMAND_NAME="gupload"
9198
INFO_PATH="$HOME/.google-drive-upload"
9299
INSTALL_PATH="$HOME/.google-drive-upload/bin"
93-
BRANCH="master"
100+
TYPE="release"
101+
TYPE_VALUE="latest"
94102
SHELL_RC="$(detectProfile)"
95103
# shellcheck source=/dev/null
96104
if [[ -f "$INFO_PATH"/google-drive-upload.info ]]; then
@@ -100,7 +108,7 @@ variables() {
100108

101109
# Start a interactive session, asks for all the varibles, exit if running in a non-tty
102110
startInteractive() {
103-
__VALUES_ARRAY=(REPO COMMAND_NAME INSTALL_PATH BRANCH SHELL_RC)
111+
__VALUES_ARRAY=(REPO COMMAND_NAME INSTALL_PATH TYPE TYPE_VALUE SHELL_RC)
104112
printf "%s\n" "Starting Interactive mode.."
105113
printf "%s\n" "Press return for default values.."
106114
for i in "${__VALUES_ARRAY[@]}"; do
@@ -123,21 +131,21 @@ install() {
123131
mkdir -p "$INSTALL_PATH"
124132
printf 'Installing google-drive-upload..\n'
125133
printf "Fetching latest sha..\n"
126-
LATEST_CURRENT_SHA="$(getLatestSHA "$REPO" "$BRANCH")"
134+
LATEST_CURRENT_SHA="$(getLatestSHA "$REPO" "$TYPE" "$TYPE_VALUE")"
127135
clearLine 1
128136
printf "Latest sha fetched\n" && printf "Downloading script..\n"
129-
if curl -s --compressed https://raw.githubusercontent.com/"$REPO"/"$BRANCH"/upload.sh -o "$INSTALL_PATH"/"$COMMAND_NAME"; then
137+
if curl -Ls --compressed https://raw.githubusercontent.com/"$REPO"/"$LATEST_CURRENT_SHA"/upload.sh -o "$INSTALL_PATH"/"$COMMAND_NAME"; then
130138
chmod +x "$INSTALL_PATH"/"$COMMAND_NAME"
131139
printf "\n%s" "PATH=$PATH/:""$INSTALL_PATH""" >> "$SHELL_RC"
132-
__VALUES_ARRAY=(REPO COMMAND_NAME INSTALL_PATH BRANCH SHELL_RC)
140+
__VALUES_ARRAY=(REPO COMMAND_NAME INSTALL_PATH TYPE TYPE_VALUE SHELL_RC)
133141
for i in "${__VALUES_ARRAY[@]}"; do
134142
updateConfig "$i" "${!i}" "$INFO_PATH"/google-drive-upload.info
135143
done
136144
updateConfig LATEST_INSTALLED_SHA "$LATEST_CURRENT_SHA" "$INFO_PATH"/google-drive-upload.info
137145
clearLine 1
138146
printf "Installed Successfully, Command name: %s\n" "$COMMAND_NAME"
139147
printf "To use the command, do\n"
140-
printf "\"source %s\" or restart your terminal.\n" "$SHELL_RC"
148+
printf "source %s or restart your terminal.\n" "$SHELL_RC"
141149
printf "To update the script in future, just run upload -U/--update.\n"
142150
else
143151
clearLine 1
@@ -149,7 +157,7 @@ install() {
149157
# Update the script
150158
update() {
151159
printf "Fetching latest version info..\n"
152-
LATEST_CURRENT_SHA="$(getLatestSHA "$REPO" "$BRANCH")"
160+
LATEST_CURRENT_SHA="$(getLatestSHA "$REPO" "$TYPE" "$TYPE_VALUE")"
153161
if [[ -z "$LATEST_CURRENT_SHA" ]]; then
154162
printf "Cannot fetch remote latest version.\n"
155163
exit 1
@@ -159,17 +167,21 @@ update() {
159167
printf "Latest google-drive-upload already installed.\n"
160168
else
161169
printf "Updating...\n"
162-
curl --compressed -s https://raw.githubusercontent.com/"$REPO"/"$BRANCH"/upload.sh -o "$INSTALL_PATH"/"$COMMAND_NAME"
170+
curl --compressed -Ls https://raw.githubusercontent.com/"$REPO"/"$LATEST_CURRENT_SHA"/upload.sh -o "$INSTALL_PATH"/"$COMMAND_NAME"
163171
updateConfig LATEST_INSTALLED_SHA "$LATEST_CURRENT_SHA" "$INFO_PATH"/google-drive-upload.info
164172
clearLine 1
173+
__VALUES_ARRAY=(REPO COMMAND_NAME INSTALL_PATH TYPE TYPE_VALUE SHELL_RC)
174+
for i in "${__VALUES_ARRAY[@]}"; do
175+
updateConfig "$i" "${!i}" "$INFO_PATH"/google-drive-upload.info
176+
done
165177
printf 'Successfully Updated.\n\n'
166178
fi
167179
}
168180

169181
# Setup the varibles and process getopts flags.
170182
setupArguments() {
171183
[[ $# = 0 ]] && printf "%s: Missing arguments\n" "${FUNCNAME[0]}" && return 1
172-
SHORTOPTS=":Dhip:r:c:b:s:-:"
184+
SHORTOPTS=":Dhip:r:c:RB:s:-:"
173185
while getopts "${SHORTOPTS}" OPTION; do
174186
case "$OPTION" in
175187
# Parse longoptions # https://stackoverflow.com/questions/402377/using-getopts-to-process-long-and-short-command-line-options/28466267#28466267
@@ -202,7 +214,13 @@ setupArguments() {
202214
;;
203215
branch)
204216
checkLongoptions
205-
BRANCH="${!OPTIND}" && OPTIND=$((OPTIND + 1))
217+
TYPE_VALUE="${!OPTIND}" && OPTIND=$((OPTIND + 1))
218+
TYPE=branch
219+
;;
220+
release)
221+
checkLongoptions
222+
TYPE_VALUE="${!OPTIND}" && OPTIND=$((OPTIND + 1))
223+
TYPE=release
206224
;;
207225
shell-rc)
208226
checkLongoptions
@@ -232,23 +250,24 @@ setupArguments() {
232250
fi
233251
;;
234252
p)
235-
236253
INSTALL_PATH="$OPTARG"
237254
;;
238255
r)
239-
240256
REPO="$OPTARG"
241257
;;
242258
c)
243-
244259
COMMAND_NAME="$OPTARG"
245260
;;
246-
b)
261+
B)
262+
TYPE=branch
263+
TYPE_VALUE="$OPTARG"
264+
;;
247265

248-
BRANCH="$OPTARG"
266+
R)
267+
TYPE=release
268+
TYPE_VALUE="$OPTARG"
249269
;;
250270
s)
251-
252271
SHELL_RC="$OPTARG"
253272
;;
254273
D)

upload.sh

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -75,15 +75,24 @@ dirname() {
7575
# Update the script
7676
update() {
7777
printf 'Fetching update script..\n'
78-
declare REPO="labbots/google-drive-upload" BRANCH="master"
7978
# shellcheck source=/dev/null
8079
if [[ -f "$HOME/.google-drive-upload/google-drive-upload.info" ]]; then
8180
source "$HOME/.google-drive-upload/google-drive-upload.info"
8281
fi
83-
if __SCRIPT="$(curl --compressed -s https://raw.githubusercontent.com/"$REPO"/"$BRANCH"/install.sh)"; then
84-
bash <<< "$__SCRIPT"
82+
declare REPO="${REPO:-labbots/google-drive-upload}" TYPE_VALUE="${TYPE_VALUE:-latest}"
83+
if [[ $TYPE = branch ]]; then
84+
if __SCRIPT="$(curl --compressed -Ls https://raw.githubusercontent.com/"$REPO"/"$TYPE_VALUE"/install.sh)"; then
85+
bash <<< "$__SCRIPT"
86+
else
87+
printf "Error: Cannot download update script..\n"
88+
fi
8589
else
86-
printf "Error: Cannot download update script..\n"
90+
declare LATEST_SHA="$(hash="$(curl -L --compressed -s https://github.com/"$REPO"/releases/"$TYPE_VALUE" | grep "=\"/"$REPO"/commit")" && read -r firstline <<< "$hash" && : "${hash/*commit\//}" && printf "%s\n" "${_/\"*/}")"
91+
if __SCRIPT="$(curl --compressed -Ls https://raw.githubusercontent.com/"$REPO"/"$LATEST_SHA"/install.sh)"; then
92+
bash <<< "$__SCRIPT"
93+
else
94+
printf "Error: Cannot download update script..\n"
95+
fi
8796
fi
8897
}
8998

0 commit comments

Comments
 (0)