summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authortypebrook <typebrook@gmail.com>2020-02-04 00:41:23 +0800
committertypebrook <typebrook@gmail.com>2020-02-04 00:41:23 +0800
commite507ca1e7ed4ecbd057feafb2c797b8a8e154969 (patch)
treec7eea9f2b425a42af4151a4a9f4084bd66b9aa45
parentbc3ffa99d3d4d2074d8ec65dcd4b6e63902c34de (diff)
update
-rwxr-xr-xscripts/gist63
1 files changed, 30 insertions, 33 deletions
diff --git a/scripts/gist b/scripts/gist
index 3be610d..cf312a6 100755
--- a/scripts/gist
+++ b/scripts/gist
@@ -56,7 +56,9 @@
56# TODO completion 56# TODO completion
57 57
58# Shell configuration 58# Shell configuration
59set -o pipefail
59[ "$TRACE" ] && set -x 60[ "$TRACE" ] && set -x
61trap 'rm -f "$http_data" "tmp_file"' EXIT
60 62
61GITHUB_API=https://api.github.com 63GITHUB_API=https://api.github.com
62CONFIG=~/.config/gist.conf; mkdir -p ~/.config 64CONFIG=~/.config/gist.conf; mkdir -p ~/.config
@@ -162,14 +164,14 @@ getConfiguredClient() {
162## Allows to call the users configured client without if statements everywhere 164## Allows to call the users configured client without if statements everywhere
163http_method() { 165http_method() {
164 local METHOD=$1; shift 166 local METHOD=$1; shift
165 [[ $METHOD =~ (POST|PATCH) ]] && read data
166 case "$configuredClient" in 167 case "$configuredClient" in
167 curl) [[ -n $token ]] && local extra="--header" local header="Authorization: token $token" 168 curl) [[ -n $token ]] && local extra="--header" local header="Authorization: token $token"
168 [[ $METHOD =~ (POST|PATCH) ]] && extra2="--data" 169 [[ $METHOD =~ (POST|PATCH) ]] && extra2="--data"
169 curl -X $METHOD -A curl -s $extra "$header" $extra2 "$data" $@ ;; 170 curl -X $METHOD -A curl -s $extra "$header" $extra2 @$http_data $@ ;;
170 wget) [[ -n $token ]] && local extra="--header" local header="Authorization: token $token" 171 wget) [[ -n $token ]] && local extra="--header" local header="Authorization: token $token"
171 [[ $METHOD =~ (POST|PATCH) ]] && extra2='--body-data' 172 [[ $METHOD =~ (POST|PATCH) ]] && extra2='--body-file'
172 wget --method=$METHOD -qO- $extra "$header" $extra2 "$data" $@ ;; 173 wget --method=$METHOD -qO- $extra "$header" $extra2 $http_data $@ ;;
174 # TODO add other methods
173 httpie) [[ -n $token ]] && header="Authorization:token $token" 175 httpie) [[ -n $token ]] && header="Authorization:token $token"
174 http -b $METHOD $@ "$header";; 176 http -b $METHOD $@ "$header";;
175 fetch) fetch -q "$@" ;; 177 fetch) fetch -q "$@" ;;
@@ -255,12 +257,10 @@ _update() {
255 filter='/^[s]/ d' 257 filter='/^[s]/ d'
256 fi 258 fi
257 259
258 local response=$(http_method GET $GITHUB_API/$route) 260 result=$(http_method GET $GITHUB_API/$route | _parse_response)
261 [[ -z $result ]] && echo Failed to update gists && return 1
259 262
260 # FIXME if response fail 263 sed -i "$filter" $INDEX && echo "$result" >> $INDEX
261 false && echo Failed to update gists && return 1
262 sed -i "$filter" $INDEX
263 echo $response | _parse_response >> $INDEX
264 _show_list $mark 264 _show_list $mark
265 265
266 if [[ $auto_sync != "false" ]]; then (_sync_repos $1 > /dev/null 2>&1 &); fi 266 if [[ $auto_sync != "false" ]]; then (_sync_repos $1 > /dev/null 2>&1 &); fi
@@ -385,7 +385,7 @@ _set_gist() {
385 filename="$2" 385 filename="$2"
386 shift; shift;; 386 shift; shift;;
387 -p) 387 -p)
388 public=false 388 public=False
389 shift;; 389 shift;;
390 *) 390 *)
391 files="$1 $files" 391 files="$1 $files"
@@ -395,11 +395,10 @@ _set_gist() {
395 ls $files > /dev/null || return 1 395 ls $files > /dev/null || return 1
396} 396}
397 397
398# TODO remove tmp file
399# Let user type the content of gist before setting filename 398# Let user type the content of gist before setting filename
400_new_file() { 399_new_file() {
401 [[ -t 0 ]] && echo "Type a gist. <Ctrl-C> to cancel, <Ctrl-D> when done" > /dev/tty 400 [[ -t 0 ]] && echo "Type a gist. <Ctrl-C> to cancel, <Ctrl-D> when done" > /dev/tty
402 local tmp_file=$(mktemp) 401 tmp_file=$(mktemp)
403 cat > $tmp_file 402 cat > $tmp_file
404 echo -e '\n' > /dev/tty 403 echo -e '\n' > /dev/tty
405 [[ -z "$1" ]] && read -p 'Type file name: ' filename < /dev/tty 404 [[ -z "$1" ]] && read -p 'Type file name: ' filename < /dev/tty
@@ -407,12 +406,19 @@ _new_file() {
407 echo /tmp/$filename 406 echo /tmp/$filename
408} 407}
409 408
410_make_gist_body() { 409_gist_body(){
411 return 0 410 echo "
411import os.path
412files_json = {}
413files = sys.stdin.readline().split()
414description = sys.stdin.readline().replace('\n','')
415for file in files:
416 with open(file, 'r') as f:
417 files_json[os.path.basename(file)] = {'content': f.read()}
418print(json.dumps({'public': $public, 'files': files_json, 'description': description}))
419 "
412} 420}
413 421
414# FIXME curl bad credential exit code
415# FIXME file content with " and \
416# create a new gist with files 422# create a new gist with files
417_create_gist() { 423_create_gist() {
418 _set_gist "$@" || return 1 424 _set_gist "$@" || return 1
@@ -420,27 +426,18 @@ _create_gist() {
420 [[ -z "$description" ]] && read -p 'Type description: ' description < /dev/tty 426 [[ -z "$description" ]] && read -p 'Type description: ' description < /dev/tty
421 427
422 echo 'Creating a new gist...' 428 echo 'Creating a new gist...'
423 local index=$(( $(sed '/^s/ d' $INDEX | wc -l) +1 )) 429 http_data=$(mktemp)
424 echo -e "$files\n$description\n$public" | AccessJsonElement " 430
425import os.path 431 echo -e "$files\n$description" \
426files_json = {} 432 | AccessJsonElement "$(_gist_body)" > $http_data \
427files = sys.stdin.readline().split() 433 && http_method POST $GITHUB_API/gists \
428description = sys.stdin.readline().replace('\n','')
429public = sys.stdin.readline().replace('\n','')
430for file in files:
431 with open(file, 'r') as f:
432 files_json[os.path.basename(file)] = {'content': f.read()}
433print(json.dumps({'public': public, 'files': files_json, 'description': description}))
434 " | tee json \
435 | http_method POST $GITHUB_API/gists \
436 | tee result \ 434 | tee result \
437 | sed '1 s/^/[/; $ s/$/]/' \ 435 | sed '1 s/^/[/; $ s/$/]/' \
438 | _parse_response $index \ 436 | _parse_response $(( $(sed '/^s/ d' $INDEX | wc -l) +1 )) >> $INDEX
439 | read result
440 437
441 if true; then 438 if [[ $? -eq 0 ]]; then
442 echo 'Gist is created' 439 echo 'Gist is created'
443 echo $result >> $INDEX && _show_list | tail -1 440 _show_list | tail -1
444 else 441 else
445 echo 'Failed to create gist' 442 echo 'Failed to create gist'
446 fi 443 fi