diff options
author | typebrook <typebrook@gmail.com> | 2020-01-31 17:40:47 +0800 |
---|---|---|
committer | typebrook <typebrook@gmail.com> | 2020-01-31 17:40:47 +0800 |
commit | 2d0d547a4e817e2cf74f65b103f032e760b496fa (patch) | |
tree | 432f81ee3e8754ddbe6dc7f8e0558b5b703b8f00 /scripts | |
parent | 9aaa20ef32e03a3dcc98a083d2df8018bc379684 (diff) |
update
Diffstat (limited to 'scripts')
-rwxr-xr-x | scripts/gist | 29 |
1 files changed, 17 insertions, 12 deletions
diff --git a/scripts/gist b/scripts/gist index ea993e9..457b07e 100755 --- a/scripts/gist +++ b/scripts/gist | |||
@@ -66,30 +66,28 @@ GITHUB_API=https://api.github.com | |||
66 | CONFIG=~/.config/gist.conf; mkdir -p ~/.config | 66 | CONFIG=~/.config/gist.conf; mkdir -p ~/.config |
67 | configuredClient="" | 67 | configuredClient="" |
68 | 68 | ||
69 | _config_cases() { | 69 | # handle configuration cases |
70 | _configure() { | ||
71 | [[ -z "$@" ]] && (${EDITOR:-vi} $CONFIG) && return 0 | ||
70 | if [[ $1 == 'token' ]]; then | 72 | if [[ $1 == 'token' ]]; then |
71 | [[ ${#2} -eq 40 ]] && echo $1=$2 \ | 73 | [[ ${#2} -eq 40 ]] && target=$1=$2 \ |
72 | || echo -e Invalid token format, it is not 40 chars '\n' > /dev/tty | 74 | || echo -e Invalid token format, it is not 40 chars '\n' > /dev/tty |
73 | elif [[ $1 == 'auto_sync' ]]; then | 75 | elif [[ $1 == 'auto_sync' ]]; then |
74 | [[ $2 == 'false' ]] && echo $1=$2 \ | 76 | [[ $2 == 'false' ]] && target=$1=$2 \ |
75 | || echo $1=true | 77 | || target=$1=true |
76 | elif [[ $1 == 'folder' ]]; then | 78 | elif [[ $1 == 'folder' ]]; then |
77 | [[ -n "$2" ]] && echo $1=$2 \ | 79 | [[ -n "$2" ]] && target=$1=$2 \ |
78 | || echo $1=~/gist | 80 | || target=$1=~/gist |
79 | elif [[ $1 == 'user' ]]; then | 81 | elif [[ $1 == 'user' ]]; then |
80 | echo $1=$2 | 82 | target=$1=$2 |
81 | fi | 83 | fi |
82 | } | ||
83 | |||
84 | _configure() { | ||
85 | [[ -z "$@" ]] && (${EDITOR:-vi} $CONFIG) && return 0 | ||
86 | target=$(_config_cases "$@") | ||
87 | 84 | ||
88 | umask 0077 && touch $CONFIG | 85 | umask 0077 && touch $CONFIG |
89 | [[ "$target" =~ [^=]$ ]] && sed -i "/^$1=/ d" $CONFIG && echo $target >> $CONFIG | 86 | [[ "$target" =~ [^=]$ ]] && sed -i "/^$1=/ d" $CONFIG && echo $target >> $CONFIG |
90 | cat $CONFIG | 87 | cat $CONFIG |
91 | } | 88 | } |
92 | 89 | ||
90 | # prompt for username | ||
93 | _ask_username() { | 91 | _ask_username() { |
94 | while [[ ! $user =~ ^[[:alnum:]]+$ ]]; do | 92 | while [[ ! $user =~ ^[[:alnum:]]+$ ]]; do |
95 | [[ -n $user ]] && echo "Not a valid username" | 93 | [[ -n $user ]] && echo "Not a valid username" |
@@ -98,6 +96,7 @@ _ask_username() { | |||
98 | _configure user $user | 96 | _configure user $user |
99 | } | 97 | } |
100 | 98 | ||
99 | # prompt for toekn | ||
101 | # TODO token check, ref: https://developer.github.com/v3/apps/oauth_applications/#check-a-token | 100 | # TODO token check, ref: https://developer.github.com/v3/apps/oauth_applications/#check-a-token |
102 | _ask_token() { | 101 | _ask_token() { |
103 | echo -n "Create a new token from web browser? [Y/n] " | 102 | echo -n "Create a new token from web browser? [Y/n] " |
@@ -114,6 +113,7 @@ _ask_token() { | |||
114 | _configure token $token | 113 | _configure token $token |
115 | } | 114 | } |
116 | 115 | ||
116 | # load configuration | ||
117 | _apply_config() { | 117 | _apply_config() { |
118 | source $CONFIG 2> /dev/null || true | 118 | source $CONFIG 2> /dev/null || true |
119 | if [[ ! -e $CONFIG || -z $user ]]; then | 119 | if [[ ! -e $CONFIG || -z $user ]]; then |
@@ -224,6 +224,7 @@ for gist in raw: | |||
224 | } | 224 | } |
225 | 225 | ||
226 | # TODO check if a user create a very first gist | 226 | # TODO check if a user create a very first gist |
227 | # parse response from gists require | ||
227 | _parse_response() { | 228 | _parse_response() { |
228 | AccessJsonElement "$(_handle_gists)" \ | 229 | AccessJsonElement "$(_handle_gists)" \ |
229 | | tac | sed 's/, /,/g' | nl -s' ' \ | 230 | | tac | sed 's/, /,/g' | nl -s' ' \ |
@@ -257,6 +258,7 @@ _update() { | |||
257 | if [[ $auto_sync != "false" ]]; then (_sync_repos $1 > /dev/null 2>&1 &); fi | 258 | if [[ $auto_sync != "false" ]]; then (_sync_repos $1 > /dev/null 2>&1 &); fi |
258 | } | 259 | } |
259 | 260 | ||
261 | # update local git repos | ||
260 | _sync_repos() { | 262 | _sync_repos() { |
261 | # clone repos which are not in the local | 263 | # clone repos which are not in the local |
262 | comm -13 <(find $folder -maxdepth 1 -type d | sed '1d; s#.*/##' | sort) \ | 264 | comm -13 <(find $folder -maxdepth 1 -type d | sed '1d; s#.*/##' | sort) \ |
@@ -328,6 +330,7 @@ _clean_repos() { | |||
328 | done | 330 | done |
329 | } | 331 | } |
330 | 332 | ||
333 | # parse JSON from gist detail | ||
331 | _handle_gist() { | 334 | _handle_gist() { |
332 | echo ' | 335 | echo ' |
333 | print("site:", raw["html_url"]) | 336 | print("site:", raw["html_url"]) |
@@ -364,6 +367,7 @@ _show_detail() { | |||
364 | | AccessJsonElement "$(_handle_comment)" | 367 | | AccessJsonElement "$(_handle_comment)" |
365 | } | 368 | } |
366 | 369 | ||
370 | # set filename/description/permission for a new gist | ||
367 | _set_gist() { | 371 | _set_gist() { |
368 | public=true | 372 | public=true |
369 | while [[ -n "$@" ]]; do case $1 in | 373 | while [[ -n "$@" ]]; do case $1 in |
@@ -384,6 +388,7 @@ _set_gist() { | |||
384 | ls $files > /dev/null || return 1 | 388 | ls $files > /dev/null || return 1 |
385 | } | 389 | } |
386 | 390 | ||
391 | # Let user type the content of gist before setting filename | ||
387 | _new_file() { | 392 | _new_file() { |
388 | [[ -t 0 ]] && echo "Type a gist. <Ctrl-C> to cancel, <Ctrl-D> when done" > /dev/tty | 393 | [[ -t 0 ]] && echo "Type a gist. <Ctrl-C> to cancel, <Ctrl-D> when done" > /dev/tty |
389 | tmp_file=$(mktemp) | 394 | tmp_file=$(mktemp) |