Skip to content

Commit

Permalink
Merge pull request #36 from Akianonymus/master
Browse files Browse the repository at this point in the history
Resolve #35 | Default installation from latest releases
  • Loading branch information
labbots committed May 6, 2020
2 parents 631622e + e960aa0 commit 6380e3d
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 30 deletions.
18 changes: 11 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,17 @@ This script does not have very many dependencies. Most of the dependencies are a

### Default values set by installation script

Default branch: `labbots/google-drive-upload`
Repo: `labbots/google-drive-upload`

Default command name: `gupload`
Command name: `gupload`

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

Default branch: `master`
Source: `release` ( can be `branch` )

Default shell file: `.bashrc` or `.zshrc` or `.profile`
Source value: `latest` ( can be `branchname` )

Shell file: `.bashrc` or `.zshrc` or `.profile`

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

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

-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.

-b | --branch <branch_name> - Specify branch name for the github repo, applies to custom and default repo both.
-B | --branch <branch_name> - Specify branch name for the github repo, applies to custom and default repo both.

-R | --release <tag/release_tag> - Specify tag name for the github repo, applies to custom and default repo both.

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

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

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

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

## Updation

Expand Down
57 changes: 38 additions & 19 deletions install.sh
Original file line number Diff line number Diff line change
Expand Up @@ -70,10 +70,17 @@ detectProfile() {
}

# scrape latest commit from a repo on github
# Usage: getLatestSHA username/reponame branchname
getLatestSHA() {
declare REPO="$1" BRANCH="${2:-master}" LATEST_SHA
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}")"
declare LATEST_SHA
case "$TYPE" in
branch)
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}")"
;;
release)
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" "${_/\"*/}")"
;;
esac

echo "$LATEST_SHA"
}

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

