diff options
| -rwxr-xr-x | gist | 46 |
1 files changed, 26 insertions, 20 deletions
| @@ -37,6 +37,7 @@ | |||
| 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 |
| @@ -65,7 +66,7 @@ _configure() { | |||
| 65 | fi | 66 | fi |
| 66 | 67 | ||
| 67 | umask 0077 && touch $CONFIG | 68 | umask 0077 && touch $CONFIG |
| 68 | sed -i "/^$1=/ d" $CONFIG && [[ "$target" =~ [^=]$ ]] && echo $target >> $CONFIG | 69 | sed -i'' -e "/^$1=/ d" $CONFIG && [[ "$target" =~ [^=]$ ]] && echo $target >> $CONFIG |
| 69 | cat $CONFIG | 70 | cat $CONFIG |
| 70 | } | 71 | } |
| 71 | 72 | ||
| @@ -170,10 +171,11 @@ _show_list() { | |||
| 170 | [[ -z $1 ]] && local filter='/^ *s/ d; /^$/ d' | 171 | [[ -z $1 ]] && local filter='/^ *s/ d; /^$/ d' |
| 171 | [[ $1 == "s" ]] && local filter='/^ *[^ s]/ d; /^$/ d' | 172 | [[ $1 == "s" ]] && local filter='/^ *[^ s]/ d; /^$/ d' |
| 172 | 173 | ||
| 173 | while read index link blob_code file_num extra author description; do | 174 | while read index link blob_code file_num comment_num author description; do |
| 174 | [[ $1 == "s" ]] && local name=$author | 175 | [[ $1 == "s" ]] && local name=$author |
| 175 | local repo=$folder/$(echo $link | sed 's#.*/##') | 176 | local repo=$folder/$(echo $link | sed 's#.*/##') |
| 176 | local occupy=0 | 177 | local occupy=0 |
| 178 | local extra="$file_num $comment_num" | ||
| 177 | 179 | ||
| 178 | # if repo is not yet cloned, show green message "Not cloned yet" | 180 | # if repo is not yet cloned, show green message "Not cloned yet" |
| 179 | [[ ! -d $repo ]] && extra="\e[32m[Not cloned yet]\e[0m" && occupy=16 | 181 | [[ ! -d $repo ]] && extra="\e[32m[Not cloned yet]\e[0m" && occupy=16 |
| @@ -184,7 +186,7 @@ _show_list() { | |||
| 184 | # if there is a commit not yet push, show red message "ahead" | 186 | # if there is a commit not yet push, show red message "ahead" |
| 185 | [[ -n $(cd $repo && git cherry) ]] 2>/dev/null && extra="\e[31m[ahead]\e[0m" && occupy=7 | 187 | [[ -n $(cd $repo && git cherry) ]] 2>/dev/null && extra="\e[31m[ahead]\e[0m" && occupy=7 |
| 186 | 188 | ||
| 187 | echo -e "$(printf "% 3s" $index)" $link $name $file_num $extra $(echo $description | cut -c -$(( 60 -$occupy -1 )) ) | 189 | echo -e "$(printf "% 3s" $index)" $link $name $extra $(echo $description | cut -c -$(( 60 -$occupy -1 )) ) |
| 188 | done < $INDEX \ | 190 | done < $INDEX \ |
| 189 | | sed "$filter" | 191 | | sed "$filter" |
| 190 | echo -e '\nrun "gist help" or "gist h" for more details' > /dev/tty | 192 | echo -e '\nrun "gist help" or "gist h" for more details' > /dev/tty |
| @@ -231,16 +233,20 @@ for gist in raw: | |||
| 231 | } | 233 | } |
| 232 | 234 | ||
| 233 | # TODO check if a user has no gist | 235 | # TODO check if a user has no gist |
| 236 | # FIXME replace space with sed | ||
| 234 | # parse response from gists require | 237 | # parse response from gists require |
| 235 | _parse_response() { | 238 | _parse_response() { |
| 236 | AccessJsonElement "$(_handle_gists)" \ | 239 | tee a \ |
| 237 | | tac | sed 's/, /,/g' | nl -s' ' \ | 240 | | AccessJsonElement "$(_handle_gists)" \ |
| 241 | | tee b \ | ||
| 242 | | tail -r | sed -e 's/, /,/g' | nl -s' ' \ | ||
| 243 | | tee ba \ | ||
| 238 | | while read index link file_url_array public file_num comment_num author description; do | 244 | | while read index link file_url_array public file_num comment_num author description; do |
| 239 | local blob_code=$(echo $file_url_array | tr ',' '\n' | sed -E 's#.*raw/(.*)/.*#\1#' | sort | cut -c -7 | paste -sd '-') | 245 | local blob_code=$(echo $file_url_array | tr ',' '\n' | sed -E -e 's#.*raw/(.*)/.*#\1#' | sort | cut -c -7 | paste -s -d '-' -) |
| 240 | [[ $public == 'False' ]] && local mark=p | 246 | [[ $public == 'False' ]] && local mark=p |
| 241 | [[ -n $1 ]] && local index=$1 | 247 | [[ -n $1 ]] && local index=$1 |
| 242 | echo $mark$index $link $blob_code $file_num $comment_num $author $description | tr -d '"' | 248 | echo $mark$index $link $blob_code $file_num $comment_num $author $description | tr -d '"' |
| 243 | done | 249 | done | tee c |
| 244 | } | 250 | } |
| 245 | 251 | ||
| 246 | # TODO pagnation for more than 30 gists | 252 | # TODO pagnation for more than 30 gists |
| @@ -261,7 +267,7 @@ _update() { | |||
| 261 | result=$(http_method GET $GITHUB_API/$route | _parse_response) | 267 | result=$(http_method GET $GITHUB_API/$route | _parse_response) |
| 262 | [[ -z $result ]] && echo Failed to update gists && return 1 | 268 | [[ -z $result ]] && echo Failed to update gists && return 1 |
| 263 | 269 | ||
| 264 | sed -i "$filter" $INDEX && echo "$result" >> $INDEX | 270 | sed -i'' -e "$filter" $INDEX && echo "$result" >> $INDEX |
| 265 | _show_list $mark | 271 | _show_list $mark |
| 266 | 272 | ||
| 267 | if [[ $auto_sync != 'false' ]]; then (_sync_repos $1 > /dev/null 2>&1 &); fi | 273 | if [[ $auto_sync != 'false' ]]; then (_sync_repos $1 > /dev/null 2>&1 &); fi |
| @@ -286,14 +292,14 @@ _blob_code() { | |||
| 286 | # update local git repos | 292 | # update local git repos |
| 287 | _sync_repos() { | 293 | _sync_repos() { |
| 288 | # clone repos which are not in the local | 294 | # clone repos which are not in the local |
| 289 | comm -13 <(find $folder -maxdepth 1 -type d | sed '1d; s#.*/##' | sort) \ | 295 | comm -13 <(find $folder -maxdepth 1 -type d | sed -e '1d; s#.*/##' | sort) \ |
| 290 | <(cat $INDEX | cut -d' ' -f2 | sed 's#.*/##' | sort) \ | 296 | <(cat $INDEX | cut -d' ' -f2 | sed -e 's#.*/##' | sort) \ |
| 291 | | xargs -I{} --max-procs 8 git clone git@github.com:{}.git $folder/{} | 297 | | xargs -I{} --max-procs 8 git clone git@github.com:{}.git $folder/{} |
| 292 | 298 | ||
| 293 | # pull if remote repo has different blob objects | 299 | # pull if remote repo has different blob objects |
| 294 | cat $INDEX | cut -d' ' -f2,3 \ | 300 | cat $INDEX | cut -d' ' -f2,3 \ |
| 295 | | while read url blob_code_remote; do | 301 | | while read url blob_code_remote; do |
| 296 | local repo=$folder/$(echo $url | sed 's#.*/##') | 302 | local repo=$folder/$(echo $url | sed -e 's#.*/##') |
| 297 | local blob_code_local=$(_blob_code $repo) | 303 | local blob_code_local=$(_blob_code $repo) |
| 298 | cd $repo \ | 304 | cd $repo \ |
| 299 | && [[ $blob_code_local != $blob_code_remote ]] \ | 305 | && [[ $blob_code_local != $blob_code_remote ]] \ |
| @@ -305,7 +311,7 @@ _sync_repos() { | |||
| 305 | 311 | ||
| 306 | # get gist id from index files | 312 | # get gist id from index files |
| 307 | _gist_id() { | 313 | _gist_id() { |
| 308 | GIST_ID=$( (grep -hs '' $INDEX || true) | sed -n "/^$1 / p" | cut -d' ' -f2 | sed -E 's#.*/##') | 314 | GIST_ID=$( (grep -hs '' $INDEX || true) | sed -n -e "/^$1 / p" | cut -d' ' -f2 | sed -E -e 's#.*/##') |
| 309 | if [[ -z "$GIST_ID" ]]; then | 315 | if [[ -z "$GIST_ID" ]]; then |
| 310 | echo -e "Not a valid index: \e[31m$1\e[0m" | 316 | echo -e "Not a valid index: \e[31m$1\e[0m" |
| 311 | echo Use the index in the first column instead: | 317 | echo Use the index in the first column instead: |
| @@ -343,14 +349,14 @@ _delete_gist() { | |||
| 343 | _gist_id "$i" | 349 | _gist_id "$i" |
| 344 | http_method DELETE $GITHUB_API/gists/$GIST_ID \ | 350 | http_method DELETE $GITHUB_API/gists/$GIST_ID \ |
| 345 | && echo "$i" deleted \ | 351 | && echo "$i" deleted \ |
| 346 | && sed -i -E "/^$i / d" $INDEX | 352 | && sed -E -i'' -e "/^$i / d" $INDEX |
| 347 | done | 353 | done |
| 348 | } | 354 | } |
| 349 | 355 | ||
| 350 | # remove repos which are not in user gists anymore | 356 | # remove repos which are not in user gists anymore |
| 351 | _clean_repos() { | 357 | _clean_repos() { |
| 352 | comm -23 <(find $folder -maxdepth 1 -type d | sed '1d; s#.*/##' | sort) \ | 358 | comm -23 <(find $folder -maxdepth 1 -type d | sed -e '1d; s#.*/##' | sort) \ |
| 353 | <(cat $INDEX 2> /dev/null | cut -d' ' -f2 | sed 's#.*/##' | sort) \ | 359 | <(cat $INDEX 2> /dev/null | cut -d' ' -f2 | sed -e 's#.*/##' | sort) \ |
| 354 | | while read dir; do | 360 | | while read dir; do |
| 355 | mv $folder/$dir /tmp && echo move $folder/$dir to /tmp | 361 | mv $folder/$dir /tmp && echo move $folder/$dir to /tmp |
| 356 | done | 362 | done |
| @@ -451,10 +457,10 @@ _create_gist() { | |||
| 451 | echo -e "$files\n$description" \ | 457 | echo -e "$files\n$description" \ |
| 452 | | AccessJsonElement "$(_gist_body)" > $http_data \ | 458 | | AccessJsonElement "$(_gist_body)" > $http_data \ |
| 453 | && http_method POST $GITHUB_API/gists \ | 459 | && http_method POST $GITHUB_API/gists \ |
| 454 | | sed '1 s/^/[/; $ s/$/]/' \ | 460 | | sed -e '1 s/^/[/; $ s/$/]/' \ |
| 455 | | _parse_response $(( $(sed '/^s/ d' $INDEX | wc -l) +1 )) \ | 461 | | _parse_response $(( $(sed -e '/^s/ d' $INDEX | wc -l) +1 )) \ |
| 456 | | tee -a $INDEX \ | 462 | | tee -a $INDEX \ |
| 457 | | cut -d' ' -f2 | sed -E 's#.*/##' \ | 463 | | cut -d' ' -f2 | sed -E -e 's#.*/##' \ |
| 458 | | (xargs -I{} git clone git@github.com:{}.git $folder/{} &> /dev/null &) | 464 | | (xargs -I{} git clone git@github.com:{}.git $folder/{} &> /dev/null &) |
| 459 | 465 | ||
| 460 | if [[ $? -eq 0 ]]; then | 466 | if [[ $? -eq 0 ]]; then |
| @@ -473,13 +479,13 @@ _edit_gist() { | |||
| 473 | read DESC < /dev/tty | 479 | read DESC < /dev/tty |
| 474 | 480 | ||
| 475 | http_data=$(mktemp) | 481 | http_data=$(mktemp) |
| 476 | echo { \"description\": \"$(echo $DESC | sed 's/"/\\"/g')\" } > $http_data | 482 | echo { \"description\": \"$(echo $DESC | sed -e 's/"/\\"/g')\" } > $http_data |
| 477 | http_method PATCH $http_data $GITHUB_API/gists/$GIST_ID > /dev/null \ | 483 | http_method PATCH $http_data $GITHUB_API/gists/$GIST_ID > /dev/null \ |
| 478 | && _update | 484 | && _update |
| 479 | } | 485 | } |
| 480 | 486 | ||
| 481 | usage() { | 487 | usage() { |
| 482 | sed -E -n ' /^$/ q; 7,$ s/^# //p' $0 | 488 | sed -E -n -e ' /^$/ q; 7,$ s/^# //p' $0 |
| 483 | } | 489 | } |
| 484 | 490 | ||
| 485 | _apply_config "$@" || exit 1 | 491 | _apply_config "$@" || exit 1 |