From d55b5b062ba8380413f3e7860bb4abe6390a310a Mon Sep 17 00:00:00 2001 From: typebrook Date: Sun, 17 May 2020 11:27:56 +0800 Subject: Improve code for update a single gist --- gist | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/gist b/gist index b9cb192..6e29fde 100755 --- a/gist +++ b/gist @@ -436,6 +436,13 @@ _fetch_gists_with_pagnation() { #result=$(printf "${extra:+$extra\n}$result") } +# Get a single JSON object of gist from response, and update index file +_update_gist() { + local index=$1 + local record=$(sed -e '1 s/^/[/; $ s/$/]/' | start_from_num="${index#[[:alpha:]]}" _parse_response) + [[ -n $record ]] && sed -i'' -Ee "/^$index / s^.+^$record^" $INDEX +} + # Fetch gists for a given user # TODO pagnation for more than 100 gists _query_user() { @@ -621,8 +628,8 @@ _push_to_remote() { git add . && git commit -m 'update' fi if [[ -n $(git cherry) ]]; then - # FIXME only fetch one gist - git push origin master && (hint=false _fetch_gists_with_pagnation &> /dev/null &) + git push origin master && \ + (http_method GET "$GITHUB_API/gists/$GIST_ID" | _update_gist $1 &> /dev/null &) fi } @@ -720,10 +727,8 @@ _edit_gist() { http_data=$(tmp_file PAYLOAD.EDIT) echo '{' \"description\": \""${DESC//\"/\\\"}"\" '}' > "$http_data" - new_record=$(http_method PATCH "$GITHUB_API/gists/$GIST_ID" \ - | sed -e '1 s/^/[/; $ s/$/]/' \ - | start_from_index="${index#[[:alpha:]]}" _parse_response) - [[ -n $new_record ]] && sed -i'' -E -e "/^$index / s^.+^$new_record^" $INDEX \ + + http_method PATCH "$GITHUB_API/gists/$GIST_ID" | _update_gist $index \ && hint=false mark="$index " _show_list \ || echo 'Fail to modify gist description' } -- cgit v1.2.3-70-g09d2