@@ -15,40 +15,40 @@ No valid arguments provided.
15
15
Usage:
16
16
17
17
./%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} "
19
19
exit 0
20
20
}
21
21
22
- [[ $1 = create ]] || [[ $1 = refresh ]] || shortHelp
22
+ [[ ${1} = create ]] || [[ ${1} = refresh ]] || shortHelp
23
23
24
24
# Move cursor to nth no. of line and clear it to the begining.
25
25
clearLine () {
26
- printf " \033[%sA\033[2K" " $1 "
26
+ printf " \033[%sA\033[2K" " ${1} "
27
27
}
28
28
29
29
# Method to extract data from json response.
30
30
# Usage: jsonValue key < json ( or use with a pipe output ).
31
31
jsonValue () {
32
32
[[ $# = 0 ]] && printf " %s: Missing arguments\n" " ${FUNCNAME[0]} " && return 1
33
33
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
35
35
}
36
36
37
37
# Update Config. Incase of old value, update, for new value add.
38
38
# Usage: updateConfig valuename value configpath
39
39
updateConfig () {
40
40
[[ $# -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=()
42
42
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} " )
45
45
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} " )
47
47
done
48
48
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} "
52
52
}
53
53
54
54
printf " Starting script..\n"
@@ -60,48 +60,48 @@ REDIRECT_URI="urn:ietf:wg:oauth:2.0:oob"
60
60
TOKEN_URL=" https://accounts.google.com/o/oauth2/token"
61
61
62
62
# shellcheck source=/dev/null
63
- [[ -f $HOME /.googledrive.conf ]] && source " $HOME " /.googledrive.conf
63
+ [[ -f ${ HOME} /.googledrive.conf ]] && source " ${ HOME} " /.googledrive.conf
64
64
65
65
printf " Checking credentials..\n"
66
66
67
67
# Credentials
68
- if [[ -z $CLIENT_ID ]]; then
68
+ if [[ -z ${ CLIENT_ID} ]]; then
69
69
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
71
71
fi
72
- if [[ -z $CLIENT_SECRET ]]; then
72
+ if [[ -z ${ CLIENT_SECRET} ]]; then
73
73
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
75
75
fi
76
76
77
77
for _ in {1..2}; do clearLine 1; done
78
78
printf " Required credentials set.\n"
79
79
80
- if [[ $1 = create ]]; then
80
+ if [[ ${1} = create ]]; then
81
81
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} "
84
84
read -r -p " Enter the authorization code: " CODE
85
85
86
86
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} ) "
89
89
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} " ) "
92
92
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} "
95
95
else
96
96
printf " \nNo code provided, run the script and try again.\n"
97
97
exit 1
98
98
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} "
104
104
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} "
106
106
fi
107
107
fi
0 commit comments