From ead327cc8efc623968b2a14f8a3846b1d79ae630 Mon Sep 17 00:00:00 2001 From: typebrook Date: Sun, 17 May 2020 22:14:22 +0800 Subject: Refactor gist fetching functions - query gists by user now supports pagnation - print '[num] gists fetched' to tty --- gist | 62 +++++++++++++++++++++++++++++++++----------------------------- 1 file changed, 33 insertions(+), 29 deletions(-) diff --git a/gist b/gist index a3b54ed..0bd4f63 100755 --- a/gist +++ b/gist @@ -389,9 +389,15 @@ _parse_response() { done } +# Get a single JSON object of gist from response, and update index file +_update_gist() { + local record="$(sed -e '1 s/^/[/; $ s/$/]/' | index=$1 _parse_response)" + [[ -n $record ]] && sed -i'' -Ee "/^$1 / s^.+^$record^" $INDEX +} + # Get latest list of gists from Github API _fetch_gists() { - local route="users/$user/gists" + local route=${route:-users/$user/gists} if [[ $mark == s ]]; then route='gists/starred' fi @@ -403,54 +409,52 @@ _fetch_gists() { # consider if HEADER is not exist _fetch_gists_with_pagnation() { - echo "fetching $user's gists from $GITHUB_API..." - echo - - local fetched_records=$(tmp_file fetched) - _fetch_gists "?per_page=$per_page" >> $fetched_records + _fetch_gists "?per_page=$per_page" >> $1 while true; do local next_page='' [[ -e $HEADER ]] && next_page=$(sed -Ene '/^[lL]ink: / s/.+page=([[:digit:]]+)>; rel=\"next\".+/\1/p' $HEADER) [[ -z $next_page ]] && break - printf "%-4s gists fetched\n" $(( ($next_page -1) * $per_page )) + printf "%-4s gists fetched\n" $(( ($next_page -1) * $per_page )) > /dev/tty - _fetch_gists "?per_page=$per_page&page=$next_page" >> $fetched_records + _fetch_gists "?per_page=$per_page&page=$next_page" >> $1 done || return 1 +} - [[ ! -s $fetched_records ]] && echo 'Not a single valid gist' && return 0 +# Update index file by GITHUB API with pagnation +_update_gists() { + echo "fetching $user's gists from $GITHUB_API..." + echo + + local fetched_records=$(tmp_file fetched) + _fetch_gists_with_pagnation $fetched_records || { echo Something screwed; exit 1; } + [[ ! -s $fetched_records ]] && echo 'Not a single valid gist' && return 0 sed -i'' -Ee "/^$mark/ d" $INDEX + + extra="s0 True https://gist.github.com/b0d2e7e67aa50298fdf8111ae7466b56 #bash,#gist NONE README.md@Markdown,gist@Shell 2 30 typebrook 2019-12-26T06:49:40Z 2020-05-15T13:00:31Z [bash-gist] A bash script for gist management" + [[ $mark == s ]] && echo $extra >> $INDEX + tac <$fetched_records | nl -s' ' \ | while read -r "${INDEX_FORMAT[@]:0:2}" extra; do - local prefix=''; [[ $public == False ]] && prefix=p + local prefix='' + [[ $public == False ]] && prefix=p; [[ $mark == s ]] && prefix=s echo $prefix$index $public $extra done >> $INDEX - hint=$hint _show_list + hint=$hint _show_list [[ $auto_sync != false ]] && (_sync_repos &> /dev/null &) true - #extra="s0 https://gist.github.com/b0d2e7e67aa50298fdf8111ae7466b56 #bash,#gist NONE README.md@Markdown,gist@Shell 2 30 typebrook 2019-12-26T06:49:40Z 2020-05-15T13:00:31Z [bash-gist] A bash script for gist management" - #result=$(printf "${extra:+$extra\n}$result") -} - -# Get a single JSON object of gist from response, and update index file -_update_gist() { - local record="$(sed -e '1 s/^/[/; $ s/$/]/' | index=$1 _parse_response)" - [[ -n $record ]] && sed -i'' -Ee "/^$1 / s^.+^$record^" $INDEX } # Fetch gists for a given user -# TODO pagnation for more than 100 gists _query_user() { - local route="users/$1/gists" - result="$(http_method GET $GITHUB_API/$route?per_page=100 | _parse_response)" - [[ -z $result ]] && echo "Failed to query $1's gists" && return 1 - - echo "$result" \ - | while read -r "${INDEX_FORMAT[@]}"; do + local fetched_records=$(tmp_file fetched) + route=users/$1/gists _fetch_gists_with_pagnation $fetched_records + cat $fetched_records \ + | while read -r ${INDEX_FORMAT[@]#index}; do echo "$url $author $file_num $comment_num $description" | cut -c -"$(tput cols)" - done + done || { echo "Failed to query $1's gists"; exit 1; } } # Return the unique code for current commit, to compare repo status and the result of 'gist fetch' @@ -838,7 +842,7 @@ _access_last_index() { } _apply_config "$@" || exit 1 -if [[ $init ]]; then _fetch_gists_with_pagnation; exit 0; fi +if [[ $init ]]; then _update_gists; exit 0; fi case "$1" in "") _show_list ;; @@ -848,7 +852,7 @@ case "$1" in mark=.; _show_list ;; fetch | f) [[ $2 =~ ^(s|star)$ ]] && mark=s || mark=[^s] - _fetch_gists_with_pagnation ;; + _update_gists ;; new | n) shift _create_gist "$@" ;; -- cgit v1.2.3-70-g09d2