Skip to content

Commit 9688072

Browse files
authored
Merge pull request #45 from Akianonymus/fix
Add -d | --skip-duplicates flag | Fix #44 | script improvements: Use curly braces, cleanup code
2 parents dc44aba + 150f04f commit 9688072

File tree

4 files changed

+600
-564
lines changed

4 files changed

+600
-564
lines changed

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,11 @@ Other Options available are
161161

162162
-o | --overwrite - Overwrite the files with the same name, if present in the root folder/input folder, also works with recursive folders and single/multiple files.
163163

164+
Note :
165+
If you use this flag along with -d/--skip-duplicates, the skip duplicates flag is preferred.
166+
167+
-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.
168+
164169
-f | --[file/folder] - Specify files and folders explicitly in one command, use multiple times for multiple folder/files.
165170

166171
For uploading multiple input into the same folder:

google-oauth2.sh

Lines changed: 31 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -15,40 +15,40 @@ No valid arguments provided.
1515
Usage:
1616
1717
./%s create - authenticates a user.
18-
./%s refresh - gets a new access token.\n" "$0" "$0"
18+
./%s refresh - gets a new access token.\n" "${0}" "${0}"
1919
exit 0
2020
}
2121

22-
[[ $1 = create ]] || [[ $1 = refresh ]] || shortHelp
22+
[[ ${1} = create ]] || [[ ${1} = refresh ]] || shortHelp
2323

2424
# Move cursor to nth no. of line and clear it to the begining.
2525
clearLine() {
26-
printf "\033[%sA\033[2K" "$1"
26+
printf "\033[%sA\033[2K" "${1}"
2727
}
2828

