-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgtaskscli
executable file
·327 lines (284 loc) · 10.4 KB
/
gtaskscli
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
#!/bin/bash
# Communicates with other scripts, bash and python alike, to manipulate the user's task list.
working_dir="."
usr_dir="$HOME/.gtaskscli"
[ -x /usr/bin/python3 ] || exit 1
[ -x /usr/bin/curl ] || exit 1
[ -x /usr/bin/getopt ] || exit 1
[ -x /bin/date ] || exit 1
[ -x /bin/sed ] || exit 1
[ -x $working_dir/googleToken ] || exit 1
[ -a $working_dir/utils/auth_wserver.py ] || exit 1
[ -a $working_dir/utils/getFreePort.py ] || exit 1
[ -a $working_dir/utils/rfc7636.py ] || exit 1
[ -a $working_dir/access_refresh_token.py ] || exit 1
[ -a $working_dir/tasklist/list.py ] || exit 1
[ -a $working_dir/task/tasks.py ] || exit 1
[ -a $working_dir/task/task.py ] || exit 1
# Check if the .gtaskscli file exist. If not, this is the first run.
if ! [ -a $usr_dir ]; then
echo "It seems that you are new here. Let's set things up."
echo "Please read README.md to find out how to obtain the following necessary parameters."
echo -n "Google Client ID: "
read google_client_id
echo -n "Google Client Secret: "
read google_client_secret
# Write inputs into the file
echo "google_client_id=$google_client_id" >> $usr_dir
echo "google_client_secret=$google_client_secret" >> $usr_dir
# Call the GoogleToken script
export google_client_id
export google_client_secret
export google_scope="https://www.googleapis.com/auth/tasks"
$working_dir/googleToken
if [ $? -eq 1 ]; then
echo "Missing dependencies. Please contact the developer."
exit $?
fi
if [ $? -eq 2 ]; then
echo "Code verification and code challenge generation failed. Please contact the developer."
exit $?
fi
if [ $? -eq 3 ]; then
echo "Free port unavailable. Please wait for a moment, or contact the developer if the problem persists."
exit $?
fi
fi
# Read from .gtaskscli to setup variables required for operations
while IFS='' read -r line || [[ -n "$line" ]]; do
PARAM=$( echo -n $line | sed "s/=.*//" )
case $PARAM in
"google_client_id")
export google_client_id=${line#"$PARAM="}
;;
"google_client_secret")
export google_client_secret=${line#"$PARAM="}
;;
"access_token")
export access_token=${line#"$PARAM="}
;;
"refresh_token")
export refresh_token=${line#"$PARAM="}
;;
"expires_at")
export expires_at=${line#"$PARAM="}
esac
done < "$usr_dir"
# Figure out if token has expired
if [ "$expires_at" -lt "$(/bin/date -d "+1 minute" +%s)" ]; then
echo "Access token expired. Renewing..."
renewJson=$( curl -s -d "client_id=$google_client_id&client_secret=$google_client_secret&refresh_token=$refresh_token&grant_type=refresh_token" https://www.googleapis.com/oauth2/v4/token )
export access_token=$(echo -n $renewJson | python3 $working_dir/access_refresh_token.py a)
export expires_at=$(echo -n $renewJson | python3 $working_dir/access_refresh_token.py e)
# Save new data into the usr_dir file
rm -f $usr_dir
if [ $? -eq 2 ]; then
echo "Refresh token is no longer valid. Re-setup required."
echo "Please rerun this command."
exit $?
fi
echo "google_client_id=$google_client_id" >> $usr_dir
echo "google_client_secret=$google_client_secret" >> $usr_dir
echo "access_token=$access_token" >> $usr_dir
echo "refresh_token=$refresh_token" >> $usr_dir
echo "expires_at=$( /bin/date -d "+$expires_at seconds" +%s )" >> $usr_dir
fi
# Parse command line arguments
OPTIND=1 # reset just in case getopts has been used previously in the shell
! PARSED=$( /usr/bin/getopt --options=hls:d:yn:r:u:c --longoptions=help,list,select:,delete:,yes,new:,due:,notes:,rename:,update:complete --name "$0" -- "$@" )
if [[ ${PIPESTATUS[0]} -ne 0 ]]; then
echo "Error occured while parsing command line arguments."
exit 5
fi
## Set the positional arguments again, I want the '--' to be there
eval set -- "$PARSED"
list=n select=n delete=n comp=n yes=0 new="" due="" notes="" name="" update=""
while true; do
case "$1" in
-h|--help)
cat cmdHelp.txt
shift
;;
-l|--list)
list=y
shift
;;
-s|--select)
select=$2
shift 2
;;
-d|--delete)
delete=$2
shift 2
;;
-y|--yes)
yes="Y"
shift
;;
-n|--new)
new=$2
shift 2
;;
-r|--rename)
name=$2
shift 2
;;
-u|--update)
update=$2
shift 2
;;
-c|--complete)
comp="Y"
shift
;;
--due)
due=$2
shift 2
;;
--notes)
notes=$2
shift 2
;;
--)
shift
break
;;
esac
done
# Process commands
if [ "$list" == "y" ]; then
if [ "$1" != "" ]; then
item_id=$( curl -s -H "Authorization: Bearer $access_token" https://www.googleapis.com/tasks/v1/users/@me/lists | python3 $working_dir/tasklist/list.py $@ )
if [[ ${PIPESTATUS[0]} -eq 3 ]]; then
echo "Could not find the list."
exit 6
fi
curl -s -H "Authorization: Bearer $access_token" https://www.googleapis.com/tasks/v1/lists/$item_id/tasks | python3 $working_dir/task/tasks.py
else
curl -s -H "Authorization: Bearer $access_token" https://www.googleapis.com/tasks/v1/users/@me/lists | python3 $working_dir/tasklist/list.py
fi
exit 0 # all other operations cannot take place when listing
fi
if [ "$select" != "n" ]; then
if [ "$1" != "" ]; then
item_id=$( curl -s -H "Authorization: Bearer $access_token" https://www.googleapis.com/tasks/v1/users/@me/lists | python3 $working_dir/tasklist/list.py $@ )
if [[ ${PIPESTATUS[0]} -eq 3 ]]; then
echo "Could not find the list."
exit 6
fi
self_url=$( curl -s -H "Authorization: Bearer $access_token" https://www.googleapis.com/tasks/v1/lists/$item_id/tasks | python3 $working_dir/task/tasks.py $select )
curl -s -H "Authorization: Bearer $access_token" $self_url | python3 $working_dir/task/task.py
else
echo "Please specify a list!"
exit 0
fi
fi
if [ "$delete" != "n" ]; then
if [ "$1" != "" ]; then # delete item
item_id=$( curl -s -H "Authorization: Bearer $access_token" https://www.googleapis.com/tasks/v1/users/@me/lists | python3 $working_dir/tasklist/list.py $@ )
if [[ ${PIPESTATUS[0]} -eq 3 ]]; then
echo "Could not find the list."
exit 6
fi
# Display the task to delete
self_url=$( curl -s -H "Authorization: Bearer $access_token" https://www.googleapis.com/tasks/v1/lists/$item_id/tasks | python3 $working_dir/task/tasks.py $delete )
curl -s -H "Authorization: Bearer $access_token" $self_url | python3 $working_dir/task/task.py
# Ask the user if they are sure
choice=$yes
while [ "$choice" != "Y" ] && [ $choice != "n" ]; do
echo -n "Are you sure you want to delete the above task? [Y/n]: "
read choice
done
# Deletes the task
if [ "$choice" == "Y" ]; then
task_id=$( curl -s -H "Authorization: Bearer $access_token" https://www.googleapis.com/tasks/v1/lists/$item_id/tasks | python3 $working_dir/task/tasks.py -$delete )
curl -s -X DELETE -H "Authorization: Bearer $access_token" $self_url
fi
else # delete list
item_id=$( curl -s -H "Authorization: Bearer $access_token" https://www.googleapis.com/tasks/v1/users/@me/lists | python3 $working_dir/tasklist/list.py $delete )
if [[ ${PIPESTATUS[0]} -eq 3 ]]; then
echo "Could not find the list."
exit 6
fi
# Prompt the user if they are sure
choice=$yes
while [ "$choice" != "Y" ] && [ $choice != "n" ]; do
echo -n "Are you sure you want to delete $delete? [Y/n]: "
read choice
done
# Deletes the task list
if [ "$choice" == "Y" ]; then
curl -s -X DELETE -H "Authorization: Bearer $access_token" https://www.googleapis.com/tasks/v1/users/@me/lists/$item_id
fi
fi
exit 0
fi
if [ "$new" != "" ]; then
if [ "$1" != "" ]; then # user wants a new task
json="{\"kind\": \"tasks#task\", \"title\": \"$new\""
if [ "$due" != "" ]; then # add due date if applicable
json="$json,\"due\": \"$( date -u +%Y-%m-%dT00:00:00.000Z -d "$due" )\""
fi
if [ "$notes" != "" ]; then
json="$json,\"notes\": \"$notes\""
fi
json="$json}"
# Figure out ID of task list
item_id=$( curl -s -H "Authorization: Bearer $access_token" https://www.googleapis.com/tasks/v1/users/@me/lists | python3 $working_dir/tasklist/list.py $@ )
if [[ ${PIPESTATUS[0]} -eq 3 ]]; then
echo "Could not find the list."
exit 6
fi
# Create a new task on the task list
curl -s -X POST -H "Authorization: Bearer $access_token" -H "Content-Type: application/json" https://www.googleapis.com/tasks/v1/lists/$item_id/tasks -d "$json" >> /dev/null
else # user wants a new task list
json="{\"kind\": \"tasks#taskList\", \"title\": \"$new\"}"
curl -s -X POST -H "Authorization: Bearer $access_token" -H "Content-Type: application/json" https://www.googleapis.com/tasks/v1/users/@me/lists -d "$json" >> /dev/null
fi
exit 0
fi
if [ "$update" != "" ] && [[ "$name" != "" || "$due" != "" || "$notes" != "" || "$comp" == "Y" ]]; then
if [ "$1" != "" ]; then # user wants to modify task
json="{"
separator="" # I'm sorry I'm lazy to write if else everywhere to check if the json has started or not okay????
if [ "$name" != "" ]; then
json="$json$separator \"title\": \"$name\""
separator=","
fi
if [ "$due" != "" ]; then
json="$json$separator \"due\": \"$( date -u +%Y-%m-%dT00:00:00.000Z -d "$due" )\""
separator=","
fi
if [ "$notes" != "" ]; then
json="$json$separator \"notes\": \"$notes\""
separator=","
fi
if [ "$comp" == "Y" ]; then
json="$json$separator \"status\": \"completed\""
separator=","
fi
json="$json }"
# Obtain the item id
item_id=$( curl -s -H "Authorization: Bearer $access_token" https://www.googleapis.com/tasks/v1/users/@me/lists | python3 $working_dir/tasklist/list.py $@ )
if [[ ${PIPESTATUS[0]} -eq 3 ]]; then
echo "Could not find the list."
exit 6
fi
self_url=$( curl -s -H "Authorization: Bearer $access_token" https://www.googleapis.com/tasks/v1/lists/$item_id/tasks | python3 $working_dir/task/tasks.py $update )
curl -s -X PATCH -H "Authorization: Bearer $access_token" -H "Content-Type: application/json" $self_url -d "$json" >> /dev/null
curl -s -X POST -H "Authorization: Bearer $access_token" "https://www.googleapis.com/tasks/v1/lists/$item_id/clear" >> /dev/null
else # user wants to modify tasklist
if [ "$name" != "" ]; then
json="{ \"title\": \"$name\" }"
# Obtain the item id
item_id=$( curl -s -H "Authorization: Bearer $access_token" https://www.googleapis.com/tasks/v1/users/@me/lists | python3 $working_dir/tasklist/list.py $update )
if [[ ${PIPESTATUS[0]} -eq 3 ]]; then
echo "Could not find the list."
exit 6
fi
# Rename the list
curl -s -X PATCH -H "Authorization: Bearer $access_token" -H "Content-Type: application/json" https://www.googleapis.com/tasks/v1/users/@me/lists/$item_id -d "$json" >> /dev/null
fi
fi
exit 0
fi