diff options
| author | typebrook <typebrook@gmail.com> | 2020-02-06 16:11:10 +0800 |
|---|---|---|
| committer | typebrook <typebrook@gmail.com> | 2020-02-06 16:11:10 +0800 |
| commit | 781505df9325fc4902cc3f399f4844a48e02d5bd (patch) | |
| tree | 3219a3babb4b8349db6ebd12313c3d65bff5ef1c | |
| parent | 3c0f9beb2232314d70f52902f3e72c422a3e065f (diff) | |
update
| -rwxr-xr-x | gist | 53 |
1 files changed, 36 insertions, 17 deletions
| @@ -18,20 +18,20 @@ | |||
| 18 | # edit, e <index_of_gist> edit a gist description | 18 | # edit, e <index_of_gist> edit a gist description |
| 19 | # delete, D <index_of_gist>... delete a gist | 19 | # delete, D <index_of_gist>... delete a gist |
| 20 | # clean, C clean removed gists in local | 20 | # clean, C clean removed gists in local |
| 21 | # config, c [token | user | folder | auto-sync | EDITOR | action [value] ] do configuration | 21 | # config, c [token | user | folder | auto_sync | EDITOR | action [value] ] do configuration |
| 22 | # user, U <user> get gists from a given Github user | 22 | # user, U <user> get gists from a given Github user |
| 23 | # grep, g <pattern> grep gists by a given pattern | ||
| 24 | # push, p <pattern> push changes by git | ||
| 23 | # help, h show this help message | 25 | # help, h show this help message |
| 24 | # | 26 | # |
| 25 | # Example: | 27 | # Example: |
| 26 | # gist (Show your gists) | 28 | # gist (Show your gists) |
| 27 | # gist 3 (show the repo path of your 3rd gist, and do custom actions) | 29 | # gist 3 (show the repo path of your 3rd gist, and do custom actions) |
| 28 | # | 30 | # |
| 29 | # Since now a gist is a local cloned repo | 31 | # Since now a gist is a local cloned repo |
| 30 | # It is your business to do git commit and git push | 32 | # It is your business to do git commit and git push |
| 31 | 33 | ||
| 32 | # TODO grep mode for description, file content | 34 | # TODO import to github repo (may need a new token) |
| 33 | # TODO push github.com (may need new token) | ||
| 34 | # TODO unit test | ||
| 35 | # TODO test on bats, mac and remote machine | 35 | # TODO test on bats, mac and remote machine |
| 36 | # TODO completion | 36 | # TODO completion |
| 37 | 37 | ||
| @@ -55,7 +55,7 @@ _configure() { | |||
| 55 | [[ ${#2} -ne 40 ]] && echo 'Invalid token format, it is not 40 chars' >&2 \ | 55 | [[ ${#2} -ne 40 ]] && echo 'Invalid token format, it is not 40 chars' >&2 \ |
| 56 | && return 1 | 56 | && return 1 |
| 57 | elif [[ $1 == 'auto_sync' ]]; then | 57 | elif [[ $1 == 'auto_sync' ]]; then |
| 58 | [[ ! $2 =~ ^(true|false)$ ]] && return 1 | 58 | [[ ! $2 =~ ^(true|false)$ ]] && return 1 |
| 59 | fi | 59 | fi |
| 60 | target=$1=$2 | 60 | target=$1=$2 |
| 61 | 61 | ||
| @@ -84,7 +84,6 @@ _ask_token() { | |||
| 84 | 84 | ||
| 85 | while [[ ! $token =~ ^[[:alnum:]]{40}$ ]]; do | 85 | while [[ ! $token =~ ^[[:alnum:]]{40}$ ]]; do |
| 86 | [[ -n $token ]] && echo "Not a valid token" | 86 | [[ -n $token ]] && echo "Not a valid token" |
| 87 | trap 'echo; return 1' INT | ||
| 88 | read -p "Paste your token here (Ctrl-C to skip): " token < /dev/tty | 87 | read -p "Paste your token here (Ctrl-C to skip): " token < /dev/tty |
| 89 | done | 88 | done |
| 90 | _configure token $token | 89 | _configure token $token |
| @@ -118,7 +117,7 @@ _apply_config() { | |||
| 118 | AUTH_HEADER="Authorization: token $token" | 117 | AUTH_HEADER="Authorization: token $token" |
| 119 | [[ -z "$action" ]] && action="${EDITOR:-vi} ." | 118 | [[ -z "$action" ]] && action="${EDITOR:-vi} ." |
| 120 | [[ -z "$folder" ]] && folder=~/gist && mkdir -p $folder | 119 | [[ -z "$folder" ]] && folder=~/gist && mkdir -p $folder |
| 121 | INDEX=$folder/index | 120 | INDEX=$folder/index; [[ -e $INDEX ]] || touch $INDEX |
| 122 | } | 121 | } |
| 123 | 122 | ||
| 124 | _apply_config "$@" || exit 1 | 123 | _apply_config "$@" || exit 1 |
| @@ -157,6 +156,7 @@ http_method() { | |||
| 157 | esac | 156 | esac |
| 158 | } | 157 | } |
| 159 | 158 | ||
| 159 | # TODO Split into 2 funcs for filter and display | ||
| 160 | # Show the list of gist, but not updated time | 160 | # Show the list of gist, but not updated time |
| 161 | _show_list() { | 161 | _show_list() { |
| 162 | if [[ ! -e $INDEX ]]; then | 162 | if [[ ! -e $INDEX ]]; then |
| @@ -164,8 +164,8 @@ _show_list() { | |||
| 164 | echo ' gist update' | 164 | echo ' gist update' |
| 165 | return 0 | 165 | return 0 |
| 166 | fi | 166 | fi |
| 167 | local filter='/^ *s/ d; /^$/ d' | 167 | [[ -z $1 ]] && local filter='/^ *s/ d; /^$/ d' |
| 168 | [[ $1 == "s" ]] && filter='/^ *[^ s]/ d; /^$/ d' | 168 | [[ $1 == "s" ]] && local filter='/^ *[^ s]/ d; /^$/ d' |
| 169 | 169 | ||
| 170 | while read index link blob_code file_num extra author description; do | 170 | while read index link blob_code file_num extra author description; do |
| 171 | [[ $1 == "s" ]] && local name=$author | 171 | [[ $1 == "s" ]] && local name=$author |
| @@ -182,7 +182,18 @@ _show_list() { | |||
| 182 | echo -e "$(printf "% 3s" $index)" $link $name $file_num $extra $(echo $description | cut -c -$(( 60 -$occupy -1 )) ) | 182 | echo -e "$(printf "% 3s" $index)" $link $name $file_num $extra $(echo $description | cut -c -$(( 60 -$occupy -1 )) ) |
| 183 | done < $INDEX \ | 183 | done < $INDEX \ |
| 184 | | sed "$filter" | 184 | | sed "$filter" |
| 185 | echo -e '\nrun "gist help" for more details' > /dev/tty | 185 | echo -e '\nrun "gist help" or "gist h" for more details' > /dev/tty |
| 186 | } | ||
| 187 | |||
| 188 | # TODO support filenames, file contents | ||
| 189 | _grep_content() { | ||
| 190 | _show_list other | grep -i $1 | ||
| 191 | } | ||
| 192 | |||
| 193 | _push_to_remote() { | ||
| 194 | _gist_id $1 | ||
| 195 | cd $folder/$GIST_ID && git add . \ | ||
| 196 | && git commit --allow-empty-message -m '' && git push origin master | ||
| 186 | } | 197 | } |
| 187 | 198 | ||
| 188 | # parse JSON from STDIN with string of commands | 199 | # parse JSON from STDIN with string of commands |
| @@ -192,7 +203,6 @@ AccessJsonElement() { | |||
| 192 | return "$?" | 203 | return "$?" |
| 193 | } | 204 | } |
| 194 | 205 | ||
| 195 | # equal to: jq '.[] | "\(.html_url) \([.files[] | .raw_url]) \(.files | keys | length) \(.comments) \(.description)"' | ||
| 196 | _handle_gists() { | 206 | _handle_gists() { |
| 197 | echo ' | 207 | echo ' |
| 198 | raw = json.load(sys.stdin) | 208 | raw = json.load(sys.stdin) |
| @@ -207,7 +217,7 @@ for gist in raw: | |||
| 207 | ' | 217 | ' |
| 208 | } | 218 | } |
| 209 | 219 | ||
| 210 | # TODO check if a user create a very first gist | 220 | # TODO check if a user has no gist |
| 211 | # parse response from gists require | 221 | # parse response from gists require |
| 212 | _parse_response() { | 222 | _parse_response() { |
| 213 | AccessJsonElement "$(_handle_gists)" \ | 223 | AccessJsonElement "$(_handle_gists)" \ |
| @@ -240,7 +250,7 @@ _update() { | |||
| 240 | sed -i "$filter" $INDEX && echo "$result" >> $INDEX | 250 | sed -i "$filter" $INDEX && echo "$result" >> $INDEX |
| 241 | _show_list $mark | 251 | _show_list $mark |
| 242 | 252 | ||
| 243 | if [[ $auto_sync != "false" ]]; then (_sync_repos $1 > /dev/null 2>&1 &); fi | 253 | if [[ $auto_sync != 'false' ]]; then (_sync_repos $1 > /dev/null 2>&1 &); fi |
| 244 | } | 254 | } |
| 245 | 255 | ||
| 246 | _query_user() { | 256 | _query_user() { |
| @@ -306,6 +316,10 @@ _goto_gist() { | |||
| 306 | } | 316 | } |
| 307 | 317 | ||
| 308 | _delete_gist() { | 318 | _delete_gist() { |
| 319 | read -r -p "Delete gists above? [y/N] " response | ||
| 320 | response=${response,,} | ||
| 321 | [[ ! "$response" =~ ^(yes|y)$ ]] && return 0 | ||
| 322 | |||
| 309 | for i in "$@"; do | 323 | for i in "$@"; do |
| 310 | _gist_id "$i" | 324 | _gist_id "$i" |
| 311 | http_method DELETE $GITHUB_API/gists/$GIST_ID \ | 325 | http_method DELETE $GITHUB_API/gists/$GIST_ID \ |
| @@ -352,7 +366,6 @@ for comment in raw: | |||
| 352 | ' | 366 | ' |
| 353 | } | 367 | } |
| 354 | 368 | ||
| 355 | # TODO format with simple text | ||
| 356 | _show_detail() { | 369 | _show_detail() { |
| 357 | _gist_id $1 | 370 | _gist_id $1 |
| 358 | http_method GET $GITHUB_API/gists/$GIST_ID \ | 371 | http_method GET $GITHUB_API/gists/$GIST_ID \ |
| @@ -441,7 +454,7 @@ _edit_gist() { | |||
| 441 | read DESC < /dev/tty | 454 | read DESC < /dev/tty |
| 442 | 455 | ||
| 443 | http_data=$(mktemp) | 456 | http_data=$(mktemp) |
| 444 | echo "{ \"description\": \"$DESC\" }" > $http_data | 457 | echo { \"description\": \"$(echo $DESC | sed 's/"/\\"/g')\" } > $http_data |
| 445 | http_method PATCH $http_data $GITHUB_API/gists/$GIST_ID > /dev/null \ | 458 | http_method PATCH $http_data $GITHUB_API/gists/$GIST_ID > /dev/null \ |
| 446 | && _update | 459 | && _update |
| 447 | } | 460 | } |
| @@ -480,6 +493,12 @@ case "$1" in | |||
| 480 | user | U) | 493 | user | U) |
| 481 | shift | 494 | shift |
| 482 | _query_user "$@" ;; | 495 | _query_user "$@" ;; |
| 496 | grep | g) | ||
| 497 | shift | ||
| 498 | _grep_content "$@" ;; | ||
| 499 | push | p) | ||
| 500 | shift | ||
| 501 | _push_to_remote "$1" ;; | ||
| 483 | help | h) | 502 | help | h) |
| 484 | usage ;; | 503 | usage ;; |
| 485 | *) | 504 | *) |