diff options
| author | Hsieh Chin Fan <typebrook@gmail.com> | 2020-06-19 11:13:57 +0800 |
|---|---|---|
| committer | Hsieh Chin Fan <typebrook@gmail.com> | 2020-06-19 11:13:57 +0800 |
| commit | e97bf1e0016aa0347b5d9a65978b1e8a5c510799 (patch) | |
| tree | 3db9a7b47fb47dce82655c85661dfa06ea8699c7 | |
| parent | 0e5187731a7269a6841d1345cde64ab8b74bd238 (diff) | |
Refactor code
| -rwxr-xr-x | gist | 40 |
1 files changed, 17 insertions, 23 deletions
| @@ -59,6 +59,7 @@ configuredClient="" | |||
| 59 | 59 | ||
| 60 | NAME=${GISTSCRIPT:-$(basename $0)} #show hint and helper message with current script name | 60 | NAME=${GISTSCRIPT:-$(basename $0)} #show hint and helper message with current script name |
| 61 | GITHUB_API=https://api.github.com | 61 | GITHUB_API=https://api.github.com |
| 62 | GIST_DOMAIN=https://gist.github.com | ||
| 62 | CONFIG=~/.config/gist.conf; mkdir -p ~/.config | 63 | CONFIG=~/.config/gist.conf; mkdir -p ~/.config |
| 63 | per_page=100 | 64 | per_page=100 |
| 64 | 65 | ||
| @@ -300,7 +301,7 @@ _print_records() { | |||
| 300 | 301 | ||
| 301 | sed -Ee "/^$mark/ !d; /^$(_index_pattern) / !d" $INDEX \ | 302 | sed -Ee "/^$mark/ !d; /^$(_index_pattern) / !d" $INDEX \ |
| 302 | | while read -r "${INDEX_FORMAT[@]}"; do | 303 | | while read -r "${INDEX_FORMAT[@]}"; do |
| 303 | local message=$(_site_url $gist_id) | 304 | local message=${GIST_DOMAIN}/${gist_id} |
| 304 | if [[ $display == 'tag' ]]; then | 305 | if [[ $display == 'tag' ]]; then |
| 305 | local tags=( ${tags_string//,/ } ); message="${tags[@]}" | 306 | local tags=( ${tags_string//,/ } ); message="${tags[@]}" |
| 306 | [[ $show_untagged == 'false' && ${#tags[@]} == '0' ]] && continue | 307 | [[ $show_untagged == 'false' && ${#tags[@]} == '0' ]] && continue |
| @@ -430,7 +431,7 @@ _update_gists() { | |||
| 430 | [[ ! -s $fetched_records ]] && echo 'Not a single valid gist' && return 0 | 431 | [[ ! -s $fetched_records ]] && echo 'Not a single valid gist' && return 0 |
| 431 | sed -i'' -Ee "/^$mark/ d" $INDEX | 432 | sed -i'' -Ee "/^$mark/ d" $INDEX |
| 432 | 433 | ||
| 433 | 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" | 434 | extra="s0 True ${GIST_DOMAIN}/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 | [[ $mark == s ]] && echo $extra >> $INDEX | 435 | [[ $mark == s ]] && echo $extra >> $INDEX |
| 435 | 436 | ||
| 436 | tac $fetched_records | nl -s' ' \ | 437 | tac $fetched_records | nl -s' ' \ |
| @@ -492,14 +493,12 @@ _sync_repos() { | |||
| 492 | } | 493 | } |
| 493 | } | 494 | } |
| 494 | 495 | ||
| 495 | _site_url() { echo https://gist.github.com/$1; } | ||
| 496 | |||
| 497 | # Get the url where to clone repo, take user and repo name as parameters | 496 | # Get the url where to clone repo, take user and repo name as parameters |
| 498 | _repo_url() { | 497 | _repo_url() { |
| 499 | if [[ $protocol == 'ssh' ]]; then | 498 | if [[ $protocol == 'ssh' ]]; then |
| 500 | echo "git@gist.github.com:$1.git" | 499 | echo "git@gist.github.com:$1.git" |
| 501 | else | 500 | else |
| 502 | echo "https://gist.github.com/$1.git" | 501 | echo "${GIST_DOMAIN}/$1.git" |
| 503 | fi | 502 | fi |
| 504 | } | 503 | } |
| 505 | 504 | ||
| @@ -527,8 +526,8 @@ _set_gist_id() { | |||
| 527 | 526 | ||
| 528 | # Show path of repo by gist ID, and perform action | 527 | # Show path of repo by gist ID, and perform action |
| 529 | _goto_gist() { | 528 | _goto_gist() { |
| 530 | echo "$folder/$GIST_ID" | 529 | echo "${folder}/${GIST_ID}" |
| 531 | touch "$folder/$GIST_ID" | 530 | touch "${folder}/${GIST_ID}" |
| 532 | 531 | ||
| 533 | if [[ $* =~ (-n|--no-action) ]]; then | 532 | if [[ $* =~ (-n|--no-action) ]]; then |
| 534 | return 0 | 533 | return 0 |
| @@ -536,7 +535,7 @@ _goto_gist() { | |||
| 536 | action='echo Inside subshell, press \<CTRL-D\> to exit; ${SHELL:-bash}' | 535 | action='echo Inside subshell, press \<CTRL-D\> to exit; ${SHELL:-bash}' |
| 537 | fi | 536 | fi |
| 538 | 537 | ||
| 539 | cd "$folder/$GIST_ID" && eval "$action" | 538 | cd "${folder}/${GIST_ID}" && eval "$action" |
| 540 | } | 539 | } |
| 541 | 540 | ||
| 542 | # Return the path of local repo with a given index | 541 | # Return the path of local repo with a given index |
| @@ -567,7 +566,7 @@ _delete_gist() { | |||
| 567 | 566 | ||
| 568 | for i in "$@"; do | 567 | for i in "$@"; do |
| 569 | _gist_id "$i" &> /dev/null || continue | 568 | _gist_id "$i" &> /dev/null || continue |
| 570 | http_method DELETE "$GITHUB_API/gists/$GIST_ID" \ | 569 | http_method DELETE "$GITHUB_API/gists/${GIST_ID}" \ |
| 571 | && echo "$i" deleted \ | 570 | && echo "$i" deleted \ |
| 572 | && sed -i'' -Ee "/^$i / d" $INDEX | 571 | && sed -i'' -Ee "/^$i / d" $INDEX |
| 573 | done | 572 | done |
| @@ -600,12 +599,12 @@ for comment in raw: | |||
| 600 | _show_detail() { | 599 | _show_detail() { |
| 601 | _set_gist_id $1 || return 1 | 600 | _set_gist_id $1 || return 1 |
| 602 | 601 | ||
| 603 | sed -En -e "/[^ ]+ [^ ]+$GIST_ID / {p; q}" $INDEX \ | 602 | sed -En -e "/[^ ]+ [^ ]+${GIST_ID} / {p; q}" $INDEX \ |
| 604 | | while read -r "${INDEX_FORMAT[@]}"; do | 603 | | while read -r "${INDEX_FORMAT[@]}"; do |
| 605 | echo -e desc: $(_color_pattern '^(\[.+\])' <<<"$description") | 604 | echo -e desc: $(_color_pattern '^(\[.+\])' <<<"$description") |
| 606 | echo -e tags: ${tags_string//,/ } | 605 | echo -e tags: ${tags_string//,/ } |
| 607 | echo -e site: https://gist.github.com/$GIST_ID | 606 | echo -e site: ${GIST_DOMAIN}/${GIST_ID} |
| 608 | echo -e API : https://api.github.com/gists/$GIST_ID | 607 | echo -e API : https://api.github.com/gists/${GIST_ID} |
| 609 | echo -e created_at: $created_at | 608 | echo -e created_at: $created_at |
| 610 | echo -e updated_at: $updated_at | 609 | echo -e updated_at: $updated_at |
| 611 | echo -e files: | 610 | echo -e files: |
| @@ -617,7 +616,7 @@ _show_detail() { | |||
| 617 | _export_to_github() { | 616 | _export_to_github() { |
| 618 | _gist_id "$1" || return 1 | 617 | _gist_id "$1" || return 1 |
| 619 | echo Put the folowing URL into web page: | 618 | echo Put the folowing URL into web page: |
| 620 | echo -n "https://gist.github.com/$GIST_ID.git" | 619 | echo -n "${GIST_DOMAIN}/${GIST_ID}.git" |
| 621 | python -mwebbrowser https://github.com/new/import | 620 | python -mwebbrowser https://github.com/new/import |
| 622 | } | 621 | } |
| 623 | 622 | ||
| @@ -630,16 +629,16 @@ _id_to_index() { | |||
| 630 | # Simply commit current changes and push to remote | 629 | # Simply commit current changes and push to remote |
| 631 | _push_to_remote() { | 630 | _push_to_remote() { |
| 632 | _set_gist_id $1 || return 1 | 631 | _set_gist_id $1 || return 1 |
| 633 | cd "$folder/$GIST_ID" | 632 | cd "${folder}/${GIST_ID}" |
| 634 | 633 | ||
| 635 | local index=$(_id_to_index $GIST_ID) | 634 | local index=$(_id_to_index ${GIST_ID}) |
| 636 | 635 | ||
| 637 | if [[ -n $(git status --short) ]]; then | 636 | if [[ -n $(git status --short) ]]; then |
| 638 | git add . && git commit -m 'update' | 637 | git add . && git commit -m 'update' |
| 639 | fi | 638 | fi |
| 640 | if [[ -n $(git cherry) ]]; then | 639 | if [[ -n $(git cherry) ]]; then |
| 641 | git push origin master && \ | 640 | git push origin master && \ |
| 642 | http_method GET "$GITHUB_API/gists/$GIST_ID" | _update_gist $index | 641 | http_method GET "$GITHUB_API/gists/${GIST_ID}" | _update_gist $index |
| 643 | fi | 642 | fi |
| 644 | } | 643 | } |
| 645 | 644 | ||
| @@ -740,7 +739,7 @@ _edit_gist() { | |||
| 740 | http_data=$(tmp_file PAYLOAD.EDIT) | 739 | http_data=$(tmp_file PAYLOAD.EDIT) |
| 741 | echo '{' \"description\": \""${DESC//\"/\\\"}"\" '}' > "$http_data" | 740 | echo '{' \"description\": \""${DESC//\"/\\\"}"\" '}' > "$http_data" |
| 742 | 741 | ||
| 743 | http_method PATCH "$GITHUB_API/gists/$GIST_ID" | _update_gist $index \ | 742 | http_method PATCH "${GITHUB_API}/gists/${GIST_ID}" | _update_gist $index \ |
| 744 | && hint=false mark="$index " _print_records \ | 743 | && hint=false mark="$index " _print_records \ |
| 745 | || echo 'Fail to modify gist description' | 744 | || echo 'Fail to modify gist description' |
| 746 | } | 745 | } |
| @@ -756,12 +755,7 @@ _check_protocol() { | |||
| 756 | find $folder -maxdepth 1 -mindepth 1 -type d \ | 755 | find $folder -maxdepth 1 -mindepth 1 -type d \ |
| 757 | | while read -r repo; do | 756 | | while read -r repo; do |
| 758 | cd "$repo" || exit 1 | 757 | cd "$repo" || exit 1 |
| 759 | url=$(git remote get-url origin) | 758 | git remote set-url origin $(_repo_url $(basename $(pwd))) |
| 760 | if [[ $protocol == 'ssh' && $url =~ ^https ]]; then | ||
| 761 | git remote set-url origin "git@gist.github.com:$(basename $(pwd)).git" | ||
| 762 | elif [[ $protocol == 'https' && $url =~ ^git ]]; then | ||
| 763 | git remote set-url origin "https://gist.github.com/$(basename $(pwd)).git" | ||
| 764 | fi | ||
| 765 | done | 759 | done |
| 766 | } | 760 | } |
| 767 | 761 | ||