diff options
author | typebrook <typebrook@gmail.com> | 2020-02-01 00:30:24 +0800 |
---|---|---|
committer | typebrook <typebrook@gmail.com> | 2020-02-01 00:30:24 +0800 |
commit | 52aa4ebde1151601ae8a35774ca289c5b6d798b8 (patch) | |
tree | 244f62b0aff0bd04ea462236d495590f2ac12c75 | |
parent | caa69cba3d90a818545ab060372dfae492e14825 (diff) |
update
-rwxr-xr-x | scripts/gist | 20 |
1 files changed, 11 insertions, 9 deletions
diff --git a/scripts/gist b/scripts/gist index a939a47..762bdfb 100755 --- a/scripts/gist +++ b/scripts/gist | |||
@@ -64,6 +64,7 @@ configuredClient="" | |||
64 | 64 | ||
65 | # handle configuration cases | 65 | # handle configuration cases |
66 | _configure() { | 66 | _configure() { |
67 | local target="" | ||
67 | [[ -z "$@" ]] && (${EDITOR:-vi} $CONFIG) && return 0 | 68 | [[ -z "$@" ]] && (${EDITOR:-vi} $CONFIG) && return 0 |
68 | if [[ $1 == 'token' ]]; then | 69 | if [[ $1 == 'token' ]]; then |
69 | [[ ${#2} -eq 40 ]] && target=$1=$2 \ | 70 | [[ ${#2} -eq 40 ]] && target=$1=$2 \ |
@@ -135,7 +136,7 @@ _apply_config() { | |||
135 | 136 | ||
136 | _apply_config "$@" || exit 1 | 137 | _apply_config "$@" || exit 1 |
137 | 138 | ||
138 | auth_header="Authorization: token $token" | 139 | AUTH_HEADER="Authorization: token $token" |
139 | 140 | ||
140 | [[ -z "$folder" ]] && folder=~/gist && mkdir -p $folder | 141 | [[ -z "$folder" ]] && folder=~/gist && mkdir -p $folder |
141 | INDEX=$folder/index | 142 | INDEX=$folder/index |
@@ -177,6 +178,7 @@ _show_list() { | |||
177 | echo ' gist update' | 178 | echo ' gist update' |
178 | return 0 | 179 | return 0 |
179 | fi | 180 | fi |
181 | local filter="" | ||
180 | if [[ $1 == "s" ]]; then | 182 | if [[ $1 == "s" ]]; then |
181 | filter='/^[^s]/ d' | 183 | filter='/^[^s]/ d' |
182 | else | 184 | else |
@@ -226,8 +228,8 @@ _parse_response() { | |||
226 | | tac | sed 's/, /,/g' | nl -s' ' \ | 228 | | tac | sed 's/, /,/g' | nl -s' ' \ |
227 | | while read index link file_url_array public file_num comment_num description; do | 229 | | while read index link file_url_array public file_num comment_num description; do |
228 | local blob_code=$(echo $file_url_array | tr ',' '\n' | sed -E 's#.*raw/(.*)/.*#\1#' | sort | cut -c -7 | paste -sd '-') | 230 | local blob_code=$(echo $file_url_array | tr ',' '\n' | sed -E 's#.*raw/(.*)/.*#\1#' | sort | cut -c -7 | paste -sd '-') |
229 | [[ $public == 'False' ]] && mark=p | 231 | [[ $public == 'False' ]] && local mark=p |
230 | [[ -n $1 ]] && index=$1 | 232 | [[ -n $1 ]] && local index=$1 |
231 | echo $mark$index $link $blob_code $file_num $comment_num $description | tr -d '"' | 233 | echo $mark$index $link $blob_code $file_num $comment_num $description | tr -d '"' |
232 | done | 234 | done |
233 | } | 235 | } |
@@ -245,7 +247,7 @@ _update() { | |||
245 | filter='/^[s]/ d' | 247 | filter='/^[s]/ d' |
246 | fi | 248 | fi |
247 | 249 | ||
248 | response=$(curl -H "$auth_header" $GITHUB_API/$route) | 250 | local response=$(curl -s -H "$AUTH_HEADER" $GITHUB_API/$route) |
249 | false && echo Failed to update gists && return 1 | 251 | false && echo Failed to update gists && return 1 |
250 | sed -i "$filter" $INDEX | 252 | sed -i "$filter" $INDEX |
251 | echo $response | _parse_response >> $INDEX | 253 | echo $response | _parse_response >> $INDEX |
@@ -307,7 +309,7 @@ _goto_gist() { | |||
307 | _delete_gist() { | 309 | _delete_gist() { |
308 | for i in "$@"; do | 310 | for i in "$@"; do |
309 | _gist_id "$i" | 311 | _gist_id "$i" |
310 | curl -X DELETE -s -H "$auth_header" $GITHUB_API/gists/$GIST_ID \ | 312 | curl -X DELETE -s -H "$AUTH_HEADER" $GITHUB_API/gists/$GIST_ID \ |
311 | && echo "$i" deleted \ | 313 | && echo "$i" deleted \ |
312 | && sed -i -E "/^$i / d" $INDEX | 314 | && sed -i -E "/^$i / d" $INDEX |
313 | done | 315 | done |
@@ -383,7 +385,7 @@ _set_gist() { | |||
383 | # Let user type the content of gist before setting filename | 385 | # Let user type the content of gist before setting filename |
384 | _new_file() { | 386 | _new_file() { |
385 | [[ -t 0 ]] && echo "Type a gist. <Ctrl-C> to cancel, <Ctrl-D> when done" > /dev/tty | 387 | [[ -t 0 ]] && echo "Type a gist. <Ctrl-C> to cancel, <Ctrl-D> when done" > /dev/tty |
386 | tmp_file=$(mktemp) | 388 | local tmp_file=$(mktemp) |
387 | cat > $tmp_file | 389 | cat > $tmp_file |
388 | echo -e '\n' > /dev/tty | 390 | echo -e '\n' > /dev/tty |
389 | [[ -z "$1" ]] && read -p 'Type file name: ' filename < /dev/tty | 391 | [[ -z "$1" ]] && read -p 'Type file name: ' filename < /dev/tty |
@@ -397,13 +399,13 @@ _create_gist() { | |||
397 | [[ -z "$files" ]] && files=$(_new_file $filename) | 399 | [[ -z "$files" ]] && files=$(_new_file $filename) |
398 | [[ -z "$description" ]] && read -p 'Type description: ' description < /dev/tty | 400 | [[ -z "$description" ]] && read -p 'Type description: ' description < /dev/tty |
399 | 401 | ||
400 | index=$(( $(sed '/^s/ d' $INDEX | wc -l) +1 )) | 402 | local index=$(( $(sed '/^s/ d' $INDEX | wc -l) +1 )) |
401 | echo 'Creating a new gist' | 403 | echo 'Creating a new gist' |
402 | for file in $files; do | 404 | for file in $files; do |
403 | echo "\"$(basename $file)\": {\"content\": \"$(sed '$ !s/$/\\n/' $file)\"}," | 405 | echo "\"$(basename $file)\": {\"content\": \"$(sed '$ !s/$/\\n/' $file)\"}," |
404 | done | tr -d '\n' | sed 's/^/{/; s/,$/}/' \ | 406 | done | tr -d '\n' | sed 's/^/{/; s/,$/}/' \ |
405 | | echo "{ \"public\": $public, \"files\": $(cat -), \"description\": \"$description\"}" \ | 407 | | echo "{ \"public\": $public, \"files\": $(cat -), \"description\": \"$description\"}" \ |
406 | | curl -s -H "$auth_header" --data @- $GITHUB_API/gists \ | 408 | | curl -s -H "$AUTH_HEADER" --data @- $GITHUB_API/gists \ |
407 | | sed '1 s/^/[/; $ s/$/]/' \ | 409 | | sed '1 s/^/[/; $ s/$/]/' \ |
408 | | _parse_response $index >> $INDEX | 410 | | _parse_response $index >> $INDEX |
409 | 411 | ||
@@ -422,7 +424,7 @@ _edit_gist() { | |||
422 | echo -n 'Type new description: ' | 424 | echo -n 'Type new description: ' |
423 | read DESC < /dev/tty | 425 | read DESC < /dev/tty |
424 | echo "{ \"description\": \"$DESC\" }" \ | 426 | echo "{ \"description\": \"$DESC\" }" \ |
425 | | curl -X PATCH -H "$auth_header" --data @- $GITHUB_API/gists/$GIST_ID > /dev/null \ | 427 | | curl -X PATCH -H "$AUTH_HEADER" --data @- $GITHUB_API/gists/$GIST_ID > /dev/null \ |
426 | && _update | 428 | && _update |
427 | } | 429 | } |
428 | 430 | ||