From a2f1012084591cdf1872f02828a922b239d4dd97 Mon Sep 17 00:00:00 2001 From: typebrook Date: Wed, 29 Jan 2020 11:33:33 +0800 Subject: update --- gist | 76 ++++++++++++++++++++++++++++++++++++++++++++++++++++---------------- 1 file changed, 58 insertions(+), 18 deletions(-) diff --git a/gist b/gist index b82fa0a..40d723a 100755 --- a/gist +++ b/gist @@ -6,7 +6,7 @@ # # # This script host your gists as local cloned git repo -# It works under GNU with jq and curl, both are easy to get in most cases +# It works under GNU curl, which is easy to get in most cases # # Use the following commands to manage your gists: # @@ -46,13 +46,12 @@ # It is your business to do git commit and git push # # * configuration -# gist (config | c) [token ] [user ] [folder ] +# gist (config | c) [token ] [user ] [folder ] [auto-sync false] # # * show this help message # gist (help | h) # TODO error handling, unit test -# TODO parallel branch works with json parsing on python # TODO parallel branch works with wget and other stuff # TODO completion # TODO grep mode for description, file content @@ -62,6 +61,12 @@ config=~/.config/gistrc set -eo pipefail [ "$TRACE" ] && set -x +## 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" + return "$?" +} + _auth() { echo 'Hi fellow! To access your gists, I need your Github username and a personal token with scope which allows "gist"!' read -p "Github username: " user < /dev/tty @@ -146,12 +151,24 @@ _update() { if [[ $auto_sync != "false" ]]; then (_sync_repos $1 > /dev/null 2>&1 &); fi } +# equal to: jq '.[] | "\(.html_url) \([.files[] | .raw_url]) \(.files | keys | length) \(.comments) \(.description)"' +_handle_gists() { + echo ' +for gist in raw: + print(gist["html_url"], end=" ") + print([file["raw_url"] for file in gist["files"].values()], end=" ") + print(len(gist["files"]), end=" ") + print(gist["comments"], end=" ") + print(gist["description"]) + ' +} + # TODO check if a user create a very first gist _parse_response() { - jq '.[] | "\(.html_url) \([.files[] | .raw_url]) \(.files | keys | length) \(.comments) \(.description)"' \ - | tac \ + AccessJsonElement "$(_handle_gists)" \ + | tac | sed 's/, /,/g'\ | while read link file_url_array file_num comment_num description; do - local blob_code=$(echo $file_url_array | jq -r '.[]' | sed -E 's#.*raw/(.*)/.*#\1#' | sort | cut -c -7 | paste -sd '-') + local blob_code=$(echo $file_url_array | tr ',' '\n' | sed -E 's#.*raw/(.*)/.*#\1#' | sort | cut -c -7 | paste -sd '-') echo $link $blob_code $file_num $comment_num $description | tr -d '"' done } @@ -163,7 +180,7 @@ _sync_repos() { # clone repos which are not in the local comm -13 <(find $folder -maxdepth 1 -type d | sed '1d; s#.*/##' | sort) \ <(cat $list_file | cut -d' ' -f2 | sed 's#.*/##' | sort) \ - | xargs -I{} git clone git@github.com:{}.git $folder/{} + | xargs -I{} --max-procs 8 git clone git@github.com:{}.git $folder/{} # pull if remote repo has different blob objects cat $index | cut -d' ' -f2,3 \ @@ -178,8 +195,9 @@ _sync_repos() { echo Everything is fine! } +# get gist id from index files _gist_id() { - GIST_ID=$(cat $index $starred 2> /dev/null | sed -n "/^$1 / p" | cut -d' ' -f2 | sed -E 's#.*/##') + GIST_ID=$( (grep -hs '' $index $starred || true) | sed -n "/^$1 / p" | cut -d' ' -f2 | sed -E 's#.*/##') if [[ -z "$GIST_ID" ]]; then echo -e "Not a valid index: \e[31m$1\e[0m" echo Use the index number in the first column instead: @@ -223,16 +241,43 @@ _clean_repos() { done } +_handle_gist() { + echo ' +print("site:", raw["html_url"]) +print("description:", raw["description"]) +print("public:", raw["public"]) +print("API:", raw["url"]) +print("created_at:", raw["created_at"]) +print("updated_at:", raw["updated_at"]) +print("files:") +for file in raw["files"].keys(): + print(" ", file) + ' +} + +# equal to jq '.[] | {user: .user.login, created_at: .created_at, updated_at: .updated_at, body: .body}' +_handle_comment() { + echo ' +for comment in raw: + print() + print("|", "user:", comment["user"]["login"]) + print("|", "created_at:", comment["created_at"]) + print("|", "updated_at:", comment["updated_at"]) + print("|", comment["body"]) + ' +} + # TODO format with simple text _show_detail() { _gist_id $1 curl -s $github_api/gists/$GIST_ID \ - | jq '{site: .html_url, description: .description, public: .public, API: .url, created_at: .created_at, updated_at: .updated_at, files: (.files | keys)}' + | AccessJsonElement "$(_handle_gist)" curl -s $github_api/gists/$GIST_ID/comments \ - | jq '.[] | {user: .user.login, created_at: .created_at, updated_at: .updated_at, body: .body}' + | AccessJsonElement "$(_handle_comment)" } +# FIXME put file before parameters _set_gist() { while [[ "$1" =~ ^- && "$1" != "--" ]]; do case $1 in -d | --desc) @@ -266,14 +311,9 @@ _create_gist() { [[ -z "$description" ]] && read -p 'Type description: ' description < /dev/tty for file in $files; do - FILE=$(basename $file) - jq --arg FILE "$FILE" '. as $content | { ($FILE): {content: $content} }' -Rs $file - done \ - | jq --slurp --arg DESC "$description" '{ - public: true, - files: add, - description: ($DESC) - }' \ + echo "\"$(basename $file)\": {\"content\": \"$(sed '$ !s/$/\\n/' $file)\"}," + done | tr -d '\n' | sed 's/^/{/; s/,$/}/' \ + | echo "{ \"public\": true, \"files\": $(cat -), \"description\": \"$description\"}" \ | curl -s -H "$auth_header" --data @- $github_api/gists \ | sed '1 s/^/[/; $ s/$/]/' \ | _parse_response \ -- cgit v1.2.3-70-g09d2