diff options
author | typebrook <typebrook@gmail.com> | 2020-01-31 14:39:51 +0800 |
---|---|---|
committer | typebrook <typebrook@gmail.com> | 2020-01-31 14:39:51 +0800 |
commit | b15b3055c0d34c677b92c0ced2eca56ee5a4d055 (patch) | |
tree | 7a8b72250cd2b07358e9d2e85ab19e70d5e29fef | |
parent | fd2b6af2ea01461cdba60cd97aa9f1ee805fd157 (diff) |
update
-rwxr-xr-x | scripts/gist | 23 |
1 files changed, 15 insertions, 8 deletions
diff --git a/scripts/gist b/scripts/gist index 58c949f..65e5565 100755 --- a/scripts/gist +++ b/scripts/gist | |||
@@ -107,6 +107,7 @@ _ask_token() { | |||
107 | 107 | ||
108 | while [[ ! $token =~ ^[[:alnum:]]{40}$ ]]; do | 108 | while [[ ! $token =~ ^[[:alnum:]]{40}$ ]]; do |
109 | [[ ! -z $token ]] && echo "Not a valid token" | 109 | [[ ! -z $token ]] && echo "Not a valid token" |
110 | trap 'echo; return 1' INT | ||
110 | read -p "Paste your token here (Ctrl-C to skip): " token < /dev/tty | 111 | read -p "Paste your token here (Ctrl-C to skip): " token < /dev/tty |
111 | done | 112 | done |
112 | _configure token $token | 113 | _configure token $token |
@@ -121,13 +122,16 @@ _apply_config() { | |||
121 | _ask_username | 122 | _ask_username |
122 | _ask_token | 123 | _ask_token |
123 | elif [[ -z $token ]] && [[ $1 =~ ^(n|new|e|edit|D|delete)$ ]]; then | 124 | elif [[ -z $token ]] && [[ $1 =~ ^(n|new|e|edit|D|delete)$ ]]; then |
124 | _ask_token | 125 | if ! (_ask_token); then |
126 | echo 'To create/edit/delete a gist, a token is needed' | ||
127 | return 1 | ||
128 | fi | ||
125 | fi | 129 | fi |
126 | 130 | ||
127 | source $CONFIG | 131 | source $CONFIG |
128 | } | 132 | } |
129 | 133 | ||
130 | _apply_config "$@" | 134 | _apply_config "$@" || exit 1 |
131 | 135 | ||
132 | auth_header="Authorization: token $token" | 136 | auth_header="Authorization: token $token" |
133 | 137 | ||
@@ -277,10 +281,13 @@ _goto_gist() { | |||
277 | 281 | ||
278 | if [[ ! -d $folder/$GIST_ID ]]; then | 282 | if [[ ! -d $folder/$GIST_ID ]]; then |
279 | echo 'Cloning gist as repo...' | 283 | echo 'Cloning gist as repo...' |
280 | git clone git@github.com:$GIST_ID.git $folder/$GIST_ID \ | 284 | if (git clone git@github.com:$GIST_ID.git $folder/$GIST_ID); then |
281 | && echo 'Repo is cloned' \ | 285 | echo 'Repo is cloned' |
282 | || echo 'Failed to clone the gist' \ | 286 | return 0 |
283 | && return 1 | 287 | else |
288 | echo 'Failed to clone the gist' | ||
289 | return 1 | ||
290 | fi | ||
284 | fi | 291 | fi |
285 | 292 | ||
286 | echo This gist is at $folder/$GIST_ID | 293 | echo This gist is at $folder/$GIST_ID |
@@ -375,6 +382,7 @@ _new_file() { | |||
375 | } | 382 | } |
376 | 383 | ||
377 | # create a new gist with files | 384 | # create a new gist with files |
385 | # FIXME incorrect index | ||
378 | # FIXME catch status code from curl if it fails | 386 | # FIXME catch status code from curl if it fails |
379 | _create_gist() { | 387 | _create_gist() { |
380 | _set_gist "$@" || return 1 | 388 | _set_gist "$@" || return 1 |
@@ -387,8 +395,7 @@ _create_gist() { | |||
387 | | echo "{ \"public\": $public, \"files\": $(cat -), \"description\": \"$description\"}" \ | 395 | | echo "{ \"public\": $public, \"files\": $(cat -), \"description\": \"$description\"}" \ |
388 | | curl -s -H "$auth_header" --data @- $GITHUB_API/gists \ | 396 | | curl -s -H "$auth_header" --data @- $GITHUB_API/gists \ |
389 | | sed '1 s/^/[/; $ s/$/]/' \ | 397 | | sed '1 s/^/[/; $ s/$/]/' \ |
390 | | _parse_response \ | 398 | | _parse_response >> $INDEX \ |
391 | | sed -E "s/^/$(( $(wc -l $INDEX | cut -d' ' -f1) + 1 )) /" >> $INDEX \ | ||
392 | && echo -e '\nGist created' \ | 399 | && echo -e '\nGist created' \ |
393 | || echo 'Fail to create gist' | 400 | || echo 'Fail to create gist' |
394 | 401 | ||