diff options
author | typebrook <typebrook@gmail.com> | 2020-02-04 12:23:00 +0800 |
---|---|---|
committer | typebrook <typebrook@gmail.com> | 2020-02-04 12:23:00 +0800 |
commit | a6400d7fef9131e50c729697fe8da9512099313a (patch) | |
tree | 2e6a73e348aacda413c58ae0af6f1a08903f45fd | |
parent | f7eefc09f4a36ad8f1ce653ae968230c72971b5a (diff) |
update
-rwxr-xr-x | scripts/gist | 44 |
1 files changed, 28 insertions, 16 deletions
diff --git a/scripts/gist b/scripts/gist index 8a82a4b..5424389 100755 --- a/scripts/gist +++ b/scripts/gist | |||
@@ -44,14 +44,16 @@ | |||
44 | # * configuration | 44 | # * configuration |
45 | # gist (config | c) [token|user|folder|auto-sync|EDITOR|action [value]] | 45 | # gist (config | c) [token|user|folder|auto-sync|EDITOR|action [value]] |
46 | # | 46 | # |
47 | # * get gists from a given Github user | ||
48 | # gist (user | U) <user> | ||
49 | # | ||
47 | # * show this help message | 50 | # * show this help message |
48 | # gist (help | h) | 51 | # gist (help | h) |
49 | 52 | ||
50 | # TODO new command "user" to fetch other user's gists | ||
51 | # TODO grep mode for description, file content | 53 | # TODO grep mode for description, file content |
52 | # TODO push github.com (may need new token) | 54 | # TODO push github.com (may need new token) |
53 | # TODO description for current directory | 55 | # TODO description for current directory |
54 | # TODO error handling, unit test | 56 | # TODO unit test |
55 | # TODO test on mac and remote machine | 57 | # TODO test on mac and remote machine |
56 | # TODO completion | 58 | # TODO completion |
57 | 59 | ||
@@ -167,14 +169,14 @@ http_method() { | |||
167 | case "$configuredClient" in | 169 | case "$configuredClient" in |
168 | curl) [[ -n $token ]] && local extra="--header" local header="Authorization: token $token" | 170 | curl) [[ -n $token ]] && local extra="--header" local header="Authorization: token $token" |
169 | [[ $METHOD =~ (POST|PATCH) ]] && extra2="--data" | 171 | [[ $METHOD =~ (POST|PATCH) ]] && extra2="--data" |
170 | curl -X $METHOD -A curl -s $extra "$header" $extra2 @$http_data $@ ;; | 172 | curl -X $METHOD -A curl -s $extra "$header" $extra2 @$http_data "$@" ;; |
171 | wget) [[ -n $token ]] && local extra="--header" local header="Authorization: token $token" | 173 | wget) [[ -n $token ]] && local extra="--header" local header="Authorization: token $token" |
172 | [[ $METHOD =~ (POST|PATCH) ]] && extra2='--body-file' | 174 | [[ $METHOD =~ (POST|PATCH) ]] && extra2='--body-file' |
173 | wget --method=$METHOD -qO- $extra "$header" $extra2 $http_data $@ ;; | 175 | wget --method=$METHOD -qO- $extra "$header" $extra2 $http_data "$@" ;; |
174 | # TODO add other methods | ||
175 | httpie) [[ -n $token ]] && header="Authorization:token $token" | 176 | httpie) [[ -n $token ]] && header="Authorization:token $token" |
176 | [[ $METHOD =~ (POST|PATCH) ]] && extra2="@$http_data" | 177 | [[ $METHOD =~ (POST|PATCH) ]] && extra2="@$http_data" |
177 | http -b $METHOD $@ "$header" $extra2 ;; | 178 | http -b $METHOD "$@" "$header" $extra2 ;; |
179 | # TODO add other methods | ||
178 | fetch) fetch -q "$@" ;; | 180 | fetch) fetch -q "$@" ;; |
179 | esac | 181 | esac |
180 | } | 182 | } |
@@ -188,14 +190,10 @@ _show_list() { | |||
188 | echo ' gist update' | 190 | echo ' gist update' |
189 | return 0 | 191 | return 0 |
190 | fi | 192 | fi |
191 | local filter="" | 193 | local filter='/^s/ d' |
192 | if [[ $1 == "s" ]]; then | 194 | [[ $1 == "s" ]] && filter='/^[^s]/ d' |
193 | filter='/^[^s]/ d' | 195 | |
194 | else | 196 | while read index link blob_code file_num extra description; do |
195 | filter='/^s/ d' | ||
196 | fi | ||
197 | cat $INDEX \ | ||
198 | | while read index link blob_code file_num extra description; do | ||
199 | local repo=$folder/$(echo $link | sed 's#.*/##') | 197 | local repo=$folder/$(echo $link | sed 's#.*/##') |
200 | local occupy=0 | 198 | local occupy=0 |
201 | 199 | ||
@@ -206,8 +204,8 @@ _show_list() { | |||
206 | # if there is a commit not yet push, show red message "ahead" | 204 | # if there is a commit not yet push, show red message "ahead" |
207 | [[ -n $(cd $repo && git cherry) ]] 2>/dev/null && extra="\e[31m[ahead]\e[0m" && occupy=7 | 205 | [[ -n $(cd $repo && git cherry) ]] 2>/dev/null && extra="\e[31m[ahead]\e[0m" && occupy=7 |
208 | 206 | ||
209 | echo -e $index $link $file_num $extra $(echo $description | cut -c -$(( 60 -$occupy -1 )) ) | 207 | echo $index $link $file_num $extra $(echo $description | cut -c -$(( 60 -$occupy -1 )) ) |
210 | done \ | 208 | done < $INDEX \ |
211 | | sed "$filter" | 209 | | sed "$filter" |
212 | } | 210 | } |
213 | 211 | ||
@@ -267,6 +265,17 @@ _update() { | |||
267 | if [[ $auto_sync != "false" ]]; then (_sync_repos $1 > /dev/null 2>&1 &); fi | 265 | if [[ $auto_sync != "false" ]]; then (_sync_repos $1 > /dev/null 2>&1 &); fi |
268 | } | 266 | } |
269 | 267 | ||
268 | _query_user() { | ||
269 | local route="users/$1/gists" | ||
270 | result=$(http_method GET $GITHUB_API/$route | _parse_response) | ||
271 | [[ -z $result ]] && echo Failed to update gists && return 1 | ||
272 | |||
273 | echo "$result" \ | ||
274 | | while read index link blob_code file_num extra description; do | ||
275 | echo $link $file_num $extra $(echo $description | cut -c -70 ) | ||
276 | done | ||
277 | } | ||
278 | |||
270 | # update local git repos | 279 | # update local git repos |
271 | _sync_repos() { | 280 | _sync_repos() { |
272 | # clone repos which are not in the local | 281 | # clone repos which are not in the local |
@@ -484,6 +493,9 @@ case "$1" in | |||
484 | config | c) | 493 | config | c) |
485 | shift | 494 | shift |
486 | _configure "$@" ;; | 495 | _configure "$@" ;; |
496 | user | U) | ||
497 | shift | ||
498 | _query_user "$@" ;; | ||
487 | help | h) | 499 | help | h) |
488 | usage ;; | 500 | usage ;; |
489 | *) | 501 | *) |