summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authortypebrook <typebrook@gmail.com>2020-02-03 18:06:33 +0800
committertypebrook <typebrook@gmail.com>2020-02-03 18:06:33 +0800
commitbc3ffa99d3d4d2074d8ec65dcd4b6e63902c34de (patch)
tree1eb461fbe5707086942749844d219eba25c86b9c
parent2c813241b3ff1557099c76db4b7730ac1912c269 (diff)
debug
-rwxr-xr-xscripts/gist36
1 files changed, 27 insertions, 9 deletions
diff --git a/scripts/gist b/scripts/gist
index 86676f4..3be610d 100755
--- a/scripts/gist
+++ b/scripts/gist
@@ -211,13 +211,14 @@ _show_list() {
211# parse JSON from STDIN with string of commands 211# parse JSON from STDIN with string of commands
212AccessJsonElement() { 212AccessJsonElement() {
213 PYTHONIOENCODING=utf-8 \ 213 PYTHONIOENCODING=utf-8 \
214 python -c "from __future__ import print_function; import sys, json; raw = json.load(sys.stdin); $1" 2> /dev/null 214 python -c "from __future__ import print_function; import sys, json; $1"
215 return "$?" 215 return "$?"
216} 216}
217 217
218# equal to: jq '.[] | "\(.html_url) \([.files[] | .raw_url]) \(.files | keys | length) \(.comments) \(.description)"' 218# equal to: jq '.[] | "\(.html_url) \([.files[] | .raw_url]) \(.files | keys | length) \(.comments) \(.description)"'
219_handle_gists() { 219_handle_gists() {
220 echo ' 220 echo '
221raw = json.load(sys.stdin)
221for gist in raw: 222for gist in raw:
222 print(gist["html_url"], end=" ") 223 print(gist["html_url"], end=" ")
223 print([file["raw_url"] for file in gist["files"].values()], end=" ") 224 print([file["raw_url"] for file in gist["files"].values()], end=" ")
@@ -337,6 +338,7 @@ _clean_repos() {
337# parse JSON from gist detail 338# parse JSON from gist detail
338_handle_gist() { 339_handle_gist() {
339 echo ' 340 echo '
341raw = json.load(sys.stdin)
340print("site:", raw["html_url"]) 342print("site:", raw["html_url"])
341print("description:", raw["description"]) 343print("description:", raw["description"])
342print("public:", raw["public"]) 344print("public:", raw["public"])
@@ -352,6 +354,7 @@ for file in raw["files"].keys():
352# equal to jq '.[] | {user: .user.login, created_at: .created_at, updated_at: .updated_at, body: .body}' 354# equal to jq '.[] | {user: .user.login, created_at: .created_at, updated_at: .updated_at, body: .body}'
353_handle_comment() { 355_handle_comment() {
354 echo ' 356 echo '
357raw = json.load(sys.stdin);
355for comment in raw: 358for comment in raw:
356 print() 359 print()
357 print("|", "user:", comment["user"]["login"]) 360 print("|", "user:", comment["user"]["login"])
@@ -373,7 +376,7 @@ _show_detail() {
373 376
374# set filename/description/permission for a new gist 377# set filename/description/permission for a new gist
375_set_gist() { 378_set_gist() {
376 public=true 379 public=True
377 while [[ -n "$@" ]]; do case $1 in 380 while [[ -n "$@" ]]; do case $1 in
378 -d | --desc) 381 -d | --desc)
379 description="$2" 382 description="$2"
@@ -392,6 +395,7 @@ _set_gist() {
392 ls $files > /dev/null || return 1 395 ls $files > /dev/null || return 1
393} 396}
394 397
398# TODO remove tmp file
395# Let user type the content of gist before setting filename 399# Let user type the content of gist before setting filename
396_new_file() { 400_new_file() {
397 [[ -t 0 ]] && echo "Type a gist. <Ctrl-C> to cancel, <Ctrl-D> when done" > /dev/tty 401 [[ -t 0 ]] && echo "Type a gist. <Ctrl-C> to cancel, <Ctrl-D> when done" > /dev/tty
@@ -403,24 +407,38 @@ _new_file() {
403 echo /tmp/$filename 407 echo /tmp/$filename
404} 408}
405 409
410_make_gist_body() {
411 return 0
412}
413
406# FIXME curl bad credential exit code 414# FIXME curl bad credential exit code
415# FIXME file content with " and \
407# create a new gist with files 416# create a new gist with files
408_create_gist() { 417_create_gist() {
409 _set_gist "$@" || return 1 418 _set_gist "$@" || return 1
410 [[ -z "$files" ]] && files=$(_new_file $filename) 419 [[ -z "$files" ]] && files=$(_new_file $filename)
411 [[ -z "$description" ]] && read -p 'Type description: ' description < /dev/tty 420 [[ -z "$description" ]] && read -p 'Type description: ' description < /dev/tty
412 421
413 local index=$(( $(sed '/^s/ d' $INDEX | wc -l) +1 ))
414 echo 'Creating a new gist...' 422 echo 'Creating a new gist...'
415 for file in $files; do 423 local index=$(( $(sed '/^s/ d' $INDEX | wc -l) +1 ))
416 echo "\"$(basename $file)\": {\"content\": \"$(sed '$ !s/$/\\n/' $file)\"}," 424 echo -e "$files\n$description\n$public" | AccessJsonElement "
417 done | tr -d '\n' | sed 's/^/{/; s/,$/}/' \ 425import os.path
418 | echo "{ \"public\": $public, \"files\": $(cat -), \"description\": \"$description\"}" \ 426files_json = {}
427files = sys.stdin.readline().split()
428description = sys.stdin.readline().replace('\n','')
429public = sys.stdin.readline().replace('\n','')
430for file in files:
431 with open(file, 'r') as f:
432 files_json[os.path.basename(file)] = {'content': f.read()}
433print(json.dumps({'public': public, 'files': files_json, 'description': description}))
434 " | tee json \
419 | http_method POST $GITHUB_API/gists \ 435 | http_method POST $GITHUB_API/gists \
436 | tee result \
420 | sed '1 s/^/[/; $ s/$/]/' \ 437 | sed '1 s/^/[/; $ s/$/]/' \
421 | _parse_response $index 438 | _parse_response $index \
439 | read result
422 440
423 if [[ -n "$result" ]]; then 441 if true; then
424 echo 'Gist is created' 442 echo 'Gist is created'
425 echo $result >> $INDEX && _show_list | tail -1 443 echo $result >> $INDEX && _show_list | tail -1
426 else 444 else