From bc3ffa99d3d4d2074d8ec65dcd4b6e63902c34de Mon Sep 17 00:00:00 2001 From: typebrook Date: Mon, 3 Feb 2020 18:06:33 +0800 Subject: debug --- scripts/gist | 36 +++++++++++++++++++++++++++--------- 1 file 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() { # parse JSON from STDIN with string of commands AccessJsonElement() { PYTHONIOENCODING=utf-8 \ - python -c "from __future__ import print_function; import sys, json; raw = json.load(sys.stdin); $1" 2> /dev/null + python -c "from __future__ import print_function; import sys, json; $1" return "$?" } # equal to: jq '.[] | "\(.html_url) \([.files[] | .raw_url]) \(.files | keys | length) \(.comments) \(.description)"' _handle_gists() { echo ' +raw = json.load(sys.stdin) for gist in raw: print(gist["html_url"], end=" ") print([file["raw_url"] for file in gist["files"].values()], end=" ") @@ -337,6 +338,7 @@ _clean_repos() { # parse JSON from gist detail _handle_gist() { echo ' +raw = json.load(sys.stdin) print("site:", raw["html_url"]) print("description:", raw["description"]) print("public:", raw["public"]) @@ -352,6 +354,7 @@ for file in raw["files"].keys(): # equal to jq '.[] | {user: .user.login, created_at: .created_at, updated_at: .updated_at, body: .body}' _handle_comment() { echo ' +raw = json.load(sys.stdin); for comment in raw: print() print("|", "user:", comment["user"]["login"]) @@ -373,7 +376,7 @@ _show_detail() { # set filename/description/permission for a new gist _set_gist() { - public=true + public=True while [[ -n "$@" ]]; do case $1 in -d | --desc) description="$2" @@ -392,6 +395,7 @@ _set_gist() { ls $files > /dev/null || return 1 } +# TODO remove tmp file # Let user type the content of gist before setting filename _new_file() { [[ -t 0 ]] && echo "Type a gist. to cancel, when done" > /dev/tty @@ -403,24 +407,38 @@ _new_file() { echo /tmp/$filename } +_make_gist_body() { + return 0 +} + # FIXME curl bad credential exit code +# FIXME file content with " and \ # create a new gist with files _create_gist() { _set_gist "$@" || return 1 [[ -z "$files" ]] && files=$(_new_file $filename) [[ -z "$description" ]] && read -p 'Type description: ' description < /dev/tty - local index=$(( $(sed '/^s/ d' $INDEX | wc -l) +1 )) echo 'Creating a new gist...' - for file in $files; do - echo "\"$(basename $file)\": {\"content\": \"$(sed '$ !s/$/\\n/' $file)\"}," - done | tr -d '\n' | sed 's/^/{/; s/,$/}/' \ - | echo "{ \"public\": $public, \"files\": $(cat -), \"description\": \"$description\"}" \ + local index=$(( $(sed '/^s/ d' $INDEX | wc -l) +1 )) + echo -e "$files\n$description\n$public" | AccessJsonElement " +import os.path +files_json = {} +files = sys.stdin.readline().split() +description = sys.stdin.readline().replace('\n','') +public = sys.stdin.readline().replace('\n','') +for file in files: + with open(file, 'r') as f: + files_json[os.path.basename(file)] = {'content': f.read()} +print(json.dumps({'public': public, 'files': files_json, 'description': description})) + " | tee json \ | http_method POST $GITHUB_API/gists \ + | tee result \ | sed '1 s/^/[/; $ s/$/]/' \ - | _parse_response $index + | _parse_response $index \ + | read result - if [[ -n "$result" ]]; then + if true; then echo 'Gist is created' echo $result >> $INDEX && _show_list | tail -1 else -- cgit v1.2.3-70-g09d2