diff options
| author | typebrook <typebrook@gmail.com> | 2020-02-03 18:06:33 +0800 |
|---|---|---|
| committer | typebrook <typebrook@gmail.com> | 2020-02-03 18:06:33 +0800 |
| commit | bc3ffa99d3d4d2074d8ec65dcd4b6e63902c34de (patch) | |
| tree | 1eb461fbe5707086942749844d219eba25c86b9c /scripts/gist | |
| parent | 2c813241b3ff1557099c76db4b7730ac1912c269 (diff) | |
debug
Diffstat (limited to 'scripts/gist')
| -rwxr-xr-x | scripts/gist | 36 |
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 |
| 212 | AccessJsonElement() { | 212 | AccessJsonElement() { |
| 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 ' |
| 221 | raw = json.load(sys.stdin) | ||
| 221 | for gist in raw: | 222 | for 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 ' |
| 341 | raw = json.load(sys.stdin) | ||
| 340 | print("site:", raw["html_url"]) | 342 | print("site:", raw["html_url"]) |
| 341 | print("description:", raw["description"]) | 343 | print("description:", raw["description"]) |
| 342 | print("public:", raw["public"]) | 344 | print("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 ' |
| 357 | raw = json.load(sys.stdin); | ||
| 355 | for comment in raw: | 358 | for 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/,$/}/' \ | 425 | import os.path |
| 418 | | echo "{ \"public\": $public, \"files\": $(cat -), \"description\": \"$description\"}" \ | 426 | files_json = {} |
| 427 | files = sys.stdin.readline().split() | ||
| 428 | description = sys.stdin.readline().replace('\n','') | ||
| 429 | public = sys.stdin.readline().replace('\n','') | ||
| 430 | for file in files: | ||
| 431 | with open(file, 'r') as f: | ||
| 432 | files_json[os.path.basename(file)] = {'content': f.read()} | ||
| 433 | print(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 |