Skip to content

Commit

Permalink
Merge pull request #78 from Akianonymus/fix
Browse files Browse the repository at this point in the history
Resolve #77
  • Loading branch information
labbots committed Jul 1, 2020
2 parents 5a7c618 + ad132fc commit 978b5de
Show file tree
Hide file tree
Showing 5 changed files with 150 additions and 108 deletions.
18 changes: 10 additions & 8 deletions google-oauth2.sh
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
# Set CLIENT_ID and CLIENT_SECRET and SCOPE
# See SCOPES at https://developers.google.com/identity/protocols/oauth2/scopes#docsv1

set -o errexit -o noclobber -o pipefail

_short_help() {
printf "
No valid arguments provided.
Expand All @@ -35,10 +37,6 @@ else
exit 1
fi

if ! _is_terminal; then
DEBUG="true"
export DEBUG
fi
_check_debug

_print_center "justify" "Starting script.." "-"
Expand All @@ -49,8 +47,11 @@ SCOPE="https://www.googleapis.com/auth/drive"
REDIRECT_URI="urn:ietf:wg:oauth:2.0:oob"
TOKEN_URL="https://accounts.google.com/o/oauth2/token"

CONFIG="$(< "${HOME}/.google-drive-upload/google-drive-upload.configpath")" &> /dev/null || :
CONFIG="${CONFIG:-${HOME}/.googledrive.conf}"

# shellcheck source=/dev/null
[[ -f ${HOME}/.googledrive.conf ]] && source "${HOME}"/.googledrive.conf
[[ -f ${CONFIG} ]] && source "${CONFIG}"

_print_center "justify" "Checking credentials.." "-"

Expand All @@ -77,7 +78,7 @@ if [[ ${1} = create ]]; then

CODE="${CODE//[[:space:]]/}"
if [[ -n ${CODE} ]]; then
RESPONSE="$(curl --compressed -s -X POST --data "client_id=${CLIENT_ID}&client_secret=${CLIENT_SECRET}&refresh_token=${REFRESH_TOKEN}&grant_type=refresh_token" ${TOKEN_URL})"
RESPONSE="$(curl --compressed -s -X POST --data "client_id=${CLIENT_ID}&client_secret=${CLIENT_SECRET}&refresh_token=${REFRESH_TOKEN}&grant_type=refresh_token" ${TOKEN_URL})" || :

ACCESS_TOKEN="$(_json_value access_token 1 1 <<< "${RESPONSE}")"
REFRESH_TOKEN="$(_json_value refresh_token 1 1 <<< "${RESPONSE}")"
Expand All @@ -102,9 +103,9 @@ elif [[ ${1} = refresh ]]; then
# Make a request on https://www.googleapis.com/oauth2/""${API_VERSION}""/tokeninfo?access_token=${ACCESS_TOKEN} url and check if the given token is valid, if not generate one.
# Requirements: Refresh Token
_get_token_and_update() {
RESPONSE="$(curl --compressed -s -X POST --data "client_id=${CLIENT_ID}&client_secret=${CLIENT_SECRET}&refresh_token=${REFRESH_TOKEN}&grant_type=refresh_token" "${TOKEN_URL}")"
RESPONSE="$(curl --compressed -s -X POST --data "client_id=${CLIENT_ID}&client_secret=${CLIENT_SECRET}&refresh_token=${REFRESH_TOKEN}&grant_type=refresh_token" "${TOKEN_URL}")" || :
ACCESS_TOKEN="$(_json_value access_token 1 1 <<< "${RESPONSE}")"
ACCESS_TOKEN_EXPIRY="$(curl --compressed -s "${API_URL}/oauth2/${API_VERSION}/tokeninfo?access_token=${ACCESS_TOKEN}" | _json_value exp 1 1)"
ACCESS_TOKEN_EXPIRY="$(curl --compressed -s "${API_URL}/oauth2/${API_VERSION}/tokeninfo?access_token=${ACCESS_TOKEN}" | _json_value exp 1 1)" || :
"${UPDATE:-:}" ACCESS_TOKEN "${ACCESS_TOKEN}" "${CONFIG}"
"${UPDATE:-:}" ACCESS_TOKEN_EXPIRY "${ACCESS_TOKEN_EXPIRY}" "${CONFIG}"
}
Expand All @@ -115,5 +116,6 @@ elif [[ ${1} = refresh ]]; then
printf "Access Token: %s\n" "${ACCESS_TOKEN}"
else
_print_center "normal" "Refresh Token not set, use ${0} create to generate one." "="
exit 1
fi
fi
13 changes: 8 additions & 5 deletions install.sh
Original file line number Diff line number Diff line change
Expand Up @@ -238,14 +238,16 @@ _get_latest_sha() {
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)" && {
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}"
})"
)"
;;
release)
LATEST_SHA="$(hash="$(curl -L --compressed -s https://github.com/"${3:-${REPO}}"/releases/"${2:-${TYPE_VALUE}}" | grep "=\"/""${3:-${REPO}}""/commit" -m1)" && {
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" "${_/\"*/}"
})"
)"
;;
esac
printf "%b" "${LATEST_SHA:+${LATEST_SHA}\n}"
Expand Down Expand Up @@ -308,7 +310,7 @@ _json_value() {
declare LC_ALL=C num
{ [[ ${2} =~ ^([0-9]+)+$ ]] && no_of_lines="${2}"; } || :
{ [[ ${3} =~ ^([0-9]+)+$ ]] && num="${3}"; } || { [[ ${3} != all ]] && num=1; }
grep -o "\"${1}\"\:.*" ${no_of_lines+-m ${no_of_lines}} | sed -e "s/.*\"""${1}""\"://" -e 's/[",]*$//' -e 's/["]*$//' -e 's/[,]*$//' -e "s/^ //" -e 's/^"//' -n -e "${num}"p
grep -o "\"${1}\"\:.*" ${no_of_lines:+-m ${no_of_lines}} | sed -e "s/.*\"""${1}""\"://" -e 's/[",]*$//' -e 's/["]*$//' -e 's/[,]*$//' -e "s/^ //" -e 's/^"//' -n -e "${num}"p || :
}