2929
# Method to extract data from json response.
3030
# Usage: jsonValue key < json ( or use with a pipe output ).
3131
jsonValue() {
3232
[[ $# = 0 ]] && printf "%s: Missing arguments\n" "${FUNCNAME[0]}" && return 1
3333
declare LC_ALL=C num="${2:-1}"
34-
grep -o "\"""$1""\"\:.*" | sed -e "s/.*\"""$1""\": //" -e 's/[",]*$//' -e 's/["]*$//' -e 's/[,]*$//' -e "s/\"//" -n -e "${num}"p
34+
grep -o "\"""${1}""\"\:.*" | sed -e "s/.*\"""${1}""\": //" -e 's/[",]*$//' -e 's/["]*$//' -e 's/[,]*$//' -e "s/\"//" -n -e "${num}"p
3535
}
3636

3737
# Update Config. Incase of old value, update, for new value add.
3838
# Usage: updateConfig valuename value configpath
3939
updateConfig() {
4040
[[ $# -lt 3 ]] && printf "%s: Missing arguments\n" "${FUNCNAME[0]}" && return 1
41-
declare VALUE_NAME="$1" VALUE="$2" CONFIG_PATH="$3" FINAL=()
41+
declare VALUE_NAME="${1}" VALUE="${2}" CONFIG_PATH="${3}" FINAL=()
4242
declare -A Aseen
43-
printf "" >> "$CONFIG_PATH" # If config file doesn't exist.
44-
mapfile -t VALUES < "$CONFIG_PATH" && VALUES+=("$VALUE_NAME=$VALUE")
43+
printf "" >> "${CONFIG_PATH}" # If config file doesn't exist.
44+
mapfile -t VALUES < "${CONFIG_PATH}" && VALUES+=("${VALUE_NAME}=${VALUE}")
4545
for i in "${VALUES[@]}"; do
46-
[[ $i =~ $VALUE_NAME\= ]] && FINAL+=("$VALUE_NAME=\"$VALUE\"") || FINAL+=("$i")
46+
[[ ${i} =~ ${VALUE_NAME}\= ]] && FINAL+=("${VALUE_NAME}=\"${VALUE}\"") || FINAL+=("${i}")
4747
done
4848
for i in "${FINAL[@]}"; do
49-
[[ ${Aseen[$i]} ]] && continue
50-
printf "%s\n" "$i" && Aseen[$i]=x
51-
done >| "$CONFIG_PATH"
49+
[[ ${Aseen[${i}]} ]] && continue
50+
printf "%s\n" "${i}" && Aseen[${i}]=x
51+
done >| "${CONFIG_PATH}"
5252
}
5353

5454
printf "Starting script..\n"
@@ -60,48 +60,48 @@ REDIRECT_URI="urn:ietf:wg:oauth:2.0:oob"
6060
TOKEN_URL="https://accounts.google.com/o/oauth2/token"
6161

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

6565
printf "Checking credentials..\n"
6666

6767
# Credentials
68-
if [[ -z $CLIENT_ID ]]; then
68+
if [[ -z ${CLIENT_ID} ]]; then
6969
read -r -p "Client ID: " CLIENT_ID
70-
updateConfig CLIENT_ID "$CLIENT_ID" "$HOME"/.googledrive.conf
70+
updateConfig CLIENT_ID "${CLIENT_ID}" "${HOME}"/.googledrive.conf
7171
fi
72-
if [[ -z $CLIENT_SECRET ]]; then
72+
if [[ -z ${CLIENT_SECRET} ]]; then
7373
read -r -p "Client Secret: " CLIENT_SECRET
74-
updateConfig CLIENT_SECRET "$CLIENT_SECRET" "$HOME"/.googledrive.conf
74+
updateConfig CLIENT_SECRET "${CLIENT_SECRET}" "${HOME}"/.googledrive.conf
7575
fi
7676

7777
for _ in {1..2}; do clearLine 1; done
7878
printf "Required credentials set.\n"
7979

80-
if [[ $1 = create ]]; then
80+
if [[ ${1} = create ]]; then
8181
printf "Visit the below URL, tap on allow and then enter the code obtained:\n"
82-
URL="https://accounts.google.com/o/oauth2/auth?client_id=$CLIENT_ID&redirect_uri=$REDIRECT_URI&scope=$SCOPE&response_type=code&prompt=consent"
83-
printf "%s\n\n" "$URL"
82+
URL="https://accounts.google.com/o/oauth2/auth?client_id=${CLIENT_ID}&redirect_uri=${REDIRECT_URI}&scope=${SCOPE}&response_type=code&prompt=consent"
83+
printf "%s\n\n" "${URL}"
8484
read -r -p "Enter the authorization code: " CODE
8585

8686
CODE="${CODE//[[:space:]]/}"
87-
if [[ -n $CODE ]]; then
88-
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)"
87+
if [[ -n ${CODE} ]]; then
88+
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})"
8989

90-
ACCESS_TOKEN="$(jsonValue access_token <<< "$RESPONSE")"
91-
REFRESH_TOKEN="$(jsonValue refresh_token <<< "$RESPONSE")"
90+
ACCESS_TOKEN="$(jsonValue access_token <<< "${RESPONSE}")"
91+
REFRESH_TOKEN="$(jsonValue refresh_token <<< "${RESPONSE}")"
9292

93-
printf "Access Token: %s\n" "$ACCESS_TOKEN"
94-
printf "Refresh Token: %s\n" "$REFRESH_TOKEN"
93+
printf "Access Token: %s\n" "${ACCESS_TOKEN}"
94+
printf "Refresh Token: %s\n" "${REFRESH_TOKEN}"
9595
else
9696
printf "\nNo code provided, run the script and try again.\n"
9797
exit 1
9898
fi
99-
elif [[ $1 = refresh ]]; then
100-
if [[ -n $REFRESH_TOKEN ]]; then
101-
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)"
102-
ACCESS_TOKEN="$(echo "$RESPONSE" | jsonValue access_token)"
103-
printf "Access Token: %s\n" "$ACCESS_TOKEN"
99+
elif [[ ${1} = refresh ]]; then
100+
if [[ -n ${REFRESH_TOKEN} ]]; then
101+
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})"
102+
ACCESS_TOKEN="$(echo "${RESPONSE}" | jsonValue access_token)"
103+
printf "Access Token: %s\n" "${ACCESS_TOKEN}"
104104
else
105-
printf "Refresh Token not set, use %s create to generate one.\n" "$0"
105+
printf "Refresh Token not set, use %s create to generate one.\n" "${0}"
106106
fi
107107
fi

0 commit comments

Comments
 (0)