From cf18a16d5237c10a104fe48c3f30f5c89f67e88f Mon Sep 17 00:00:00 2001 From: typebrook Date: Thu, 13 Feb 2020 15:27:05 +0800 Subject: update --- gist | 44 +++++++++++++++++++++++++------------------- 1 file changed, 25 insertions(+), 19 deletions(-) diff --git a/gist b/gist index 07dc725..acb0066 100755 --- a/gist +++ b/gist @@ -21,7 +21,7 @@ # config, c [token | user | folder | auto_sync | EDITOR | action [value] ] Do configuration # user, U Get gists from a given Github user # grep, g Grep gists by a given pattern -# push, p Push changes by git (well, better to make commit by youself) +# push, p Push changes by git (well, better to make commit by youself) # github, G Import selected gist as a new Github repo # help, h Show this help message # version Get the tool version @@ -46,10 +46,11 @@ CONFIG=~/.config/gist.conf; mkdir -p ~/.config folder=~/gist && mkdir -p $folder action="${EDITOR:-vi} ." auto_sync=true # automatically clone the gist repo +[[ -z $hint ]] && hint=true # default to show hint with list of gist # Shell configuration set -o pipefail -[[ $TRACE ]] && set -x +[[ $TRACE == 'true' ]] && set -x [[ $(uname) == 'Darwin' ]] && alias tac='tail -r' trap 'rm -f "$http_data" "$tmp_file"' EXIT @@ -68,6 +69,7 @@ getConfiguredClient() { } # Allows to call the users configured client without if statements everywhere +# TODO return false if code is not 20x http_method() { local METHOD=$1; shift case "$configuredClient" in @@ -145,8 +147,8 @@ update() { _configure() { [[ -z $@ ]] && (${EDITOR:-vi} $CONFIG) && return 0 - local target="" - if [[ $1 =~ ^(user|token|folder|auto_sync|EDITOR|action)$ ]]; then + local valid_keys='user|token|folder|auto_sync|EDITOR|action' + if [[ $1 =~ ^($valid_keys)$ ]]; then if [[ $1 == 'user' ]]; then [[ -z $2 ]] && echo "Must specify username" >&2 && return 1 elif [[ $1 == 'token' ]]; then @@ -155,11 +157,14 @@ _configure() { elif [[ $1 == 'auto_sync' ]]; then [[ ! $2 =~ ^(true|false)$ ]] && return 1 fi - target=$1=$2 + local key=$1 && shift && local target=$key=\'$@\' + else + echo "Not a valid key for configuration, use <$valid_keys> instead." + return 1 fi umask 0077 && touch $CONFIG - sed -i'' -e "/^$1=/ d" $CONFIG && [[ -n $target ]] && echo $target >> $CONFIG + sed -i'' -e "/^$key=/ d" $CONFIG && [[ -n $target ]] && echo $target >> $CONFIG cat $CONFIG } @@ -172,8 +177,8 @@ _ask_username() { _configure user $user } -# prompt for toekn -# TODO token check, ref: https://developer.github.com/v3/apps/oauth_applications/#check-a-token +# prompt for token +# TODO check token scope contains gist, ref: https://developer.github.com/v3/apps/oauth_applications/#check-a-token _ask_token() { echo -n "Create a new token from web browser? [Y/n] " read answer < /dev/tty @@ -220,7 +225,7 @@ _apply_config() { _check_repo_status() { if [[ ! -d $1 ]]; then - if $auto_sync; then + if [[ $auto_sync == 'true' ]]; then echo "\e[32m[cloning]\e[0m"; else echo "\e[32m[Not cloned yet]\e[0m"; @@ -244,7 +249,7 @@ _show_list() { return 0 fi local filter='/^ *s/ d; /^$/ d' - [[ $mark == "s" ]] && filter='/^ *[^ s]/ d; /^$/ d' + [[ $mark == 's' ]] && filter='/^ *[^ s]/ d; /^$/ d' sed -e "$filter" $INDEX \ | while read index link blob_code file_num comment_num author description; do @@ -257,7 +262,7 @@ _show_list() { | cut -c -$(tput cols) done - $hint && echo -e '\nrun "gist fetch" to update gists or "gist help" for more details' > /dev/tty \ + [[ $hint == 'true' ]] && echo -e '\nrun "gist fetch" to update gists or "gist help" for more details' > /dev/tty \ || return 0 } @@ -266,9 +271,10 @@ _grep_content() { _show_list | grep -i $1 } +# Open Github repository import page _import_to_github() { _gist_id $1 - echo put the folowing URL into webpage: + echo put the folowing URL into web page: echo -n git@github.com:$GIST_ID.git python -mwebbrowser https://github.com/new/import } @@ -324,9 +330,9 @@ _fetch_gists() { [[ -z $result ]] && echo Failed to update gists && return 1 sed -i'' -e "$filter" $INDEX && echo "$result" >> $INDEX - mark=$mark hint=true _show_list + mark=$mark _show_list - $auto_sync && (_sync_repos $1 > /dev/null 2>&1 &) + [[ $auto_sync == 'true' ]] && (_sync_repos $1 > /dev/null 2>&1 &) } _query_user() { @@ -521,13 +527,14 @@ _create_gist() { if [[ $? -eq 0 ]]; then echo 'Gist is created' - _show_list | tail -1 + hint=false _show_list | tail -1 else echo 'Failed to create gist' fi } # update description of a gist +# TODO use response to modify index file, do not fetch gists again _edit_gist() { _gist_id $1 @@ -537,7 +544,7 @@ _edit_gist() { http_data=$(mktemp) echo { \"description\": \"$(echo $DESC | sed -e 's/"/\\"/g')\" } > $http_data http_method PATCH $http_data $GITHUB_API/gists/$GIST_ID > /dev/null \ - && _fetch_gists + && hint=false _fetch_gists | grep -E "^[ ]+$1" } usage() { @@ -547,12 +554,11 @@ usage() { _apply_config "$@" || exit 1 getConfiguredClient || exit 1 if [[ $init ]]; then _fetch_gists; exit 0; fi -[[ -z $hint ]] && hint=true case "$1" in "") - hint=$hint _show_list ;; + _show_list ;; star | s) - hint=$hint mark=s _show_list ;; + mark=s _show_list ;; fetch | f) _fetch_gists "$2" ;; new | n) -- cgit v1.2.3-70-g09d2