# Start a interactive session, asks for all the varibles, exit if running in a non-tty
startInteractive() {
__VALUES_ARRAY=(REPO COMMAND_NAME INSTALL_PATH BRANCH SHELL_RC)
__VALUES_ARRAY=(REPO COMMAND_NAME INSTALL_PATH TYPE TYPE_VALUE SHELL_RC)
printf "%s\n" "Starting Interactive mode.."
printf "%s\n" "Press return for default values.."
for i in "${__VALUES_ARRAY[@]}"; do
Expand All @@ -123,21 +131,21 @@ install() {
mkdir -p "$INSTALL_PATH"
printf 'Installing google-drive-upload..\n'
printf "Fetching latest sha..\n"
LATEST_CURRENT_SHA="$(getLatestSHA "$REPO" "$BRANCH")"
LATEST_CURRENT_SHA="$(getLatestSHA "$REPO" "$TYPE" "$TYPE_VALUE")"
clearLine 1
printf "Latest sha fetched\n" && printf "Downloading script..\n"
if curl -s --compressed https://raw.githubusercontent.com/"$REPO"/"$BRANCH"/upload.sh -o "$INSTALL_PATH"/"$COMMAND_NAME"; then
if curl -Ls --compressed https://raw.githubusercontent.com/"$REPO"/"$LATEST_CURRENT_SHA"/upload.sh -o "$INSTALL_PATH"/"$COMMAND_NAME"; then
chmod +x "$INSTALL_PATH"/"$COMMAND_NAME"
printf "\n%s" "PATH=$PATH/:""$INSTALL_PATH""" >> "$SHELL_RC"
__VALUES_ARRAY=(REPO COMMAND_NAME INSTALL_PATH BRANCH SHELL_RC)
__VALUES_ARRAY=(REPO COMMAND_NAME INSTALL_PATH TYPE TYPE_VALUE SHELL_RC)
for i in "${__VALUES_ARRAY[@]}"; do
updateConfig "$i" "${!i}" "$INFO_PATH"/google-drive-upload.info
done
updateConfig LATEST_INSTALLED_SHA "$LATEST_CURRENT_SHA" "$INFO_PATH"/google-drive-upload.info
clearLine 1
printf "Installed Successfully, Command name: %s\n" "$COMMAND_NAME"
printf "To use the command, do\n"
printf "\"source %s\" or restart your terminal.\n" "$SHELL_RC"
printf "source %s or restart your terminal.\n" "$SHELL_RC"
printf "To update the script in future, just run upload -U/--update.\n"
else
clearLine 1
Expand All @@ -149,7 +157,7 @@ install() {
# Update the script
update() {
printf "Fetching latest version info..\n"
LATEST_CURRENT_SHA="$(getLatestSHA "$REPO" "$BRANCH")"
LATEST_CURRENT_SHA="$(getLatestSHA "$REPO" "$TYPE" "$TYPE_VALUE")"
if [[ -z "$LATEST_CURRENT_SHA" ]]; then
printf "Cannot fetch remote latest version.\n"
exit 1
Expand All @@ -159,17 +167,21 @@ update() {
printf "Latest google-drive-upload already installed.\n"
else
printf "Updating...\n"
curl --compressed -s https://raw.githubusercontent.com/"$REPO"/"$BRANCH"/upload.sh -o "$INSTALL_PATH"/"$COMMAND_NAME"
curl --compressed -Ls https://raw.githubusercontent.com/"$REPO"/"$LATEST_CURRENT_SHA"/upload.sh -o "$INSTALL_PATH"/"$COMMAND_NAME"
updateConfig LATEST_INSTALLED_SHA "$LATEST_CURRENT_SHA" "$INFO_PATH"/google-drive-upload.info
clearLine 1
__VALUES_ARRAY=(REPO COMMAND_NAME INSTALL_PATH TYPE TYPE_VALUE SHELL_RC)
for i in "${__VALUES_ARRAY[@]}"; do
updateConfig "$i" "${!i}" "$INFO_PATH"/google-drive-upload.info
done
printf 'Successfully Updated.\n\n'
fi
}

# Setup the varibles and process getopts flags.
setupArguments() {
[[ $# = 0 ]] && printf "%s: Missing arguments\n" "${FUNCNAME[0]}" && return 1
SHORTOPTS=":Dhip:r:c:b:s:-:"
SHORTOPTS=":Dhip:r:c:RB:s:-:"
while getopts "${SHORTOPTS}" OPTION; do
case "$OPTION" in
# Parse longoptions # https://stackoverflow.com/questions/402377/using-getopts-to-process-long-and-short-command-line-options/28466267#28466267
Expand Down Expand Up @@ -202,7 +214,13 @@ setupArguments() {
;;
branch)
checkLongoptions
BRANCH="${!OPTIND}" && OPTIND=$((OPTIND + 1))
TYPE_VALUE="${!OPTIND}" && OPTIND=$((OPTIND + 1))
TYPE=branch
;;
release)
checkLongoptions
TYPE_VALUE="${!OPTIND}" && OPTIND=$((OPTIND + 1))
TYPE=release
;;
shell-rc)
checkLongoptions
Expand Down Expand Up @@ -232,23 +250,24 @@ setupArguments() {
fi
;;
p)

INSTALL_PATH="$OPTARG"
;;
r)

REPO="$OPTARG"
;;
c)

COMMAND_NAME="$OPTARG"
;;
b)
B)
TYPE=branch
TYPE_VALUE="$OPTARG"
;;

BRANCH="$OPTARG"
R)
TYPE=release
TYPE_VALUE="$OPTARG"
;;
s)

SHELL_RC="$OPTARG"
;;
D)
Expand Down
17 changes: 13 additions & 4 deletions upload.sh
Original file line number Diff line number Diff line change
Expand Up @@ -75,15 +75,24 @@ dirname() {
# Update the script
update() {
printf 'Fetching update script..\n'
declare REPO="labbots/google-drive-upload" BRANCH="master"
# shellcheck source=/dev/null
if [[ -f "$HOME/.google-drive-upload/google-drive-upload.info" ]]; then
source "$HOME/.google-drive-upload/google-drive-upload.info"
fi
if __SCRIPT="$(curl --compressed -s https://raw.githubusercontent.com/"$REPO"/"$BRANCH"/install.sh)"; then
bash <<< "$__SCRIPT"
declare REPO="${REPO:-labbots/google-drive-upload}" TYPE_VALUE="${TYPE_VALUE:-latest}"
if [[ $TYPE = branch ]]; then
if __SCRIPT="$(curl --compressed -Ls https://raw.githubusercontent.com/"$REPO"/"$TYPE_VALUE"/install.sh)"; then
bash <<< "$__SCRIPT"
else
printf "Error: Cannot download update script..\n"
fi
else
printf "Error: Cannot download update script..\n"
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" "${_/\"*/}")"
if __SCRIPT="$(curl --compressed -Ls https://raw.githubusercontent.com/"$REPO"/"$LATEST_SHA"/install.sh)"; then
bash <<< "$__SCRIPT"
else
printf "Error: Cannot download update script..\n"
fi
fi
}

Expand Down

0 comments on commit 6380e3d

Please sign in to comment.