diff options
| -rwxr-xr-x | gist | 62 |
1 files changed, 33 insertions, 29 deletions
| @@ -389,9 +389,15 @@ _parse_response() { | |||
| 389 | done | 389 | done |
| 390 | } | 390 | } |
| 391 | 391 | ||
| 392 | # Get a single JSON object of gist from response, and update index file | ||
| 393 | _update_gist() { | ||
| 394 | local record="$(sed -e '1 s/^/[/; $ s/$/]/' | index=$1 _parse_response)" | ||
| 395 | [[ -n $record ]] && sed -i'' -Ee "/^$1 / s^.+^$record^" $INDEX | ||
| 396 | } | ||
| 397 | |||
| 392 | # Get latest list of gists from Github API | 398 | # Get latest list of gists from Github API |
| 393 | _fetch_gists() { | 399 | _fetch_gists() { |
| 394 | local route="users/$user/gists" | 400 | local route=${route:-users/$user/gists} |
| 395 | if [[ $mark == s ]]; then | 401 | if [[ $mark == s ]]; then |
| 396 | route='gists/starred' | 402 | route='gists/starred' |
| 397 | fi | 403 | fi |
| @@ -403,54 +409,52 @@ _fetch_gists() { | |||
| 403 | 409 | ||
| 404 | # consider if HEADER is not exist | 410 | # consider if HEADER is not exist |
| 405 | _fetch_gists_with_pagnation() { | 411 | _fetch_gists_with_pagnation() { |
| 406 | echo "fetching $user's gists from $GITHUB_API..." | 412 | _fetch_gists "?per_page=$per_page" >> $1 |
| 407 | echo | ||
| 408 | |||
| 409 | local fetched_records=$(tmp_file fetched) | ||
| 410 | _fetch_gists "?per_page=$per_page" >> $fetched_records | ||
| 411 | 413 | ||
| 412 | while true; do | 414 | while true; do |
| 413 | local next_page='' | 415 | local next_page='' |
| 414 | [[ -e $HEADER ]] && next_page=$(sed -Ene '/^[lL]ink: / s/.+page=([[:digit:]]+)>; rel=\"next\".+/\1/p' $HEADER) | 416 | [[ -e $HEADER ]] && next_page=$(sed -Ene '/^[lL]ink: / s/.+page=([[:digit:]]+)>; rel=\"next\".+/\1/p' $HEADER) |
| 415 | [[ -z $next_page ]] && break | 417 | [[ -z $next_page ]] && break |
| 416 | printf "%-4s gists fetched\n" $(( ($next_page -1) * $per_page )) | 418 | printf "%-4s gists fetched\n" $(( ($next_page -1) * $per_page )) > /dev/tty |
| 417 | 419 | ||
| 418 | _fetch_gists "?per_page=$per_page&page=$next_page" >> $fetched_records | 420 | _fetch_gists "?per_page=$per_page&page=$next_page" >> $1 |
| 419 | done || return 1 | 421 | done || return 1 |
| 422 | } | ||
| 420 | 423 | ||
| 421 | [[ ! -s $fetched_records ]] && echo 'Not a single valid gist' && return 0 | 424 | # Update index file by GITHUB API with pagnation |
| 425 | _update_gists() { | ||
| 426 | echo "fetching $user's gists from $GITHUB_API..." | ||
| 427 | echo | ||
| 428 | |||
| 429 | local fetched_records=$(tmp_file fetched) | ||
| 430 | _fetch_gists_with_pagnation $fetched_records || { echo Something screwed; exit 1; } | ||
| 422 | 431 | ||
| 432 | [[ ! -s $fetched_records ]] && echo 'Not a single valid gist' && return 0 | ||
| 423 | sed -i'' -Ee "/^$mark/ d" $INDEX | 433 | sed -i'' -Ee "/^$mark/ d" $INDEX |
| 434 | |||
| 435 | 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" | ||
| 436 | [[ $mark == s ]] && echo $extra >> $INDEX | ||
| 437 | |||
| 424 | tac <$fetched_records | nl -s' ' \ | 438 | tac <$fetched_records | nl -s' ' \ |
| 425 | | while read -r "${INDEX_FORMAT[@]:0:2}" extra; do | 439 | | while read -r "${INDEX_FORMAT[@]:0:2}" extra; do |
| 426 | local prefix=''; [[ $public == False ]] && prefix=p | 440 | local prefix='' |
| 441 | [[ $public == False ]] && prefix=p; [[ $mark == s ]] && prefix=s | ||
| 427 | echo $prefix$index $public $extra | 442 | echo $prefix$index $public $extra |
| 428 | done >> $INDEX | 443 | done >> $INDEX |
| 429 | hint=$hint _show_list | ||
| 430 | 444 | ||
| 445 | hint=$hint _show_list | ||
| 431 | [[ $auto_sync != false ]] && (_sync_repos &> /dev/null &) | 446 | [[ $auto_sync != false ]] && (_sync_repos &> /dev/null &) |
| 432 | true | 447 | true |
| 433 | #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" | ||
| 434 | #result=$(printf "${extra:+$extra\n}$result") | ||
| 435 | } | ||
| 436 | |||
| 437 | # Get a single JSON object of gist from response, and update index file | ||
| 438 | _update_gist() { | ||
| 439 | local record="$(sed -e '1 s/^/[/; $ s/$/]/' | index=$1 _parse_response)" | ||
| 440 | [[ -n $record ]] && sed -i'' -Ee "/^$1 / s^.+^$record^" $INDEX | ||
| 441 | } | 448 | } |
| 442 | 449 | ||
| 443 | # Fetch gists for a given user | 450 | # Fetch gists for a given user |
| 444 | # TODO pagnation for more than 100 gists | ||
| 445 | _query_user() { | 451 | _query_user() { |
| 446 | local route="users/$1/gists" | 452 | local fetched_records=$(tmp_file fetched) |
| 447 | result="$(http_method GET $GITHUB_API/$route?per_page=100 | _parse_response)" | 453 | route=users/$1/gists _fetch_gists_with_pagnation $fetched_records |
| 448 | [[ -z $result ]] && echo "Failed to query $1's gists" && return 1 | 454 | cat $fetched_records \ |
| 449 | 455 | | while read -r ${INDEX_FORMAT[@]#index}; do | |
| 450 | echo "$result" \ | ||
| 451 | | while read -r "${INDEX_FORMAT[@]}"; do | ||
| 452 | echo "$url $author $file_num $comment_num $description" | cut -c -"$(tput cols)" | 456 | echo "$url $author $file_num $comment_num $description" | cut -c -"$(tput cols)" |
| 453 | done | 457 | done || { echo "Failed to query $1's gists"; exit 1; } |
| 454 | } | 458 | } |
| 455 | 459 | ||
| 456 | # Return the unique code for current commit, to compare repo status and the result of 'gist fetch' | 460 | # Return the unique code for current commit, to compare repo status and the result of 'gist fetch' |
| @@ -838,7 +842,7 @@ _access_last_index() { | |||
| 838 | } | 842 | } |
| 839 | 843 | ||
| 840 | _apply_config "$@" || exit 1 | 844 | _apply_config "$@" || exit 1 |
| 841 | if [[ $init ]]; then _fetch_gists_with_pagnation; exit 0; fi | 845 | if [[ $init ]]; then _update_gists; exit 0; fi |
| 842 | case "$1" in | 846 | case "$1" in |
| 843 | "") | 847 | "") |
| 844 | _show_list ;; | 848 | _show_list ;; |
| @@ -848,7 +852,7 @@ case "$1" in | |||
| 848 | mark=.; _show_list ;; | 852 | mark=.; _show_list ;; |
| 849 | fetch | f) | 853 | fetch | f) |
| 850 | [[ $2 =~ ^(s|star)$ ]] && mark=s || mark=[^s] | 854 | [[ $2 =~ ^(s|star)$ ]] && mark=s || mark=[^s] |
| 851 | _fetch_gists_with_pagnation ;; | 855 | _update_gists ;; |
| 852 | new | n) | 856 | new | n) |
| 853 | shift | 857 | shift |
| 854 | _create_gist "$@" ;; | 858 | _create_gist "$@" ;; |