Skip to content

Commit

Permalink
Merge pull request #81 from Akianonymus/fix
Browse files Browse the repository at this point in the history
Add flag to limit speed | Add github action check for installation | Bugfixes | Misc
  • Loading branch information
labbots committed Jul 3, 2020
2 parents f7a0987 + 37f82b1 commit 1f26e65
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 32 deletions.
24 changes: 24 additions & 0 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
name: Checks
on:
push:
branches:
- '**'
paths-ignore:
- '*.md'
pull_request:
branches:
- '**'
paths-ignore:
- '*.md'
jobs:
installation_and_download:
name: Installation
runs-on: ubuntu-latest
if: "!contains(github.event.head_commit.message, '[SKIP-CI]')"
steps:
- uses: actions/checkout@v1
- name: 'Install Google drive upload'
run: |
bash install.sh
bash install.sh -B master -c custom_gupload
sudo bash install.sh -p /usr/bin -c custom_gupload
14 changes: 12 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
<h1 align="center">Google drive upload</h1>
<p align="center">
<a href="https://github.com/labbots/google-drive-upload/stargazers"><img src="https://img.shields.io/github/stars/labbots/google-drive-upload.svg?color=blueviolet&style=for-the-badge" alt="Stars"></a>
<a href="https://github.com/labbots/google-drive-upload/releases"><img src="https://img.shields.io/github/release/labbots/google-drive-upload.svg?style=for-the-badge" alt="Latest Release"></a>
<a href="https://github.com/labbots/google-drive-upload/stargazers"><img src="https://img.shields.io/github/stars/labbots/google-drive-upload.svg?color=blueviolet&style=for-the-badge" alt="Stars"></a>
<a href="https://github.com/labbots/google-drive-upload/blob/master/LICENSE"><img src="https://img.shields.io/github/license/labbots/google-drive-upload.svg?style=for-the-badge" alt="License"></a>
</p>
<p align="center">
<a href="https://www.codacy.com/manual/labbots/google-drive-upload?utm_source=github.com&amp;utm_medium=referral&amp;utm_content=labbots/google-drive-upload&amp;utm_campaign=Badge_Grade"><img alt="Codacy grade" src="https://img.shields.io/codacy/grade/55b1591a28af473886c8dfdb3f2c9123?style=for-the-badge"></a>
<a href="https://github.com/labbots/google-drive-upload/actions"><img alt="Github Action Checks" src="https://img.shields.io/github/workflow/status/labbots/google-drive-upload/Checks?label=CI%20Checks&style=for-the-badge"></a>
</p>
</p>
<p align="center">
<a href="https://github.com/labbots/google-drive-upload/blob/master/LICENSE"><img src="https://img.shields.io/github/license/labbots/google-drive-upload.svg?style=for-the-badge" alt="License"></a>
<a href="https://plant.treeware.earth/labbots/google-drive-upload"><img alt="Buy us a tree" src="https://img.shields.io/treeware/trees/labbots/google-drive-upload?color=green&label=Buy%20us%20a%20Tree%20%F0%9F%8C%B3&style=for-the-badge"></a>
</p>

Expand Down Expand Up @@ -410,6 +414,12 @@ These are the custom flags that are currently implemented:

---

- <strong>--speed 'speed'</strong>

Limit the download speed, supported formats: 1K, 1M and 1G.

---

- <strong>-q | --quiet</strong>

