From e97bf1e0016aa0347b5d9a65978b1e8a5c510799 Mon Sep 17 00:00:00 2001 From: Hsieh Chin Fan Date: Fri, 19 Jun 2020 11:13:57 +0800 Subject: Refactor code --- gist | 40 +++++++++++++++++----------------------- 1 file changed, 17 insertions(+), 23 deletions(-) diff --git a/gist b/gist index a6b2ee0..e7cafc5 100755 --- a/gist +++ b/gist @@ -59,6 +59,7 @@ configuredClient="" NAME=${GISTSCRIPT:-$(basename $0)} #show hint and helper message with current script name GITHUB_API=https://api.github.com +GIST_DOMAIN=https://gist.github.com CONFIG=~/.config/gist.conf; mkdir -p ~/.config per_page=100 @@ -300,7 +301,7 @@ _print_records() { sed -Ee "/^$mark/ !d; /^$(_index_pattern) / !d" $INDEX \ | while read -r "${INDEX_FORMAT[@]}"; do - local message=$(_site_url $gist_id) + local message=${GIST_DOMAIN}/${gist_id} if [[ $display == 'tag' ]]; then local tags=( ${tags_string//,/ } ); message="${tags[@]}" [[ $show_untagged == 'false' && ${#tags[@]} == '0' ]] && continue @@ -430,7 +431,7 @@ _update_gists() { [[ ! -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" + 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" [[ $mark == s ]] && echo $extra >> $INDEX tac $fetched_records | nl -s' ' \ @@ -492,14 +493,12 @@ _sync_repos() { } } -_site_url() { echo https://gist.github.com/$1; } - # Get the url where to clone repo, take user and repo name as parameters _repo_url() { if [[ $protocol == 'ssh' ]]; then echo "git@gist.github.com:$1.git" else - echo "https://gist.github.com/$1.git" + echo "${GIST_DOMAIN}/$1.git" fi } @@ -527,8 +526,8 @@ _set_gist_id() { # Show path of repo by gist ID, and perform action _goto_gist() { - echo "$folder/$GIST_ID" - touch "$folder/$GIST_ID" + echo "${folder}/${GIST_ID}" + touch "${folder}/${GIST_ID}" if [[ $* =~ (-n|--no-action) ]]; then return 0 @@ -536,7 +535,7 @@ _goto_gist() { action='echo Inside subshell, press \ to exit; ${SHELL:-bash}' fi - cd "$folder/$GIST_ID" && eval "$action" + cd "${folder}/${GIST_ID}" && eval "$action" } # Return the path of local repo with a given index @@ -567,7 +566,7 @@ _delete_gist() { for i in "$@"; do _gist_id "$i" &> /dev/null || continue - http_method DELETE "$GITHUB_API/gists/$GIST_ID" \ + http_method DELETE "$GITHUB_API/gists/${GIST_ID}" \ && echo "$i" deleted \ && sed -i'' -Ee "/^$i / d" $INDEX done @@ -600,12 +599,12 @@ for comment in raw: _show_detail() { _set_gist_id $1 || return 1 - sed -En -e "/[^ ]+ [^ ]+$GIST_ID / {p; q}" $INDEX \ + sed -En -e "/[^ ]+ [^ ]+${GIST_ID} / {p; q}" $INDEX \ | while read -r "${INDEX_FORMAT[@]}"; do echo -e desc: $(_color_pattern '^(\[.+\])' <<<"$description") echo -e tags: ${tags_string//,/ } - echo -e site: https://gist.github.com/$GIST_ID - echo -e API : https://api.github.com/gists/$GIST_ID + echo -e site: ${GIST_DOMAIN}/${GIST_ID} + echo -e API : https://api.github.com/gists/${GIST_ID} echo -e created_at: $created_at echo -e updated_at: $updated_at echo -e files: @@ -617,7 +616,7 @@ _show_detail() { _export_to_github() { _gist_id "$1" || return 1 echo Put the folowing URL into web page: - echo -n "https://gist.github.com/$GIST_ID.git" + echo -n "${GIST_DOMAIN}/${GIST_ID}.git" python -mwebbrowser https://github.com/new/import } @@ -630,16 +629,16 @@ _id_to_index() { # Simply commit current changes and push to remote _push_to_remote() { _set_gist_id $1 || return 1 - cd "$folder/$GIST_ID" + cd "${folder}/${GIST_ID}" - local index=$(_id_to_index $GIST_ID) + local index=$(_id_to_index ${GIST_ID}) if [[ -n $(git status --short) ]]; then git add . && git commit -m 'update' fi if [[ -n $(git cherry) ]]; then git push origin master && \ - http_method GET "$GITHUB_API/gists/$GIST_ID" | _update_gist $index + http_method GET "$GITHUB_API/gists/${GIST_ID}" | _update_gist $index fi } @@ -740,7 +739,7 @@ _edit_gist() { http_data=$(tmp_file PAYLOAD.EDIT) echo '{' \"description\": \""${DESC//\"/\\\"}"\" '}' > "$http_data" - http_method PATCH "$GITHUB_API/gists/$GIST_ID" | _update_gist $index \ + http_method PATCH "${GITHUB_API}/gists/${GIST_ID}" | _update_gist $index \ && hint=false mark="$index " _print_records \ || echo 'Fail to modify gist description' } @@ -756,12 +755,7 @@ _check_protocol() { find $folder -maxdepth 1 -mindepth 1 -type d \ | while read -r repo; do cd "$repo" || exit 1 - url=$(git remote get-url origin) - if [[ $protocol == 'ssh' && $url =~ ^https ]]; then - git remote set-url origin "git@gist.github.com:$(basename $(pwd)).git" - elif [[ $protocol == 'https' && $url =~ ^git ]]; then - git remote set-url origin "https://gist.github.com/$(basename $(pwd)).git" - fi + git remote set-url origin $(_repo_url $(basename $(pwd))) done } -- cgit v1.2.3-70-g09d2