diff options
author | typebrook <typebrook@gmail.com> | 2020-02-08 15:16:54 +0800 |
---|---|---|
committer | typebrook <typebrook@gmail.com> | 2020-02-08 15:16:54 +0800 |
commit | ce8fa56e813af98d035190a52f5427010b21f72e (patch) | |
tree | 8e32716e5bf6a3d345078acb1083de6d655a79bf | |
parent | fefb48a4ce734e35348dce4d45c81e6554559e9c (diff) |
update
-rwxr-xr-x | scripts/gist | 37 |
1 files changed, 20 insertions, 17 deletions
diff --git a/scripts/gist b/scripts/gist index 5217c53..fad035c 100755 --- a/scripts/gist +++ b/scripts/gist | |||
@@ -37,12 +37,15 @@ | |||
37 | 37 | ||
38 | # TODO test on bats, mac and remote machine | 38 | # TODO test on bats, mac and remote machine |
39 | # TODO completion | 39 | # TODO completion |
40 | # FIXME mac: tac=tail -r | ||
40 | 41 | ||
41 | # Shell configuration | 42 | # Shell configuration |
42 | set -o pipefail | 43 | set -o pipefail |
43 | [ "$TRACE" ] && set -x | 44 | [ "$TRACE" ] && set -x |
44 | trap 'rm -f "$http_data" "$tmp_file"' EXIT | 45 | trap 'rm -f "$http_data" "$tmp_file"' EXIT |
45 | 46 | ||
47 | [ $(uname) == 'Darwin' ] && alias tac='tail -r' | ||
48 | |||
46 | GITHUB_API=https://api.github.com | 49 | GITHUB_API=https://api.github.com |
47 | CONFIG=~/.config/gist.conf; mkdir -p ~/.config | 50 | CONFIG=~/.config/gist.conf; mkdir -p ~/.config |
48 | configuredClient="" | 51 | configuredClient="" |
@@ -65,7 +68,7 @@ _configure() { | |||
65 | fi | 68 | fi |
66 | 69 | ||
67 | umask 0077 && touch $CONFIG | 70 | umask 0077 && touch $CONFIG |
68 | sed -i "/^$1=/ d" $CONFIG && [[ "$target" =~ [^=]$ ]] && echo $target >> $CONFIG | 71 | sed -i'' -e "/^$1=/ d" $CONFIG && [[ "$target" =~ [^=]$ ]] && echo $target >> $CONFIG |
69 | cat $CONFIG | 72 | cat $CONFIG |
70 | } | 73 | } |
71 | 74 | ||
@@ -235,10 +238,10 @@ for gist in raw: | |||
235 | # FIXME replace space with sed | 238 | # FIXME replace space with sed |
236 | # parse response from gists require | 239 | # parse response from gists require |
237 | _parse_response() { | 240 | _parse_response() { |
238 | AccessJsonElement "$(_handle_gists)" \ | 241 | | AccessJsonElement "$(_handle_gists)" \ |
239 | | tac | sed 's/, /,/g' | nl -s' ' \ | 242 | | tac | sed -e 's/, /,/g' | nl -s' ' \ |
240 | | while read index link file_url_array public file_num comment_num author description; do | 243 | | while read index link file_url_array public file_num comment_num author description; do |
241 | local blob_code=$(echo $file_url_array | tr ',' '\n' | sed -E 's#.*raw/(.*)/.*#\1#' | sort | cut -c -7 | paste -sd '-') | 244 | local blob_code=$(echo $file_url_array | tr ',' '\n' | sed -E -e 's#.*raw/(.*)/.*#\1#' | sort | cut -c -7 | paste -s -d '-' -) |
242 | [[ $public == 'False' ]] && local mark=p | 245 | [[ $public == 'False' ]] && local mark=p |
243 | [[ -n $1 ]] && local index=$1 | 246 | [[ -n $1 ]] && local index=$1 |
244 | echo $mark$index $link $blob_code $file_num $comment_num $author $description | tr -d '"' | 247 | echo $mark$index $link $blob_code $file_num $comment_num $author $description | tr -d '"' |
@@ -263,7 +266,7 @@ _update() { | |||
263 | result=$(http_method GET $GITHUB_API/$route | _parse_response) | 266 | result=$(http_method GET $GITHUB_API/$route | _parse_response) |
264 | [[ -z $result ]] && echo Failed to update gists && return 1 | 267 | [[ -z $result ]] && echo Failed to update gists && return 1 |
265 | 268 | ||
266 | sed -i "$filter" $INDEX && echo "$result" >> $INDEX | 269 | sed -i'' -e "$filter" $INDEX && echo "$result" >> $INDEX |
267 | _show_list $mark | 270 | _show_list $mark |
268 | 271 | ||
269 | if [[ $auto_sync != 'false' ]]; then (_sync_repos $1 > /dev/null 2>&1 &); fi | 272 | if [[ $auto_sync != 'false' ]]; then (_sync_repos $1 > /dev/null 2>&1 &); fi |
@@ -288,14 +291,14 @@ _blob_code() { | |||
288 | # update local git repos | 291 | # update local git repos |
289 | _sync_repos() { | 292 | _sync_repos() { |
290 | # clone repos which are not in the local | 293 | # clone repos which are not in the local |
291 | comm -13 <(find $folder -maxdepth 1 -type d | sed '1d; s#.*/##' | sort) \ | 294 | comm -13 <(find $folder -maxdepth 1 -type d | sed -e '1d; s#.*/##' | sort) \ |
292 | <(cat $INDEX | cut -d' ' -f2 | sed 's#.*/##' | sort) \ | 295 | <(cat $INDEX | cut -d' ' -f2 | sed -e 's#.*/##' | sort) \ |
293 | | xargs -I{} --max-procs 8 git clone git@github.com:{}.git $folder/{} | 296 | | xargs -I{} --max-procs 8 git clone git@github.com:{}.git $folder/{} |
294 | 297 | ||
295 | # pull if remote repo has different blob objects | 298 | # pull if remote repo has different blob objects |
296 | cat $INDEX | cut -d' ' -f2,3 \ | 299 | cat $INDEX | cut -d' ' -f2,3 \ |
297 | | while read url blob_code_remote; do | 300 | | while read url blob_code_remote; do |
298 | local repo=$folder/$(echo $url | sed 's#.*/##') | 301 | local repo=$folder/$(echo $url | sed -e 's#.*/##') |
299 | local blob_code_local=$(_blob_code $repo) | 302 | local blob_code_local=$(_blob_code $repo) |
300 | cd $repo \ | 303 | cd $repo \ |
301 | && [[ $blob_code_local != $blob_code_remote ]] \ | 304 | && [[ $blob_code_local != $blob_code_remote ]] \ |
@@ -307,7 +310,7 @@ _sync_repos() { | |||
307 | 310 | ||
308 | # get gist id from index files | 311 | # get gist id from index files |
309 | _gist_id() { | 312 | _gist_id() { |
310 | GIST_ID=$( (grep -hs '' $INDEX || true) | sed -n "/^$1 / p" | cut -d' ' -f2 | sed -E 's#.*/##') | 313 | GIST_ID=$( (grep -hs '' $INDEX || true) | sed -n -e "/^$1 / p" | cut -d' ' -f2 | sed -E -e 's#.*/##') |
311 | if [[ -z "$GIST_ID" ]]; then | 314 | if [[ -z "$GIST_ID" ]]; then |
312 | echo -e "Not a valid index: \e[31m$1\e[0m" | 315 | echo -e "Not a valid index: \e[31m$1\e[0m" |
313 | echo Use the index in the first column instead: | 316 | echo Use the index in the first column instead: |
@@ -345,14 +348,14 @@ _delete_gist() { | |||
345 | _gist_id "$i" | 348 | _gist_id "$i" |
346 | http_method DELETE $GITHUB_API/gists/$GIST_ID \ | 349 | http_method DELETE $GITHUB_API/gists/$GIST_ID \ |
347 | && echo "$i" deleted \ | 350 | && echo "$i" deleted \ |
348 | && sed -i -E "/^$i / d" $INDEX | 351 | && sed -E -i'' -e "/^$i / d" $INDEX |
349 | done | 352 | done |
350 | } | 353 | } |
351 | 354 | ||
352 | # remove repos which are not in user gists anymore | 355 | # remove repos which are not in user gists anymore |
353 | _clean_repos() { | 356 | _clean_repos() { |
354 | comm -23 <(find $folder -maxdepth 1 -type d | sed '1d; s#.*/##' | sort) \ | 357 | comm -23 <(find $folder -maxdepth 1 -type d | sed -e '1d; s#.*/##' | sort) \ |
355 | <(cat $INDEX 2> /dev/null | cut -d' ' -f2 | sed 's#.*/##' | sort) \ | 358 | <(cat $INDEX 2> /dev/null | cut -d' ' -f2 | sed -e 's#.*/##' | sort) \ |
356 | | while read dir; do | 359 | | while read dir; do |
357 | mv $folder/$dir /tmp && echo move $folder/$dir to /tmp | 360 | mv $folder/$dir /tmp && echo move $folder/$dir to /tmp |
358 | done | 361 | done |
@@ -453,10 +456,10 @@ _create_gist() { | |||
453 | echo -e "$files\n$description" \ | 456 | echo -e "$files\n$description" \ |
454 | | AccessJsonElement "$(_gist_body)" > $http_data \ | 457 | | AccessJsonElement "$(_gist_body)" > $http_data \ |
455 | && http_method POST $GITHUB_API/gists \ | 458 | && http_method POST $GITHUB_API/gists \ |
456 | | sed '1 s/^/[/; $ s/$/]/' \ | 459 | | sed -e '1 s/^/[/; $ s/$/]/' \ |
457 | | _parse_response $(( $(sed '/^s/ d' $INDEX | wc -l) +1 )) \ | 460 | | _parse_response $(( $(sed -e '/^s/ d' $INDEX | wc -l) +1 )) \ |
458 | | tee -a $INDEX \ | 461 | | tee -a $INDEX \ |
459 | | cut -d' ' -f2 | sed -E 's#.*/##' \ | 462 | | cut -d' ' -f2 | sed -E -e 's#.*/##' \ |
460 | | (xargs -I{} git clone git@github.com:{}.git $folder/{} &> /dev/null &) | 463 | | (xargs -I{} git clone git@github.com:{}.git $folder/{} &> /dev/null &) |
461 | 464 | ||
462 | if [[ $? -eq 0 ]]; then | 465 | if [[ $? -eq 0 ]]; then |
@@ -475,13 +478,13 @@ _edit_gist() { | |||
475 | read DESC < /dev/tty | 478 | read DESC < /dev/tty |
476 | 479 | ||
477 | http_data=$(mktemp) | 480 | http_data=$(mktemp) |
478 | echo { \"description\": \"$(echo $DESC | sed 's/"/\\"/g')\" } > $http_data | 481 | echo { \"description\": \"$(echo $DESC | sed -e 's/"/\\"/g')\" } > $http_data |
479 | http_method PATCH $http_data $GITHUB_API/gists/$GIST_ID > /dev/null \ | 482 | http_method PATCH $http_data $GITHUB_API/gists/$GIST_ID > /dev/null \ |
480 | && _update | 483 | && _update |
481 | } | 484 | } |
482 | 485 | ||
483 | usage() { | 486 | usage() { |
484 | sed -E -n ' /^$/ q; 7,$ s/^# //p' $0 | 487 | sed -E -n -e ' /^$/ q; 7,$ s/^# //p' $0 |
485 | } | 488 | } |
486 | 489 | ||
487 | _apply_config "$@" || exit 1 | 490 | _apply_config "$@" || exit 1 |