Skip to content

Commit

Permalink
Add -U/--uninstall flag
Browse files Browse the repository at this point in the history
  • Loading branch information
Akianonymus committed May 13, 2020
1 parent 04059f9 commit 72c426b
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 13 deletions.
23 changes: 23 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
- [Upload](#upload)
- [Custom Flags](#custom-flags)
- [Resuming Interrupted Uploads](#resuming-interrupted-uploads)
- [Uninstall](#Uninstall)
- [Inspired By](#inspired-by)
- [License](#license)

Expand Down Expand Up @@ -343,6 +344,28 @@ Uploads interrupted either due to bad internet connection or manual interruption
- No progress bars for resumable uploads as it messes up with output.
- You can interrupt many times you want, it will resume ( hopefully ).

## Uninstall

If you have followed the automatic method to install the script, then you can automatically uninstall the script.

There are two methods:

1. Use the script itself to uninstall the script.

`gupload -U or gupload --uninstall`

This will remove the script related files and remove path change from shell file.

2. Run the installation script again with -U/--uninstall flag

```shell
bash <(curl --compressed -s https://raw.githubusercontent.com/labbots/google-drive-upload/master/install.sh) --uninstall
```

Yes, just run the installation script again with the flag and voila, it's done.
**Note: Both above methods obeys the values set by user in advanced installation,**
## Inspired By
- [github-bashutils](https://github.com/soulseekah/bash-utils) - soulseekah/bash-utils
Expand Down
41 changes: 35 additions & 6 deletions install.sh
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#!/usr/bin/env bash
# Install or Update google-drive-upload
# Install, Update or Uninstall google-drive-upload

usage() {
printf "
Expand All @@ -16,6 +16,7 @@ Options:\n
-R | --release <tag/release_tag> - Specify tag name for the github repo, applies to custom and default repo both.\n
-B | --branch <branch_name> - Specify branch name for the github repo, applies to custom and default repo both.\n
-s | --shell-rc <shell_file> - Specify custom rc file, where PATH is appended, by default script detects .zshrc and .bashrc.\n
-U | --uninstall - Uninstall the script and remove related files.\n
-D | --debug - Display script command trace.\n
-h | --help - Display usage instructions.\n\n" "${0##*/}" "${HOME}"
exit 0
Expand Down Expand Up @@ -194,6 +195,21 @@ update() {
fi
}

# Uninstall the script
uninstall() {
printf "Uninstalling..\n"
__bak="source ${INFO_PATH}/google-drive-upload.binpath"
if sed -i "s|${__bak}||g" "${SHELL_RC}"; then
rm -f "${INSTALL_PATH}/${COMMAND_NAME}"
rm -f "${INFO_PATH}/google-drive-upload.info"
rm -f "${INFO_PATH}/google-drive-upload.binpath"
clearLine 1
printf "Uninstall complete\n"
else
printf 'Error: Uninstall failed\n'
fi
}

# Setup the varibles and process getopts flags.
setupArguments() {
[[ $# = 0 ]] && printf "%s: Missing arguments\n" "${FUNCNAME[0]}" && return 1
Expand Down Expand Up @@ -242,6 +258,9 @@ setupArguments() {
checkLongoptions
SHELL_RC="${!OPTIND}" && OPTIND=$((OPTIND + 1))
;;
uninstall)
UNINSTALL="true"
;;
debug)
DEBUG=true
;;
Expand Down Expand Up @@ -278,14 +297,16 @@ setupArguments() {
TYPE=branch
TYPE_VALUE="${OPTARG}"
;;

R)
TYPE=release
TYPE_VALUE="${OPTARG}"
;;
s)
SHELL_RC="${OPTARG}"
;;
U)
UNINSTALL="true"
;;
D)
DEBUG=true
;;
Expand Down Expand Up @@ -346,11 +367,19 @@ main() {
if [[ -n ${INTERACTIVE} ]]; then
startInteractive
fi

if type -a "${COMMAND_NAME}" &> /dev/null; then
update
if [[ -n ${UNINSTALL} ]]; then
if type -a "${COMMAND_NAME}" &> /dev/null; then
uninstall
else
printf "google-drive-upload is not installed\n"
exit 1
fi
else
install
if type -a "${COMMAND_NAME}" &> /dev/null; then
update
else
install
fi
fi
}

Expand Down
22 changes: 15 additions & 7 deletions upload.sh
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ Options:\n
-V | --verbose-progress - Display detailed message and detailed upload progress(only for non-parallel uploads).\n
-u | --update - Update the installed script in your system.\n
--info - Show detailed info, only if script is installed system wide.\n
-U | --uninstall - Uninstall script, remove related files.\n
-D | --debug - Display script command trace.\n
-h | --help - Display usage instructions.\n" "${0##*/}"
exit 0
Expand Down Expand Up @@ -86,28 +87,29 @@ dirname() {
printf '%s\n' "${tmp:-/}"
}

# Update ( install ) the script
# Update ( install, uninstall ) the script
update() {
printf 'Fetching update script..\n'
declare job="${1}"
printf 'Fetching %s script..\n' "${job:-update}"
# 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
declare REPO="${REPO:-labbots/google-drive-upload}" TYPE_VALUE="${TYPE_VALUE:-latest}"
if [[ ${TYPE} = branch ]]; then
if __SCRIPT="$(curlCmd -Ls "https://raw.githubusercontent.com/${REPO}/${TYPE_VALUE}/install.sh")"; then
bash <<< "${__SCRIPT}"
bash <(printf "%s\n" "${__SCRIPT}") --"${job:-}"
else
printf "Error: Cannot download update script..\n"
printf "Error: Cannot download %s script..\n" "${job:-update}"
fi
else
declare LATEST_SHA
LATEST_SHA="$(hash="$(curlCmd -L -s "https://github.com/${REPO}/releases/${TYPE_VALUE}" | grep "=\"/""${REPO}""/commit")" &&
read -r firstline <<< "${hash}" && : "${hash/*commit\//}" && printf "%s\n" "${_/\"*/}")"
if __SCRIPT="$(curlCmd -Ls "https://raw.githubusercontent.com/${REPO}/${LATEST_SHA}/install.sh")"; then
bash <<< "${__SCRIPT}"
bash <(printf "%s\n" "${__SCRIPT}") --"${job:-}"
else
printf "Error: Cannot download update script..\n"
printf "Error: Cannot download %s script..\n" "${job:-update}"
fi
fi
}
Expand Down Expand Up @@ -559,7 +561,7 @@ setupArguments() {
REDIRECT_URI="urn:ietf:wg:oauth:2.0:oob"
TOKEN_URL="https://accounts.google.com/o/oauth2/token"

SHORTOPTS=":qvVi:sp:odf:Shur:C:Dz:-:"
SHORTOPTS=":qvVi:sp:odf:ShuUr:C:Dz:-:"
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 All @@ -573,6 +575,9 @@ setupArguments() {
update)
update && exit $?
;;
uninstall)
update uninstall && exit $?
;;
info)
versionInfo && exit $?
;;
Expand Down Expand Up @@ -658,6 +663,9 @@ setupArguments() {
u)
update && exit $?
;;
U)
update uninstall && exit $?
;;
C)
FOLDERNAME="${OPTARG}"
;;
Expand Down

0 comments on commit 72c426b

Please sign in to comment.