Supress the normal output, only show success/error upload messages for files, and one extra line at the beginning for folder showing no. of files and sub folders.
Expand Down
15 changes: 8 additions & 7 deletions install.sh
Original file line number Diff line number Diff line change
Expand Up @@ -238,12 +238,11 @@ _full_path() {
# Globals: None
# Arguments: 2
# ${1} = repo name
# ${2} = branch or release
# ${3} = branch name or release name
# ${2} = sha sum or branch name or tag name
# Result: print fetched shas
###################################################
_get_files_and_commits() {
declare repo="${1:-${REPO}}" type_value="${2:-${TYPE_VALUE}}"
declare repo="${1:-${REPO}}" type_value="${2:-${LATEST_CURRENT_SHA}}"
declare html commits files

# shellcheck disable=SC2086
Expand Down Expand Up @@ -277,14 +276,16 @@ _get_latest_sha() {
case "${1:-${TYPE}}" in
branch)
LATEST_SHA="$(
hash="$(curl --compressed -s https://github.com/"${3:-${REPO}}"/commits/"${2:-${TYPE_VALUE}}".atom -r 0-2000 | grep "Commit\\/" -m1 || :)"
read -r firstline <<< "${hash}" && regex="(/.*<)" && [[ ${firstline} =~ ${regex} ]] && printf "%s\n" "${BASH_REMATCH[1]:1:-1}"
: "$(curl --compressed -s https://github.com/"${3:-${REPO}}"/commits/"${2:-${TYPE_VALUE}}".atom -r 0-2000)"
: "$(grep "Commit\\/" -m1 <<< "${_}" || :)"
read -r firstline <<< "${_}" && regex="(/.*<)" && [[ ${firstline} =~ ${regex} ]] && printf "%s\n" "${BASH_REMATCH[1]:1:-1}"
)"
;;
release)
LATEST_SHA="$(
hash="$(curl -L --compressed -s https://github.com/"${3:-${REPO}}"/releases/"${2:-${TYPE_VALUE}}" | grep "=\"/""${3:-${REPO}}""/commit" -m1 || :)"
read -r firstline <<< "${hash}" && : "${hash/*commit\//}" && printf "%s\n" "${_/\"*/}"
: "$(curl -L --compressed -s https://github.com/"${3:-${REPO}}"/releases/"${2:-${TYPE_VALUE}}")"
: "$(grep "=\"/""${3:-${REPO}}""/commit" -m1 <<< "${_}" || :)"
: "${_/*commit\//}" && printf "%s\n" "${_/\"*/}"
)"
;;
esac
Expand Down
42 changes: 26 additions & 16 deletions upload.sh
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ Options:\n
-o | --overwrite - Overwrite the files with the same name, if present in the root folder/input folder, also works with recursive folders.\n
-d | --skip-duplicates - Do not upload the files with the same name, if already present in the root folder/input folder, also works with recursive folders.\n
-S | --share <optional_email_address>- Share the uploaded input file/folder, grant reader permission to provided email address or to everyone with the shareable link.\n
--speed 'speed' - Limit the download speed, supported formats: 1K, 1M and 1G.\n
-i | --save-info <file_to_save_info> - Save uploaded files info to the given filename.\n
-z | --config <config_path> - Override default config file with custom config file.\nIf you want to change default value, then use this format -z/--config default=default=your_config_file_path.\n
-q | --quiet - Supress the normal output, only show success/error upload messages for files, and one extra line at the beginning for folder showing no. of files and sub folders.\n
Expand Down Expand Up @@ -331,6 +332,7 @@ _upload_file() {
-o- \
--url "${uploadlink}" \
--globoff \
${CURL_SPEED} \
${CURL_ARGS})" || :
return 0
}
Expand Down Expand Up @@ -404,6 +406,7 @@ _upload_file() {
-T "${input}" \
-o- \
--url "${uploadlink}" \
${CURL_SPEED} \
--globoff)" || :
if [[ -n ${upload_body} ]]; then
_collect_file_info
Expand Down Expand Up @@ -567,7 +570,7 @@ _setup_arguments() {
# De-initialize if any variables set already.
unset FIRST_INPUT FOLDER_INPUT FOLDERNAME LOCAL_INPUT_ARRAY ID_INPUT_ARRAY
unset PARALLEL NO_OF_PARALLEL_JOBS SHARE SHARE_EMAIL OVERWRITE SKIP_DUPLICATES SKIP_SUBDIRS ROOTDIR QUIET
unset VERBOSE VERBOSE_PROGRESS DEBUG LOG_FILE_ID
unset VERBOSE VERBOSE_PROGRESS DEBUG LOG_FILE_ID CURL_SPEED
CURL_ARGS="-#"
INFO_PATH="${HOME}/.google-drive-upload"
INFO_FILE="${INFO_PATH}/google-drive-upload.info"
Expand Down Expand Up @@ -682,6 +685,16 @@ _setup_arguments() {
shift
fi
;;
--speed)
_check_longoptions "${1}" "${2}"
regex='^([0-9]+)([k,K]|[m,M]|[g,G])+$'
if [[ ${2} =~ ${regex} ]]; then
CURL_SPEED="--limit-rate ${2}" && shift
else
printf "Error: Wrong speed limit format, supported formats: 1K , 1M and 1G\n" 1>&2
exit 1
fi
;;
-q | --quiet)
QUIET="_print_center_quiet"
;;
Expand Down Expand Up @@ -1018,15 +1031,14 @@ _process_arguments() {
if [[ -n ${parallel} ]]; then
{ [[ ${NO_OF_PARALLEL_JOBS} -gt ${NO_OF_FILES} ]] && NO_OF_PARALLEL_JOBS_FINAL="${NO_OF_FILES}"; } || { NO_OF_PARALLEL_JOBS_FINAL="${NO_OF_PARALLEL_JOBS}"; }
# Export because xargs cannot access if it is just an internal variable.
export ID CURL_ARGS="-s" ACCESS_TOKEN OVERWRITE COLUMNS API_URL API_VERSION LOG_FILE_ID SKIP_DUPLICATES QUIET UPLOAD_METHOD TMPFILE
export ID CURL_ARGS="-s" ACCESS_TOKEN OVERWRITE COLUMNS API_URL API_VERSION LOG_FILE_ID SKIP_DUPLICATES QUIET UPLOAD_METHOD TMPFILE CURL_SPEED
export -f _upload_file _print_center _clear_line _json_value _url_encode _check_existing_file _print_center_quiet _newline _bytes_to_human

[[ -f ${TMPFILE}SUCCESS ]] && rm "${TMPFILE}"SUCCESS
[[ -f ${TMPFILE}ERROR ]] && rm "${TMPFILE}"ERROR

# shellcheck disable=SC2016
printf "\"%s\"\n" "${FILENAMES[@]}" | xargs -n1 -P"${NO_OF_PARALLEL_JOBS_FINAL}" -i bash -c '
printf "%s\n" "$$" >| "${TMPFILE}"pid"$$"
_upload_file "${UPLOAD_METHOD:-create}" "{}" "${ID}" "${ACCESS_TOKEN}" parallel
' 1>| "${TMPFILE}"SUCCESS 2>| "${TMPFILE}"ERROR &

Expand Down Expand Up @@ -1113,15 +1125,14 @@ _process_arguments() {
if [[ -n ${parallel} ]]; then
{ [[ ${NO_OF_PARALLEL_JOBS} -gt ${NO_OF_FILES} ]] && NO_OF_PARALLEL_JOBS_FINAL="${NO_OF_FILES}"; } || { NO_OF_PARALLEL_JOBS_FINAL="${NO_OF_PARALLEL_JOBS}"; }
# Export because xargs cannot access if it is just an internal variable.
export CURL_ARGS="-s" ACCESS_TOKEN OVERWRITE COLUMNS API_URL API_VERSION LOG_FILE_ID SKIP_DUPLICATES QUIET UPLOAD_METHOD TMPFILE
export CURL_ARGS="-s" ACCESS_TOKEN OVERWRITE COLUMNS API_URL API_VERSION LOG_FILE_ID SKIP_DUPLICATES QUIET UPLOAD_METHOD TMPFILE CURL_SPEED
export -f _upload_file _print_center _clear_line _json_value _url_encode _check_existing_file _print_center_quiet _newline _bytes_to_human

[[ -f "${TMPFILE}"SUCCESS ]] && rm "${TMPFILE}"SUCCESS
[[ -f "${TMPFILE}"ERROR ]] && rm "${TMPFILE}"ERROR

# shellcheck disable=SC2016
printf "\"%s\"\n" "${FINAL_LIST[@]}" | xargs -n1 -P"${NO_OF_PARALLEL_JOBS_FINAL}" -i bash -c '
printf "%s\n" "$$" >| "${TMPFILE}"pid"$$"
LIST="{}"
FILETOUPLOAD="${LIST//*"|:_//_:|"}"
DIRTOUPLOAD="$(: "|:_//_:|""${FILETOUPLOAD}" && : "${LIST::-${#_}}" && printf "%s\n" "${_//*"|:_//_:|"}")"
Expand Down Expand Up @@ -1238,21 +1249,20 @@ main() {
[[ -n ${PARALLEL_UPLOAD} ]] && _setup_tempfile

_cleanup() {
(
if [[ -n ${PARALLEL_UPLOAD} ]]; then
pid_files="$(printf "%b " "${TMPFILE}"pid* && printf "\n")"
pids="${pid_files//${TMPFILE}pid/}"
# shellcheck disable=SC2086
kill -9 ${pids} || :
rm -f "${TMPFILE:?}"*
{
[[ -n ${PARALLEL_UPLOAD} ]] && rm -f "${TMPFILE:?}"*
export abnormal_exit
if [[ -n ${abnormal_exit} ]]; then
kill -- -$$
else
_auto_update
fi
kill -9 $$ || :
) &> /dev/null &
} &> /dev/null || :
return 0
}

trap 'printf "\n" ; exit' SIGINT
trap '_auto_update ; _cleanup' SIGTERM EXIT
trap 'printf "\n" ; abnormal_exit=1; exit' SIGINT SIGTERM
trap '_cleanup' EXIT

START="$(printf "%(%s)T\\n" "-1")"
_print_center "justify" "Starting script" "-"
Expand Down
17 changes: 10 additions & 7 deletions utils.sh
Original file line number Diff line number Diff line change
Expand Up @@ -244,18 +244,21 @@ _full_path() {
# Result: print fetched sha
###################################################
_get_latest_sha() {
[[ $# -lt 3 ]] && printf "%s: Missing arguments\n" "${FUNCNAME[0]}" && return 1
declare LATEST_SHA
case "${1:-${TYPE}}" in
branch)
LATEST_SHA="$(hash="$(curl --compressed -s https://github.com/"${3:-${REPO}}"/commits/"${2:-${TYPE_VALUE}}".atom -r 0-2000 | grep "Commit\\/" -m1 || :)" && {
read -r firstline <<< "${hash}" && regex="(/.*<)" && [[ ${firstline} =~ ${regex} ]] && printf "%s\n" "${BASH_REMATCH[1]:1:-1}"
})"
LATEST_SHA="$(
: "$(curl --compressed -s https://github.com/"${3:-${REPO}}"/commits/"${2:-${TYPE_VALUE}}".atom -r 0-2000)"
: "$(grep "Commit\\/" -m1 <<< "${_}" || :)"
read -r firstline <<< "${_}" && regex="(/.*<)" && [[ ${firstline} =~ ${regex} ]] && printf "%s\n" "${BASH_REMATCH[1]:1:-1}"
)"
;;
release)
LATEST_SHA="$(hash="$(curl -L --compressed -s https://github.com/"${3:-${REPO}}"/releases/"${2:-${TYPE_VALUE}}" | grep "=\"/""${3:-${REPO}}""/commit" -m1 || :)" && {
read -r firstline <<< "${hash}" && : "${hash/*commit\//}" && printf "%s\n" "${_/\"*/}"
})"
LATEST_SHA="$(
: "$(curl -L --compressed -s https://github.com/"${3:-${REPO}}"/releases/"${2:-${TYPE_VALUE}}")"
: "$(grep "=\"/""${3:-${REPO}}""/commit" -m1 <<< "${_}" || :)"
: "${_/*commit\//}" && printf "%s\n" "${_/\"*/}"
)"
;;
esac
printf "%b" "${LATEST_SHA:+${LATEST_SHA}\n}"
Expand Down

0 comments on commit 1f26e65

Please sign in to comment.