diff options
| author | typebrook <typebrook@gmail.com> | 2020-02-12 12:02:23 +0800 |
|---|---|---|
| committer | typebrook <typebrook@gmail.com> | 2020-02-12 12:02:23 +0800 |
| commit | 726cc1634c9e8dc2673ea94ebbe86e2261e3e2f2 (patch) | |
| tree | 6a3d39b2a39d5496b7203831558ce055dc5de9aa | |
| parent | fd110204d35f4d66a0deb9dadf74d81603f59a23 (diff) | |
update
| -rwxr-xr-x | gist | 32 |
1 files changed, 15 insertions, 17 deletions
| @@ -37,7 +37,6 @@ | |||
| 37 | # Since now a gist is a local cloned repo | 37 | # Since now a gist is a local cloned repo |
| 38 | # It is your business to do git commit and git push | 38 | # It is your business to do git commit and git push |
| 39 | 39 | ||
| 40 | # TODO test on bats, mac and remote machine | ||
| 41 | currentVersion="1.23.0" | 40 | currentVersion="1.23.0" |
| 42 | configuredClient="" | 41 | configuredClient="" |
| 43 | 42 | ||
| @@ -50,8 +49,8 @@ auto_sync=true # automatically clone the gist repo | |||
| 50 | 49 | ||
| 51 | # Shell configuration | 50 | # Shell configuration |
| 52 | set -o pipefail | 51 | set -o pipefail |
| 53 | [ "$TRACE" ] && set -x | 52 | [[ $TRACE ]] && set -x |
| 54 | [ $(uname) == 'Darwin' ] && alias tac='tail -r' | 53 | [[ $(uname) == 'Darwin' ]] && alias tac='tail -r' |
| 55 | trap 'rm -f "$http_data" "$tmp_file"' EXIT | 54 | trap 'rm -f "$http_data" "$tmp_file"' EXIT |
| 56 | 55 | ||
| 57 | # This function determines which http get tool the system has installed and returns an error if there isnt one | 56 | # This function determines which http get tool the system has installed and returns an error if there isnt one |
| @@ -115,11 +114,11 @@ update() { | |||
| 115 | echo "Error: no active internet connection" >&2 | 114 | echo "Error: no active internet connection" >&2 |
| 116 | exit 1 | 115 | exit 1 |
| 117 | else | 116 | else |
| 118 | if [[ "$latestVersion" != "$currentVersion" ]]; then | 117 | if [[ $latestVersion != $currentVersion ]]; then |
| 119 | echo "Version $latestVersion available" | 118 | echo "Version $latestVersion available" |
| 120 | echo -n "Do you wish to update $repositoryName [Y/n]: " | 119 | echo -n "Do you wish to update $repositoryName [Y/n]: " |
| 121 | read -r answer | 120 | read -r answer |
| 122 | if [[ "$answer" == [Yy] ]]; then | 121 | if [[ $answer == [Yy] ]]; then |
| 123 | cd ~ || { echo 'Update Failed'; exit 1; } | 122 | cd ~ || { echo 'Update Failed'; exit 1; } |
| 124 | if [[ -d ~/$repositoryName ]]; then rm -r -f $repositoryName || { echo "Permissions Error: try running the update as sudo"; exit 1; } ; fi | 123 | if [[ -d ~/$repositoryName ]]; then rm -r -f $repositoryName || { echo "Permissions Error: try running the update as sudo"; exit 1; } ; fi |
| 125 | echo -n "Downloading latest version of: $repositoryName." | 124 | echo -n "Downloading latest version of: $repositoryName." |
| @@ -144,7 +143,7 @@ update() { | |||
| 144 | 143 | ||
| 145 | # handle configuration cases | 144 | # handle configuration cases |
| 146 | _configure() { | 145 | _configure() { |
| 147 | [[ -z "$@" ]] && (${EDITOR:-vi} $CONFIG) && return 0 | 146 | [[ -z $@ ]] && (${EDITOR:-vi} $CONFIG) && return 0 |
| 148 | 147 | ||
| 149 | local target="" | 148 | local target="" |
| 150 | if [[ $1 =~ ^(user|token|folder|auto_sync|EDITOR|action)$ ]]; then | 149 | if [[ $1 =~ ^(user|token|folder|auto_sync|EDITOR|action)$ ]]; then |
| @@ -160,7 +159,7 @@ _configure() { | |||
| 160 | fi | 159 | fi |
| 161 | 160 | ||
| 162 | umask 0077 && touch $CONFIG | 161 | umask 0077 && touch $CONFIG |
| 163 | sed -i'' -e "/^$1=/ d" $CONFIG && [[ -n "$target" ]] && echo $target >> $CONFIG | 162 | sed -i'' -e "/^$1=/ d" $CONFIG && [[ -n $target ]] && echo $target >> $CONFIG |
| 164 | cat $CONFIG | 163 | cat $CONFIG |
| 165 | } | 164 | } |
| 166 | 165 | ||
| @@ -315,7 +314,7 @@ _fetch_gists() { | |||
| 315 | echo | 314 | echo |
| 316 | local route="users/$user/gists" | 315 | local route="users/$user/gists" |
| 317 | local filter='/^[^s]/ d; /^$/ d' | 316 | local filter='/^[^s]/ d; /^$/ d' |
| 318 | if [[ "$1" =~ ^(star|s)$ ]];then | 317 | if [[ $1 =~ ^(star|s)$ ]];then |
| 319 | route="gists/starred" | 318 | route="gists/starred" |
| 320 | local mark="s" | 319 | local mark="s" |
| 321 | filter='/^[s]/ d; /^$/ d' | 320 | filter='/^[s]/ d; /^$/ d' |
| @@ -369,9 +368,9 @@ _sync_repos() { | |||
| 369 | # get gist id from index files | 368 | # get gist id from index files |
| 370 | _gist_id() { | 369 | _gist_id() { |
| 371 | GIST_ID=$( (grep -hs '' $INDEX || true) | sed -n -e "/^$1 / p" | cut -d' ' -f2 | sed -E -e 's#.*/##') | 370 | GIST_ID=$( (grep -hs '' $INDEX || true) | sed -n -e "/^$1 / p" | cut -d' ' -f2 | sed -E -e 's#.*/##') |
| 372 | if [[ -z "$GIST_ID" ]]; then | 371 | if [[ -z $GIST_ID ]]; then |
| 373 | echo -e "Not a valid index: \e[31m$1\e[0m" | 372 | echo -e "Not a valid index: \e[31m$1\e[0m" |
| 374 | echo Use the index in the first column instead: | 373 | echo 'Use the index in the first column instead (like 1 or s1):' |
| 375 | echo | 374 | echo |
| 376 | _show_list | 375 | _show_list |
| 377 | return 1 | 376 | return 1 |
| @@ -400,7 +399,7 @@ _goto_gist() { | |||
| 400 | _delete_gist() { | 399 | _delete_gist() { |
| 401 | read -r -p "Delete gists above? [y/N] " response | 400 | read -r -p "Delete gists above? [y/N] " response |
| 402 | response=${response,,} | 401 | response=${response,,} |
| 403 | [[ ! "$response" =~ ^(yes|y)$ ]] && return 0 | 402 | [[ ! $response =~ ^(yes|y)$ ]] && return 0 |
| 404 | 403 | ||
| 405 | for i in "$@"; do | 404 | for i in "$@"; do |
| 406 | _gist_id "$i" | 405 | _gist_id "$i" |
| @@ -460,7 +459,7 @@ _show_detail() { | |||
| 460 | # set filename/description/permission for a new gist | 459 | # set filename/description/permission for a new gist |
| 461 | _set_gist() { | 460 | _set_gist() { |
| 462 | public=True | 461 | public=True |
| 463 | while [[ -n "$@" ]]; do case $1 in | 462 | while [[ -n $@ ]]; do case $1 in |
| 464 | -d | --desc) | 463 | -d | --desc) |
| 465 | description="$2" | 464 | description="$2" |
| 466 | shift; shift;; | 465 | shift; shift;; |
| @@ -484,7 +483,7 @@ _new_file() { | |||
| 484 | tmp_file=$(mktemp) | 483 | tmp_file=$(mktemp) |
| 485 | cat > $tmp_file | 484 | cat > $tmp_file |
| 486 | echo -e '\n' > /dev/tty | 485 | echo -e '\n' > /dev/tty |
| 487 | [[ -z "$1" ]] && read -p 'Type file name: ' filename < /dev/tty | 486 | [[ -z $1 ]] && read -p 'Type file name: ' filename < /dev/tty |
| 488 | mv $tmp_file /tmp/$filename | 487 | mv $tmp_file /tmp/$filename |
| 489 | echo /tmp/$filename | 488 | echo /tmp/$filename |
| 490 | } | 489 | } |
| @@ -505,8 +504,8 @@ print(json.dumps({'public': $public, 'files': files_json, 'description': descrip | |||
| 505 | # create a new gist with files | 504 | # create a new gist with files |
| 506 | _create_gist() { | 505 | _create_gist() { |
| 507 | _set_gist "$@" || return 1 | 506 | _set_gist "$@" || return 1 |
| 508 | [[ -z "$files" ]] && files=$(_new_file $filename) | 507 | [[ -z $files ]] && files=$(_new_file $filename) |
| 509 | [[ -z "$description" ]] && read -p 'Type description: ' description < /dev/tty | 508 | [[ -z $description ]] && read -p 'Type description: ' description < /dev/tty |
| 510 | 509 | ||
| 511 | echo 'Creating a new gist...' | 510 | echo 'Creating a new gist...' |
| 512 | http_data=$(mktemp) | 511 | http_data=$(mktemp) |
| @@ -548,12 +547,11 @@ usage() { | |||
| 548 | _apply_config "$@" || exit 1 | 547 | _apply_config "$@" || exit 1 |
| 549 | getConfiguredClient || exit 1 | 548 | getConfiguredClient || exit 1 |
| 550 | if [[ $init ]]; then _fetch_gists; exit 0; fi | 549 | if [[ $init ]]; then _fetch_gists; exit 0; fi |
| 550 | [[ -z $hint ]] && hint=true | ||
| 551 | case "$1" in | 551 | case "$1" in |
| 552 | "") | 552 | "") |
| 553 | [[ -z "$hint" ]] && hint=true | ||
| 554 | hint=$hint _show_list ;; | 553 | hint=$hint _show_list ;; |
| 555 | star | s) | 554 | star | s) |
| 556 | [[ -z "$hint" ]] && hint=true | ||
| 557 | hint=$hint mark=s _show_list ;; | 555 | hint=$hint mark=s _show_list ;; |
| 558 | fetch | f) | 556 | fetch | f) |
| 559 | _fetch_gists "$2" ;; | 557 | _fetch_gists "$2" ;; |