diff options
-rwxr-xr-x | gist | 44 |
1 files changed, 25 insertions, 19 deletions
@@ -21,7 +21,7 @@ | |||
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 | 23 | # grep, g <pattern> Grep gists by a given pattern |
24 | # push, p <pattern> Push changes by git (well, better to make commit by youself) | 24 | # push, p <index_of_gist> Push changes by git (well, better to make commit by youself) |
25 | # github, G <index_of_gist> Import selected gist as a new Github repo | 25 | # github, G <index_of_gist> Import selected gist as a new Github repo |
26 | # help, h Show this help message | 26 | # help, h Show this help message |
27 | # version Get the tool version | 27 | # version Get the tool version |
@@ -46,10 +46,11 @@ CONFIG=~/.config/gist.conf; mkdir -p ~/.config | |||
46 | folder=~/gist && mkdir -p $folder | 46 | folder=~/gist && mkdir -p $folder |
47 | action="${EDITOR:-vi} ." | 47 | action="${EDITOR:-vi} ." |
48 | auto_sync=true # automatically clone the gist repo | 48 | auto_sync=true # automatically clone the gist repo |
49 | [[ -z $hint ]] && hint=true # default to show hint with list of gist | ||
49 | 50 | ||
50 | # Shell configuration | 51 | # Shell configuration |
51 | set -o pipefail | 52 | set -o pipefail |
52 | [[ $TRACE ]] && set -x | 53 | [[ $TRACE == 'true' ]] && set -x |
53 | [[ $(uname) == 'Darwin' ]] && alias tac='tail -r' | 54 | [[ $(uname) == 'Darwin' ]] && alias tac='tail -r' |
54 | trap 'rm -f "$http_data" "$tmp_file"' EXIT | 55 | trap 'rm -f "$http_data" "$tmp_file"' EXIT |
55 | 56 | ||
@@ -68,6 +69,7 @@ getConfiguredClient() { | |||
68 | } | 69 | } |
69 | 70 | ||
70 | # Allows to call the users configured client without if statements everywhere | 71 | # Allows to call the users configured client without if statements everywhere |
72 | # TODO return false if code is not 20x | ||
71 | http_method() { | 73 | http_method() { |
72 | local METHOD=$1; shift | 74 | local METHOD=$1; shift |
73 | case "$configuredClient" in | 75 | case "$configuredClient" in |
@@ -145,8 +147,8 @@ update() { | |||
145 | _configure() { | 147 | _configure() { |
146 | [[ -z $@ ]] && (${EDITOR:-vi} $CONFIG) && return 0 | 148 | [[ -z $@ ]] && (${EDITOR:-vi} $CONFIG) && return 0 |
147 | 149 | ||
148 | local target="" | 150 | local valid_keys='user|token|folder|auto_sync|EDITOR|action' |
149 | if [[ $1 =~ ^(user|token|folder|auto_sync|EDITOR|action)$ ]]; then | 151 | if [[ $1 =~ ^($valid_keys)$ ]]; then |
150 | if [[ $1 == 'user' ]]; then | 152 | if [[ $1 == 'user' ]]; then |
151 | [[ -z $2 ]] && echo "Must specify username" >&2 && return 1 | 153 | [[ -z $2 ]] && echo "Must specify username" >&2 && return 1 |
152 | elif [[ $1 == 'token' ]]; then | 154 | elif [[ $1 == 'token' ]]; then |
@@ -155,11 +157,14 @@ _configure() { | |||
155 | elif [[ $1 == 'auto_sync' ]]; then | 157 | elif [[ $1 == 'auto_sync' ]]; then |
156 | [[ ! $2 =~ ^(true|false)$ ]] && return 1 | 158 | [[ ! $2 =~ ^(true|false)$ ]] && return 1 |
157 | fi | 159 | fi |
158 | target=$1=$2 | 160 | local key=$1 && shift && local target=$key=\'$@\' |
161 | else | ||
162 | echo "Not a valid key for configuration, use <$valid_keys> instead." | ||
163 | return 1 | ||
159 | fi | 164 | fi |
160 | 165 | ||
161 | umask 0077 && touch $CONFIG | 166 | umask 0077 && touch $CONFIG |
162 | sed -i'' -e "/^$1=/ d" $CONFIG && [[ -n $target ]] && echo $target >> $CONFIG | 167 | sed -i'' -e "/^$key=/ d" $CONFIG && [[ -n $target ]] && echo $target >> $CONFIG |
163 | cat $CONFIG | 168 | cat $CONFIG |
164 | } | 169 | } |
165 | 170 | ||
@@ -172,8 +177,8 @@ _ask_username() { | |||
172 | _configure user $user | 177 | _configure user $user |
173 | } | 178 | } |
174 | 179 | ||
175 | # prompt for toekn | 180 | # prompt for token |
176 | # TODO token check, ref: https://developer.github.com/v3/apps/oauth_applications/#check-a-token | 181 | # TODO check token scope contains gist, ref: https://developer.github.com/v3/apps/oauth_applications/#check-a-token |
177 | _ask_token() { | 182 | _ask_token() { |
178 | echo -n "Create a new token from web browser? [Y/n] " | 183 | echo -n "Create a new token from web browser? [Y/n] " |
179 | read answer < /dev/tty | 184 | read answer < /dev/tty |
@@ -220,7 +225,7 @@ _apply_config() { | |||
220 | 225 | ||
221 | _check_repo_status() { | 226 | _check_repo_status() { |
222 | if [[ ! -d $1 ]]; then | 227 | if [[ ! -d $1 ]]; then |
223 | if $auto_sync; then | 228 | if [[ $auto_sync == 'true' ]]; then |
224 | echo "\e[32m[cloning]\e[0m"; | 229 | echo "\e[32m[cloning]\e[0m"; |
225 | else | 230 | else |
226 | echo "\e[32m[Not cloned yet]\e[0m"; | 231 | echo "\e[32m[Not cloned yet]\e[0m"; |
@@ -244,7 +249,7 @@ _show_list() { | |||
244 | return 0 | 249 | return 0 |
245 | fi | 250 | fi |
246 | local filter='/^ *s/ d; /^$/ d' | 251 | local filter='/^ *s/ d; /^$/ d' |
247 | [[ $mark == "s" ]] && filter='/^ *[^ s]/ d; /^$/ d' | 252 | [[ $mark == 's' ]] && filter='/^ *[^ s]/ d; /^$/ d' |
248 | 253 | ||
249 | sed -e "$filter" $INDEX \ | 254 | sed -e "$filter" $INDEX \ |
250 | | while read index link blob_code file_num comment_num author description; do | 255 | | while read index link blob_code file_num comment_num author description; do |
@@ -257,7 +262,7 @@ _show_list() { | |||
257 | | cut -c -$(tput cols) | 262 | | cut -c -$(tput cols) |
258 | done | 263 | done |
259 | 264 | ||
260 | $hint && echo -e '\nrun "gist fetch" to update gists or "gist help" for more details' > /dev/tty \ | 265 | [[ $hint == 'true' ]] && echo -e '\nrun "gist fetch" to update gists or "gist help" for more details' > /dev/tty \ |
261 | || return 0 | 266 | || return 0 |
262 | } | 267 | } |
263 | 268 | ||
@@ -266,9 +271,10 @@ _grep_content() { | |||
266 | _show_list | grep -i $1 | 271 | _show_list | grep -i $1 |
267 | } | 272 | } |
268 | 273 | ||
274 | # Open Github repository import page | ||
269 | _import_to_github() { | 275 | _import_to_github() { |
270 | _gist_id $1 | 276 | _gist_id $1 |
271 | echo put the folowing URL into webpage: | 277 | echo put the folowing URL into web page: |
272 | echo -n git@github.com:$GIST_ID.git | 278 | echo -n git@github.com:$GIST_ID.git |
273 | python -mwebbrowser https://github.com/new/import | 279 | python -mwebbrowser https://github.com/new/import |
274 | } | 280 | } |
@@ -324,9 +330,9 @@ _fetch_gists() { | |||
324 | [[ -z $result ]] && echo Failed to update gists && return 1 | 330 | [[ -z $result ]] && echo Failed to update gists && return 1 |
325 | 331 | ||
326 | sed -i'' -e "$filter" $INDEX && echo "$result" >> $INDEX | 332 | sed -i'' -e "$filter" $INDEX && echo "$result" >> $INDEX |
327 | mark=$mark hint=true _show_list | 333 | mark=$mark _show_list |
328 | 334 | ||
329 | $auto_sync && (_sync_repos $1 > /dev/null 2>&1 &) | 335 | [[ $auto_sync == 'true' ]] && (_sync_repos $1 > /dev/null 2>&1 &) |
330 | } | 336 | } |
331 | 337 | ||
332 | _query_user() { | 338 | _query_user() { |
@@ -521,13 +527,14 @@ _create_gist() { | |||
521 | 527 | ||
522 | if [[ $? -eq 0 ]]; then | 528 | if [[ $? -eq 0 ]]; then |
523 | echo 'Gist is created' | 529 | echo 'Gist is created' |
524 | _show_list | tail -1 | 530 | hint=false _show_list | tail -1 |
525 | else | 531 | else |
526 | echo 'Failed to create gist' | 532 | echo 'Failed to create gist' |
527 | fi | 533 | fi |
528 | } | 534 | } |
529 | 535 | ||
530 | # update description of a gist | 536 | # update description of a gist |
537 | # TODO use response to modify index file, do not fetch gists again | ||
531 | _edit_gist() { | 538 | _edit_gist() { |
532 | _gist_id $1 | 539 | _gist_id $1 |
533 | 540 | ||
@@ -537,7 +544,7 @@ _edit_gist() { | |||
537 | http_data=$(mktemp) | 544 | http_data=$(mktemp) |
538 | echo { \"description\": \"$(echo $DESC | sed -e 's/"/\\"/g')\" } > $http_data | 545 | echo { \"description\": \"$(echo $DESC | sed -e 's/"/\\"/g')\" } > $http_data |
539 | http_method PATCH $http_data $GITHUB_API/gists/$GIST_ID > /dev/null \ | 546 | http_method PATCH $http_data $GITHUB_API/gists/$GIST_ID > /dev/null \ |
540 | && _fetch_gists | 547 | && hint=false _fetch_gists | grep -E "^[ ]+$1" |
541 | } | 548 | } |
542 | 549 | ||
543 | usage() { | 550 | usage() { |
@@ -547,12 +554,11 @@ usage() { | |||
547 | _apply_config "$@" || exit 1 | 554 | _apply_config "$@" || exit 1 |
548 | getConfiguredClient || exit 1 | 555 | getConfiguredClient || exit 1 |
549 | if [[ $init ]]; then _fetch_gists; exit 0; fi | 556 | if [[ $init ]]; then _fetch_gists; exit 0; fi |
550 | [[ -z $hint ]] && hint=true | ||
551 | case "$1" in | 557 | case "$1" in |
552 | "") | 558 | "") |
553 | hint=$hint _show_list ;; | 559 | _show_list ;; |
554 | star | s) | 560 | star | s) |
555 | hint=$hint mark=s _show_list ;; | 561 | mark=s _show_list ;; |
556 | fetch | f) | 562 | fetch | f) |
557 | _fetch_gists "$2" ;; | 563 | _fetch_gists "$2" ;; |
558 | new | n) | 564 | new | n) |