diff options
author | typebrook <typebrook@gmail.com> | 2020-01-31 17:24:08 +0800 |
---|---|---|
committer | typebrook <typebrook@gmail.com> | 2020-01-31 17:24:08 +0800 |
commit | b992a5631ef62badf6f3bc8e36c2651433cb0d88 (patch) | |
tree | 327a50fdd1d1087cb86874014569e801811301c1 | |
parent | 62e700ed8b84b5cb43809fe22514f5298ecdb8c1 (diff) |
update
-rwxr-xr-x | scripts/gist | 57 |
1 files changed, 34 insertions, 23 deletions
diff --git a/scripts/gist b/scripts/gist index 4b7ef0c..45ec76e 100755 --- a/scripts/gist +++ b/scripts/gist | |||
@@ -127,6 +127,11 @@ _apply_config() { | |||
127 | echo 'To create/edit/delete a gist, a token is needed' | 127 | echo 'To create/edit/delete a gist, a token is needed' |
128 | return 1 | 128 | return 1 |
129 | fi | 129 | fi |
130 | elif [[ -z $token ]] && [[ $1 =~ ^(u|update)$ ]] && [[ $2 =~ ^(s|star) ]]; then | ||
131 | if ! (_ask_token); then | ||
132 | echo 'To get user starred gists, a token is needed' | ||
133 | return 1 | ||
134 | fi | ||
130 | fi | 135 | fi |
131 | 136 | ||
132 | source $CONFIG | 137 | source $CONFIG |
@@ -138,8 +143,6 @@ auth_header="Authorization: token $token" | |||
138 | 143 | ||
139 | [[ -z "$folder" ]] && folder=~/gist && mkdir -p $folder | 144 | [[ -z "$folder" ]] && folder=~/gist && mkdir -p $folder |
140 | INDEX=$folder/index | 145 | INDEX=$folder/index |
141 | # TODO join star file into index | ||
142 | STARRED=$folder/starred | ||
143 | 146 | ||
144 | ## This function determines which http get tool the system has installed and returns an error if there isnt one | 147 | ## This function determines which http get tool the system has installed and returns an error if there isnt one |
145 | getConfiguredClient() | 148 | getConfiguredClient() |
@@ -173,12 +176,17 @@ httpGet() | |||
173 | # TODO a way to show files | 176 | # TODO a way to show files |
174 | # TODO show git status outdated | 177 | # TODO show git status outdated |
175 | _show_list() { | 178 | _show_list() { |
176 | if [[ ! -e "$1" ]]; then | 179 | if [[ ! -e $INDEX ]]; then |
177 | echo 'No local file found for last update, please run command:' | 180 | echo 'No local file found for last update, please run command:' |
178 | echo " gist update $([[ $1 == $STARRED ]] && echo 'star')" | 181 | echo ' gist update' |
179 | return 0 | 182 | return 0 |
180 | fi | 183 | fi |
181 | cat $1 \ | 184 | if [[ $1 == "s" ]]; then |
185 | filter='/^[^s]/ d' | ||
186 | else | ||
187 | filter='/^s/ d' | ||
188 | fi | ||
189 | cat $INDEX \ | ||
182 | | while read index link blob_code file_num extra description; do | 190 | | while read index link blob_code file_num extra description; do |
183 | local repo=$folder/$(echo $link | sed 's#.*/##') | 191 | local repo=$folder/$(echo $link | sed 's#.*/##') |
184 | local occupy=0 | 192 | local occupy=0 |
@@ -191,7 +199,8 @@ _show_list() { | |||
191 | [[ -n $(cd $repo && git cherry) ]] 2>/dev/null && extra="\e[31m[ahead]\e[0m" && occupy=7 | 199 | [[ -n $(cd $repo && git cherry) ]] 2>/dev/null && extra="\e[31m[ahead]\e[0m" && occupy=7 |
192 | 200 | ||
193 | echo -e $index $link $file_num $extra $(echo $description | cut -c -$(( 60 -$occupy -1 )) ) | 201 | echo -e $index $link $file_num $extra $(echo $description | cut -c -$(( 60 -$occupy -1 )) ) |
194 | done | 202 | done \ |
203 | | sed "$filter" | ||
195 | } | 204 | } |
196 | 205 | ||
197 | # parse JSON from STDIN with string of commands | 206 | # parse JSON from STDIN with string of commands |
@@ -230,26 +239,28 @@ _parse_response() { | |||
230 | _update() { | 239 | _update() { |
231 | echo "fetching $user's gists from $GITHUB_API..." | 240 | echo "fetching $user's gists from $GITHUB_API..." |
232 | echo | 241 | echo |
233 | local list_file=$INDEX | ||
234 | local route="users/$user/gists" | 242 | local route="users/$user/gists" |
235 | local mark="" | 243 | local mark="" |
236 | [[ "$1" =~ ^(star|s)$ ]] && list_file=$STARRED && route="gists/starred" && mark="s" | 244 | local filter='/^[^s]/ d' |
245 | if [[ "$1" =~ ^(star|s)$ ]];then | ||
246 | route="gists/starred" | ||
247 | mark="s" | ||
248 | filter='/^[s]/ d' | ||
249 | fi | ||
237 | 250 | ||
238 | curl -H "$auth_header" $GITHUB_API/$route \ | 251 | response=$(curl -H "$auth_header" $GITHUB_API/$route) |
239 | | _parse_response | sed -E "s/^ */$mark/" > $list_file \ | 252 | false && echo Fail to update gists && return 1 |
240 | && _show_list $list_file \ | 253 | sed -i "$filter" $INDEX |
241 | || echo Fail to update gists | 254 | echo $response | _parse_response >> $INDEX |
255 | _show_list $mark | ||
242 | 256 | ||
243 | if [[ $auto_sync != "false" ]]; then (_sync_repos $1 > /dev/null 2>&1 &); fi | 257 | if [[ $auto_sync != "false" ]]; then (_sync_repos $1 > /dev/null 2>&1 &); fi |
244 | } | 258 | } |
245 | 259 | ||
246 | _sync_repos() { | 260 | _sync_repos() { |
247 | local list_file=$INDEX | ||
248 | [[ "$1" =~ ^(star|s)$ ]] && list_file=$STARRED && route="gists/starred" | ||
249 | |||
250 | # clone repos which are not in the local | 261 | # clone repos which are not in the local |
251 | comm -13 <(find $folder -maxdepth 1 -type d | sed '1d; s#.*/##' | sort) \ | 262 | comm -13 <(find $folder -maxdepth 1 -type d | sed '1d; s#.*/##' | sort) \ |
252 | <(cat $list_file | cut -d' ' -f2 | sed 's#.*/##' | sort) \ | 263 | <(cat $INDEX | cut -d' ' -f2 | sed 's#.*/##' | sort) \ |
253 | | xargs -I{} --max-procs 8 git clone git@github.com:{}.git $folder/{} | 264 | | xargs -I{} --max-procs 8 git clone git@github.com:{}.git $folder/{} |
254 | 265 | ||
255 | # pull if remote repo has different blob objects | 266 | # pull if remote repo has different blob objects |
@@ -267,18 +278,18 @@ _sync_repos() { | |||
267 | 278 | ||
268 | # get gist id from index files | 279 | # get gist id from index files |
269 | _gist_id() { | 280 | _gist_id() { |
270 | GIST_ID=$( (grep -hs '' $INDEX $STARRED || true) | sed -n "/^$1 / p" | cut -d' ' -f2 | sed -E 's#.*/##') | 281 | GIST_ID=$( (grep -hs '' $INDEX || true) | sed -n "/^$1 / p" | cut -d' ' -f2 | sed -E 's#.*/##') |
271 | if [[ -z "$GIST_ID" ]]; then | 282 | if [[ -z "$GIST_ID" ]]; then |
272 | echo -e "Not a valid index: \e[31m$1\e[0m" | 283 | echo -e "Not a valid index: \e[31m$1\e[0m" |
273 | echo Use the index in the first column instead: | 284 | echo Use the index in the first column instead: |
274 | echo | 285 | echo |
275 | _show_list "$INDEX" | 286 | _show_list |
276 | return 1 | 287 | return 1 |
277 | fi | 288 | fi |
278 | } | 289 | } |
279 | 290 | ||
280 | # TODO a better way without source | ||
281 | # FIXME source cause eixt | 291 | # FIXME source cause eixt |
292 | # TODO a better way without source | ||
282 | _goto_gist() { | 293 | _goto_gist() { |
283 | _gist_id $1 | 294 | _gist_id $1 |
284 | 295 | ||
@@ -311,7 +322,7 @@ _delete_gist() { | |||
311 | # remove repos which are not in user gists anymore | 322 | # remove repos which are not in user gists anymore |
312 | _clean_repos() { | 323 | _clean_repos() { |
313 | comm -23 <(find $folder -maxdepth 1 -type d | sed '1d; s#.*/##' | sort) \ | 324 | comm -23 <(find $folder -maxdepth 1 -type d | sed '1d; s#.*/##' | sort) \ |
314 | <(cat $INDEX $STARRED 2> /dev/null | cut -d' ' -f2 | sed 's#.*/##' | sort) \ | 325 | <(cat $INDEX 2> /dev/null | cut -d' ' -f2 | sed 's#.*/##' | sort) \ |
315 | | while read dir; do | 326 | | while read dir; do |
316 | mv $folder/$dir /tmp && echo move $folder/$dir to /tmp | 327 | mv $folder/$dir /tmp && echo move $folder/$dir to /tmp |
317 | done | 328 | done |
@@ -401,7 +412,7 @@ _create_gist() { | |||
401 | && echo -e '\nGist created' \ | 412 | && echo -e '\nGist created' \ |
402 | || echo 'Fail to create gist' | 413 | || echo 'Fail to create gist' |
403 | 414 | ||
404 | _show_list $INDEX | tail -1 | 415 | _show_list | tail -1 |
405 | } | 416 | } |
406 | 417 | ||
407 | # update description of a gist | 418 | # update description of a gist |
@@ -422,9 +433,9 @@ usage() { | |||
422 | getConfiguredClient | 433 | getConfiguredClient |
423 | case "$1" in | 434 | case "$1" in |
424 | "") | 435 | "") |
425 | _show_list $INDEX ;; | 436 | _show_list ;; |
426 | star | s) | 437 | star | s) |
427 | _show_list $STARRED ;; | 438 | _show_list s ;; |
428 | update | u) | 439 | update | u) |
429 | _update "$2" ;; | 440 | _update "$2" ;; |
430 | new | n) | 441 | new | n) |