###################################################
Expand Down Expand Up @@ -764,6 +766,7 @@ _setup_arguments() {

main() {
_check_bash_version && _check_dependencies
set -o errexit -o noclobber -o pipefail

_variables
if [[ $* ]]; then
Expand Down
75 changes: 42 additions & 33 deletions sync.sh
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,12 @@ Options:\n
-s | --service 'service name' - To generate systemd service file to setup background jobs on boot.\n
-D | --debug - Display script command trace, use before all the flags to see maximum script trace.\n
-h | --help - Display usage instructions.\n" "${0##*/}" "${0##*/}" "${0##*/}" "${0##*/}"
exit
exit 0
}

_short_help() {
printf "No valid arguments provided, use -h/--help flag to see usage.\n"
exit
exit 0
}

###################################################
Expand Down Expand Up @@ -62,28 +62,28 @@ _get_job_info() {
pid="${1}"
input="${3:-$(grep "${pid}" "${SYNC_LIST}" || :)}"
if [[ -n ${input} ]]; then
if _check_pid "${pid}"; then
if times="$(ps -p "${pid}" -o etimes --no-headers)"; then
printf "\n%s\n" "PID: ${pid}"
: "${input#*"|:_//_:|"}" && local_folder="${_/"|:_//_:|"*/}"
printf "Local Folder: %s\n" "${local_folder}"
printf "Drive Folder: %s\n" "${input/*"|:_//_:|"/}"
times="$(ps -p "${pid}" -o etimes --no-headers)"
printf "Running Since: %s\n" "$(_display_time "${times}")"
if [[ -n ${2} ]]; then
extra="$(ps -p "${pid}" -o %cpu,%mem --no-headers)"
extra="$(ps -p "${pid}" -o %cpu,%mem --no-headers || :)"
printf "CPU usage:%s\n" "${extra% *}"
printf "Memory usage: %s\n" "${extra##* }"
_setup_loop_variables "${local_folder}" "${input/*"|:_//_:|"/}"
printf "Success: %s\n" "$(_count < "${SUCCESS_LOG}")"
printf "Failed: %s\n" "$(_count < "${ERROR_LOG}")"
fi
return 0
return_status=0
else
return 1
return_status=1
fi
else
return 11
return_status=11
fi
return 0
}

###################################################
Expand All @@ -97,16 +97,17 @@ _get_job_info() {
###################################################
_remove_job() {
declare pid="${1}" input local_folder drive_folder
input="$(grep "${pid}" "${SYNC_LIST}")"
input="$(grep "${pid}" "${SYNC_LIST}" || :)"
: "${input#*"|:_//_:|"}" && local_folder="${_/"|:_//_:|"*/}"
drive_folder="${input/*"|:_//_:|"/}"
new_list="$(grep -v "${pid}" "${SYNC_LIST}")"
new_list="$(grep -v "${pid}" "${SYNC_LIST}" || :)"
printf "%s\n" "${new_list}" >| "${SYNC_LIST}"
rm -rf "${SYNC_DETAIL_DIR:?}/${drive_folder}${local_folder}"
# Cleanup dir if empty
if find "${SYNC_DETAIL_DIR:?}/${drive_folder}" -type f &> /dev/null; then
rm -rf "${SYNC_DETAIL_DIR:?}/${drive_folder}"
fi
return 0
}

###################################################
Expand All @@ -119,7 +120,7 @@ _remove_job() {
###################################################
_kill_job() {
declare pid="${1}"
kill -9 "${pid}" &> /dev/null
kill -9 "${pid}" &> /dev/null || :
_remove_job "${pid}"
printf "Killed.\n"
}
Expand All @@ -141,11 +142,12 @@ _show_jobs() {
if [[ -n ${line} ]]; then
: "${line/"|:_//_:|"*/}" && pid="${_/*: /}"
_get_job_info "${pid}" "${1}" "${line}"
{ [[ ${?} = 1 ]] && _remove_job "${pid}"; } || { ((total += 1)) && no_task="printf"; }
{ [[ ${return_status} = 1 ]] && _remove_job "${pid}"; } || { ((total += 1)) && no_task="printf"; }
fi
done 4< "${SYNC_LIST}"
printf "\nTotal Jobs Running: %s\n" "${total}"
[[ v${1} = v ]] && "${no_task:-:}" "For more info: %s -j/--jobs v/verbose\n" "${0##*/}"
[[ -n ${1} ]] && "${no_task:-:}" "For more info: %s -j/--jobs v/verbose\n" "${0##*/}"
return 0
}

###################################################
Expand Down Expand Up @@ -201,6 +203,7 @@ _check_and_upload() {
else
all+=(*)
fi

mapfile -t final <<< "$(_remove_array_duplicates "${all[@]}")"

mapfile -t new_files <<< "$(diff \
Expand Down Expand Up @@ -252,15 +255,16 @@ _check_existing_loop() {
_setup_loop_variables "${FOLDER}" "${GDRIVE_FOLDER}"
_setup_loop_files
if [[ -z ${PID} ]]; then
return 0
return_status=0
elif _check_pid "${PID}"; then
return 1
return_status=1
else
_remove_job "${PID}"
_setup_loop_variables "${FOLDER}" "${GDRIVE_FOLDER}"
_setup_loop_files
return 2
return_status=2
fi
return 0
}

###################################################
Expand All @@ -284,8 +288,9 @@ _start_new_loop() {
printf "%b\n" "Job started.\nLocal Folder: ${INPUT}\nDrive Folder: ${GDRIVE_FOLDER}"
printf "%s\n" "PID: ${PID}"
printf "%b\n" "PID: ${PID}|:_//_:|${FOLDER}|:_//_:|${GDRIVE_FOLDER}" >> "${SYNC_LIST}"
{ [[ -n ${SHOW_LOGS} ]] && tail -f "${LOGS}"; } || :
[[ -n ${SHOW_LOGS} ]] && tail -f "${LOGS}"
fi
return 0
}

###################################################
Expand Down Expand Up @@ -321,9 +326,8 @@ _do_job() {
for pid in "${ALL_PIDS[@]}"; do
if [[ ${JOB_TYPE} =~ INFO ]]; then
_get_job_info "${pid}" more
status="${?}"
if [[ ${status} != 0 ]]; then
{ [[ ${status} = 1 ]] && _remove_job "${pid}"; } || :
if [[ ${return_status} != 0 ]]; then
[[ ${return_status} = 1 ]] && _remove_job "${pid}"
printf "No job running with given PID ( %s ).\n" "${pid}" 1>&2
fi
fi
Expand All @@ -341,11 +345,10 @@ _do_job() {
fi
if [[ ${JOB_TYPE} =~ KILL ]]; then
_get_job_info "${pid}"
status="${?}"
if [[ ${status} = 0 ]]; then
if [[ ${return_status} = 0 ]]; then
_kill_job "${pid}"
else
{ [[ ${status} = 1 ]] && _remove_job "${pid}"; } || :
[[ ${return_status} = 1 ]] && _remove_job "${pid}"
printf "No job running with given PID ( %s ).\n" "${pid}" 1>&2
fi
fi
Expand All @@ -355,6 +358,7 @@ _do_job() {
fi
;;
esac
return 0
}

###################################################
Expand All @@ -370,12 +374,13 @@ _do_job() {
###################################################
_setup_arguments() {
[[ $# = 0 ]] && printf "%s: Missing arguments\n" "${FUNCNAME[0]}" && return 1
declare -g SYNC_TIME_TO_SLEEP ARGS COMMAND_NAME DEBUG GDRIVE_FOLDER KILL SHOW_LOGS
unset SYNC_TIME_TO_SLEEP ARGS COMMAND_NAME DEBUG GDRIVE_FOLDER KILL SHOW_LOGS

_check_longoptions() {
{ [[ -z ${2} ]] &&
[[ -z ${2} ]] &&
printf '%s: %s: option requires an argument\nTry '"%s -h/--help"' for more information.\n' \
"${0##*/}" "${1}" "${0##*/}" && exit 1; } || :
"${0##*/}" "${1}" "${0##*/}" && exit 1
return 0
}

while [[ $# -gt 0 ]]; do
Expand All @@ -393,7 +398,7 @@ _setup_arguments() {
ARGS+=" -C ${GDRIVE_FOLDER} "
;;
-j | --jobs)
{ [[ ${2} = v* ]] && SHOW_JOBS_VERBOSE="true" && shift; } || :
[[ ${2} = v* ]] && SHOW_JOBS_VERBOSE="true" && shift
JOB=(SHOW_JOBS)
;;
-p | --pid)
Expand Down Expand Up @@ -424,7 +429,7 @@ _setup_arguments() {
-t | --time)
_check_longoptions "${1}" "${2}"
if [[ ${2} =~ ^([0-9]+)+$ ]]; then
{ [[ ${2} = default* ]] && UPDATE_DEFAULT_TIME_TO_SLEEP="_update_config"; } || :
[[ ${2} = default* ]] && UPDATE_DEFAULT_TIME_TO_SLEEP="_update_config"
TO_SLEEP="${2/default=/}" && shift
else
printf "-t/--time only takes positive integers as arguments, min = 1, max = infinity.\n"
Expand All @@ -433,7 +438,7 @@ _setup_arguments() {
;;
-a | --arguments)
_check_longoptions "${1}" "${2}"
{ [[ ${2} = default* ]] && UPDATE_DEFAULT_ARGS="_update_config"; } || :
[[ ${2} = default* ]] && UPDATE_DEFAULT_ARGS="_update_config"
ARGS+="${2/default=/} " && shift
;;
-fg | --foreground)
Expand Down Expand Up @@ -489,6 +494,7 @@ _setup_arguments() {
fi

mapfile -t FINAL_INPUT_ARRAY <<< "$(_remove_array_duplicates "${FINAL_INPUT_ARRAY[@]}")"
return 0
}

###################################################
Expand All @@ -502,7 +508,7 @@ _setup_arguments() {
# source CONFIG, update default values if required
###################################################
_config_variables() {
if [[ -f "${INFO_PATH}/google-drive-upload.info" ]]; then
if [[ -r "${INFO_PATH}/google-drive-upload.info" ]]; then
# shellcheck source=/dev/null
source "${INFO_PATH}/google-drive-upload.info"
else
Expand Down Expand Up @@ -532,6 +538,7 @@ _config_variables() {
ARGS+=" ${SYNC_DEFAULT_ARGS:-} "
"${UPDATE_DEFAULT_ARGS:-:}" SYNC_DEFAULT_ARGS " ${ARGS} " "${CONFIG}"
"${UPDATE_DEFAULT_TIME_TO_SLEEP:-:}" SYNC_TIME_TO_SLEEP "${SYNC_TIME_TO_SLEEP}" "${CONFIG}"
return 0
}

###################################################
Expand Down Expand Up @@ -613,8 +620,7 @@ done'
fi
cd "${FOLDER}" || exit 1
_check_existing_loop
status="$?"
case "${status}" in
case "${return_status}" in
0 | 2)
_start_new_loop
;;
Expand All @@ -630,16 +636,19 @@ done'
_kill_job "${PID}"
exit
fi
{ [[ -n ${SHOW_LOGS} ]] && tail -f "${LOGS}"; } || :
[[ -n ${SHOW_LOGS} ]] && tail -f "${LOGS}"
;;
esac
cd "${CURRENT_FOLDER}" || exit 1
done
return 0
}

main() {
[[ $# = 0 ]] && _short_help

set -o errexit -o noclobber -o pipefail

UTILS_FILE="${UTILS_FILE:-./utils.sh}"
if [[ -r ${UTILS_FILE} ]]; then
# shellcheck source=/dev/null
Expand Down

0 comments on commit 978b5de

Please sign in to comment.