From 3c0f9beb2232314d70f52902f3e72c422a3e065f Mon Sep 17 00:00:00 2001 From: typebrook Date: Wed, 5 Feb 2020 14:57:27 +0800 Subject: update --- gist | 65 +++++++++++++++++++++++++++++++++-------------------------------- 1 file changed, 33 insertions(+), 32 deletions(-) diff --git a/gist b/gist index e9e5130..f7933e1 100755 --- a/gist +++ b/gist @@ -11,7 +11,7 @@ # [star | s] list your gists with format below, star for your starred gists: # [index_of_gist] [url] [file_num] [comment_num] [short description] # update, u [star | s] update the local list of your gists, star for your starred gists -# show the path of local gist repo and do custom actions +# [--no-action] show the path of local gist repo and do custom actions # new, n [-d | --desc ] ... create a new gist with files # new, n [-d | --desc ] [-f | --file ] create a new gist from STDIN # detail, d show the detail of a gist @@ -31,15 +31,14 @@ # TODO grep mode for description, file content # TODO push github.com (may need new token) -# TODO description for current directory # TODO unit test -# TODO test on mac and remote machine +# TODO test on bats, mac and remote machine # TODO completion # Shell configuration set -o pipefail [ "$TRACE" ] && set -x -trap 'rm -f "$http_data" "tmp_file"' EXIT +trap 'rm -f "$http_data" "$tmp_file"' EXIT GITHUB_API=https://api.github.com CONFIG=~/.config/gist.conf; mkdir -p ~/.config @@ -47,23 +46,21 @@ configuredClient="" # handle configuration cases _configure() { - local target="" [[ -z "$@" ]] && (${EDITOR:-vi} $CONFIG) && return 0 - if [[ $1 == 'token' ]]; then - [[ ${#2} -eq 40 ]] && target=$1=$2 \ - || echo -e Invalid token format, it is not 40 chars '\n' > /dev/tty + + local target="" + if [[ $1 == 'user' ]]; then + [[ -z $2 ]] && echo "Must specify username" >&2 && return 1 + elif [[ $1 == 'token' ]]; then + [[ ${#2} -ne 40 ]] && echo 'Invalid token format, it is not 40 chars' >&2 \ + && return 1 elif [[ $1 == 'auto_sync' ]]; then - [[ $2 == 'false' ]] && target=$1=$2 \ - || target=$1=true - elif [[ $1 == 'folder' ]]; then - [[ -n "$2" ]] && target=$1=$2 \ - || target=$1=~/gist - elif [[ $1 == 'user' ]]; then - target=$1=$2 + [[ ! $2 =~ ^(true|false)$ ]] && return 1 fi + target=$1=$2 umask 0077 && touch $CONFIG - [[ "$target" =~ [^=]$ ]] && sed -i "/^$1=/ d" $CONFIG && echo $target >> $CONFIG + sed -i "/^$1=/ d" $CONFIG && [[ "$target" =~ [^=]$ ]] && echo $target >> $CONFIG cat $CONFIG } @@ -93,14 +90,14 @@ _ask_token() { _configure token $token } +# check configuration is fine with user setting _validate_config(){ source $CONFIG 2> /dev/null || true if [[ ! -e $CONFIG || -z $user ]]; then echo 'Hi fellow! To access your gists, I need your Github username' echo "Also a personal token with scope which allows "gist"!'" echo - _ask_username - _ask_token + _ask_username && _ask_token && init=true elif [[ -z $token && $1 =~ ^(n|new|e|edit|D|delete)$ ]]; then if ! (_ask_token); then echo 'To create/edit/delete a gist, a token is needed' @@ -161,19 +158,17 @@ http_method() { } # Show the list of gist, but not updated time -# TODO a way to show files -# TODO show git status outdated _show_list() { if [[ ! -e $INDEX ]]; then echo 'No local file found for last update, please run command:' echo ' gist update' return 0 fi - local filter='/^s/ d; /^$/ d' - [[ $1 == "s" ]] && filter='/^[^s]/ d; /^$/ d' + local filter='/^ *s/ d; /^$/ d' + [[ $1 == "s" ]] && filter='/^ *[^ s]/ d; /^$/ d' while read index link blob_code file_num extra author description; do - [[ $1 == "s" ]] && local author=$author + [[ $1 == "s" ]] && local name=$author local repo=$folder/$(echo $link | sed 's#.*/##') local occupy=0 @@ -184,10 +179,10 @@ _show_list() { # if there is a commit not yet push, show red message "ahead" [[ -n $(cd $repo && git cherry) ]] 2>/dev/null && extra="\e[31m[ahead]\e[0m" && occupy=7 - echo -e $index $link $author $file_num $extra $(echo $description | cut -c -$(( 60 -$occupy -1 )) ) + echo -e "$(printf "% 3s" $index)" $link $name $file_num $extra $(echo $description | cut -c -$(( 60 -$occupy -1 )) ) done < $INDEX \ | sed "$filter" - echo -e '\nrun "gist help" for more details' + echo -e '\nrun "gist help" for more details' > /dev/tty } # parse JSON from STDIN with string of commands @@ -225,7 +220,7 @@ _parse_response() { done } -# TODO add author, files and date of a gist +# TODO add files and date of a gist # get latest list of gists from Github API _update() { echo "fetching $user's gists from $GITHUB_API..." @@ -306,7 +301,7 @@ _goto_gist() { fi fi - (cd $folder/$GIST_ID && eval "$action") + [[ $2 != '--no-action' ]] && cd $folder/$GIST_ID && eval "$action" echo $folder/$GIST_ID } @@ -425,7 +420,10 @@ _create_gist() { | AccessJsonElement "$(_gist_body)" > $http_data \ && http_method POST $GITHUB_API/gists \ | sed '1 s/^/[/; $ s/$/]/' \ - | _parse_response $(( $(sed '/^s/ d' $INDEX | wc -l) +1 )) >> $INDEX + | _parse_response $(( $(sed '/^s/ d' $INDEX | wc -l) +1 )) \ + | tee -a $INDEX \ + | cut -d' ' -f2 | sed -E 's#.*/##' \ + | (xargs -I{} git clone git@github.com:{}.git $folder/{} &> /dev/null &) if [[ $? -eq 0 ]]; then echo 'Gist is created' @@ -441,16 +439,19 @@ _edit_gist() { echo -n 'Type new description: ' read DESC < /dev/tty - echo "{ \"description\": \"$DESC\" }" \ - | http_method PATCH $GITHUB_API/gists/$GIST_ID > /dev/null \ + + http_data=$(mktemp) + echo "{ \"description\": \"$DESC\" }" > $http_data + http_method PATCH $http_data $GITHUB_API/gists/$GIST_ID > /dev/null \ && _update } usage() { - sed -E -n ' /^$/ q; 7,$ s/^#//p' $0 + sed -E -n ' /^$/ q; 7,$ s/^# //p' $0 } getConfiguredClient +if [[ $init ]]; then _update; exit 0; fi case "$1" in "") _show_list ;; @@ -482,5 +483,5 @@ case "$1" in help | h) usage ;; *) - _goto_gist "$1" ;; + _goto_gist "$@" ;; esac -- cgit v1.2.3-70-g09d2