From e960aa07e5e3e6792c9f19ef6867afb85cfc1779 Mon Sep 17 00:00:00 2001 From: Akianonymus Date: Wed, 6 May 2020 23:17:11 +0530 Subject: [PATCH] Resolve #35 | Default installation from latest releases --- README.md | 18 ++++++++++------- install.sh | 57 ++++++++++++++++++++++++++++++++++++------------------ upload.sh | 17 ++++++++++++---- 3 files changed, 62 insertions(+), 30 deletions(-) diff --git a/README.md b/README.md index c85cdd1..07f09d9 100755 --- a/README.md +++ b/README.md @@ -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. @@ -72,7 +74,9 @@ These are the flags that are available in the install.sh script: -r | --repo - 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 - Specify branch name for the github repo, applies to custom and default repo both. + -B | --branch - Specify branch name for the github repo, applies to custom and default repo both. + + -R | --release - Specify tag name for the github repo, applies to custom and default repo both. -s | --shell-rc - Specify custom rc file, where PATH is appended, by default script detects .zshrc and .bashrc. @@ -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 diff --git a/install.sh b/install.sh index 720a1fd..9617452 100755 --- a/install.sh +++ b/install.sh @@ -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" } @@ -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 @@ -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 @@ -123,13 +131,13 @@ 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 @@ -137,7 +145,7 @@ install() { 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 @@ -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 @@ -159,9 +167,13 @@ 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 } @@ -169,7 +181,7 @@ update() { # 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 @@ -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 @@ -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) diff --git a/upload.sh b/upload.sh index bc133a2..c2ed60a 100755 --- a/upload.sh +++ b/upload.sh @@ -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 }