aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authortypebrook <typebrook@gmail.com>2020-05-17 11:27:56 +0800
committertypebrook <typebrook@gmail.com>2020-05-17 11:27:56 +0800
commitd55b5b062ba8380413f3e7860bb4abe6390a310a (patch)
tree1759bd67dc941b9bb0044edc32fff9816b8df7dd
parent48657936432fbbea797c4822234f41f739fd05e9 (diff)
Improve code for update a single gist
-rwxr-xr-xgist17
1 files 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() {
436 #result=$(printf "${extra:+$extra\n}$result") 436 #result=$(printf "${extra:+$extra\n}$result")
437} 437}
438 438
439# Get a single JSON object of gist from response, and update index file
440_update_gist() {
441 local index=$1
442 local record=$(sed -e '1 s/^/[/; $ s/$/]/' | start_from_num="${index#[[:alpha:]]}" _parse_response)
443 [[ -n $record ]] && sed -i'' -Ee "/^$index / s^.+^$record^" $INDEX
444}
445
439# Fetch gists for a given user 446# Fetch gists for a given user
440# TODO pagnation for more than 100 gists 447# TODO pagnation for more than 100 gists
441_query_user() { 448_query_user() {
@@ -621,8 +628,8 @@ _push_to_remote() {
621 git add . && git commit -m 'update' 628 git add . && git commit -m 'update'
622 fi 629 fi
623 if [[ -n $(git cherry) ]]; then 630 if [[ -n $(git cherry) ]]; then
624 # FIXME only fetch one gist 631 git push origin master && \
625 git push origin master && (hint=false _fetch_gists_with_pagnation &> /dev/null &) 632 (http_method GET "$GITHUB_API/gists/$GIST_ID" | _update_gist $1 &> /dev/null &)
626 fi 633 fi
627} 634}
628 635
@@ -720,10 +727,8 @@ _edit_gist() {
720 727
721 http_data=$(tmp_file PAYLOAD.EDIT) 728 http_data=$(tmp_file PAYLOAD.EDIT)
722 echo '{' \"description\": \""${DESC//\"/\\\"}"\" '}' > "$http_data" 729 echo '{' \"description\": \""${DESC//\"/\\\"}"\" '}' > "$http_data"
723 new_record=$(http_method PATCH "$GITHUB_API/gists/$GIST_ID" \ 730
724 | sed -e '1 s/^/[/; $ s/$/]/' \ 731 http_method PATCH "$GITHUB_API/gists/$GIST_ID" | _update_gist $index \
725 | start_from_index="${index#[[:alpha:]]}" _parse_response)
726 [[ -n $new_record ]] && sed -i'' -E -e "/^$index / s^.+^$new_record^" $INDEX \
727 && hint=false mark="$index " _show_list \ 732 && hint=false mark="$index " _show_list \
728 || echo 'Fail to modify gist description' 733 || echo 'Fail to modify gist description'
729} 734}