diff options
author | typebrook <typebrook@gmail.com> | 2020-02-15 15:40:19 +0800 |
---|---|---|
committer | typebrook <typebrook@gmail.com> | 2020-02-15 15:40:19 +0800 |
commit | 082cda661a1d69d866668c69a4b846c7d7adf862 (patch) | |
tree | fd70d17c05a8aa2efe50ab3d3503142a049c2a08 | |
parent | dc75fe677587a5a9c08c1b166029f0597c90272f (diff) |
update
-rwxr-xr-x | scripts/gist | 162 |
1 files changed, 83 insertions, 79 deletions
diff --git a/scripts/gist b/scripts/gist index acb0066..55a021f 100755 --- a/scripts/gist +++ b/scripts/gist | |||
@@ -72,16 +72,17 @@ getConfiguredClient() { | |||
72 | # TODO return false if code is not 20x | 72 | # TODO return false if code is not 20x |
73 | http_method() { | 73 | http_method() { |
74 | local METHOD=$1; shift | 74 | local METHOD=$1; shift |
75 | local header_opt; local header; local data_opt | ||
75 | case "$configuredClient" in | 76 | case "$configuredClient" in |
76 | curl) [[ -n $token ]] && local extra="--header" local header="Authorization: token $token" | 77 | curl) [[ -n $token ]] && header_opt="--header" header="Authorization: token $token" |
77 | [[ $METHOD =~ (POST|PATCH) ]] && extra2="--data" | 78 | [[ $METHOD =~ (POST|PATCH) ]] && data_opt='--data' |
78 | curl -X $METHOD -A curl -s $extra "$header" $extra2 @$http_data "$@" ;; | 79 | curl -X "$METHOD" -A curl -s $header_opt "$header" $data_opt "@$http_data" "$@" ;; |
79 | wget) [[ -n $token ]] && local extra="--header" local header="Authorization: token $token" | 80 | wget) [[ -n $token ]] && header_opt="--header" header="Authorization: token $token" |
80 | [[ $METHOD =~ (POST|PATCH) ]] && extra2='--body-file' | 81 | [[ $METHOD =~ (POST|PATCH) ]] && data_opt='--body-file' |
81 | wget --method=$METHOD -qO- $extra "$header" $extra2 $http_data "$@" ;; | 82 | wget --method="$METHOD" -qO- $header_opt "$header" $data_opt "$http_data" "$@" ;; |
82 | httpie) [[ -n $token ]] && header="Authorization:token $token" | 83 | httpie) [[ -n $token ]] && header="Authorization:token $token" |
83 | [[ $METHOD =~ (POST|PATCH) ]] && extra2="@$http_data" | 84 | [[ $METHOD =~ (POST|PATCH) ]] && data_opt="@$http_data" |
84 | http -b $METHOD "$@" "$header" $extra2 ;; | 85 | http -b "$METHOD" "$@" "$header" "$data_opt" ;; |
85 | esac | 86 | esac |
86 | } | 87 | } |
87 | 88 | ||
@@ -116,14 +117,15 @@ update() { | |||
116 | echo "Error: no active internet connection" >&2 | 117 | echo "Error: no active internet connection" >&2 |
117 | exit 1 | 118 | exit 1 |
118 | else | 119 | else |
119 | if [[ $latestVersion != $currentVersion ]]; then | 120 | if [[ $latestVersion != "$currentVersion" ]]; then |
120 | echo "Version $latestVersion available" | 121 | echo "Version $latestVersion available" |
121 | echo -n "Do you wish to update $repositoryName [Y/n]: " | 122 | echo -n "Do you wish to update $repositoryName [Y/n]: " |
122 | read -r answer | 123 | read -r answer |
123 | if [[ $answer == [Yy] ]]; then | 124 | if [[ $answer == [Yy] ]]; then |
124 | cd ~ || { echo 'Update Failed'; exit 1; } | 125 | cd ~ || { echo 'Update Failed'; exit 1; } |
125 | if [[ -d ~/$repositoryName ]]; then rm -r -f $repositoryName || { echo "Permissions Error: try running the update as sudo"; exit 1; } ; fi | 126 | if [[ -d ~/$repositoryName ]]; then rm -r -f $repositoryName || { echo "Permissions Error: try running the update as sudo"; exit 1; } ; fi |
126 | echo -n "Downloading latest version of: $repositoryName." | 127 | echo -n "Downloading latest version of: $repositoryName." |
128 | # shellcheck disable=SC2015 | ||
127 | git clone -q "https://github.com/$githubUserName/$repositoryName" && touch .BSnippetsHiddenFile || { echo "Failure!"; exit 1; } & | 129 | git clone -q "https://github.com/$githubUserName/$repositoryName" && touch .BSnippetsHiddenFile || { echo "Failure!"; exit 1; } & |
128 | while [ ! -f .BSnippetsHiddenFile ]; do { echo -n "."; sleep 2; };done | 130 | while [ ! -f .BSnippetsHiddenFile ]; do { echo -n "."; sleep 2; };done |
129 | rm -f .BSnippetsHiddenFile | 131 | rm -f .BSnippetsHiddenFile |
@@ -145,7 +147,7 @@ update() { | |||
145 | 147 | ||
146 | # handle configuration cases | 148 | # handle configuration cases |
147 | _configure() { | 149 | _configure() { |
148 | [[ -z $@ ]] && (${EDITOR:-vi} $CONFIG) && return 0 | 150 | [[ $# == 0 ]] && (${EDITOR:-vi} "$CONFIG") && return 0 |
149 | 151 | ||
150 | local valid_keys='user|token|folder|auto_sync|EDITOR|action' | 152 | local valid_keys='user|token|folder|auto_sync|EDITOR|action' |
151 | if [[ $1 =~ ^($valid_keys)$ ]]; then | 153 | if [[ $1 =~ ^($valid_keys)$ ]]; then |
@@ -157,49 +159,50 @@ _configure() { | |||
157 | elif [[ $1 == 'auto_sync' ]]; then | 159 | elif [[ $1 == 'auto_sync' ]]; then |
158 | [[ ! $2 =~ ^(true|false)$ ]] && return 1 | 160 | [[ ! $2 =~ ^(true|false)$ ]] && return 1 |
159 | fi | 161 | fi |
160 | local key=$1 && shift && local target=$key=\'$@\' | 162 | local key=$1 && shift && local target=$key="'$*'" |
161 | else | 163 | else |
162 | echo "Not a valid key for configuration, use <$valid_keys> instead." | 164 | echo "Not a valid key for configuration, use <$valid_keys> instead." |
163 | return 1 | 165 | return 1 |
164 | fi | 166 | fi |
165 | 167 | ||
166 | umask 0077 && touch $CONFIG | 168 | umask 0077 && touch "$CONFIG" |
167 | sed -i'' -e "/^$key=/ d" $CONFIG && [[ -n $target ]] && echo $target >> $CONFIG | 169 | sed -i'' -e "/^$key=/ d" "$CONFIG" && [[ -n $target ]] && echo "$target" >> "$CONFIG" |
168 | cat $CONFIG | 170 | cat "$CONFIG" |
169 | } | 171 | } |
170 | 172 | ||
171 | # prompt for username | 173 | # prompt for username |
172 | _ask_username() { | 174 | _ask_username() { |
173 | while [[ ! $user =~ ^[[:alnum:]]+$ ]]; do | 175 | while [[ ! $user =~ ^[[:alnum:]]+$ ]]; do |
174 | [[ -n $user ]] && echo "Not a valid username" | 176 | [[ -n $user ]] && echo "Not a valid username" |
175 | read -p "Github username: " user < /dev/tty | 177 | read -r -p "Github username: " user < /dev/tty |
176 | done | 178 | done |
177 | _configure user $user | 179 | _configure user "$user" |
178 | } | 180 | } |
179 | 181 | ||
180 | # prompt for token | 182 | # prompt for token |
181 | # TODO check token scope contains gist, ref: https://developer.github.com/v3/apps/oauth_applications/#check-a-token | 183 | # TODO check token scope contains gist, ref: https://developer.github.com/v3/apps/oauth_applications/#check-a-token |
182 | _ask_token() { | 184 | _ask_token() { |
183 | echo -n "Create a new token from web browser? [Y/n] " | 185 | echo -n "Create a new token from web browser? [Y/n] " |
184 | read answer < /dev/tty | 186 | read -r answer < /dev/tty |
185 | if [[ ! $answer =~ ^(N|n|No|NO|no)$ ]]; then | 187 | if [[ ! $answer =~ ^(N|n|No|NO|no)$ ]]; then |
186 | python -mwebbrowser https://github.com/settings/tokens/new\?scopes\=gist | 188 | python -mwebbrowser https://github.com/settings/tokens/new?scopes=gist |
187 | fi | 189 | fi |
188 | 190 | ||
189 | while [[ ! $token =~ ^[[:alnum:]]{40}$ ]]; do | 191 | while [[ ! $token =~ ^[[:alnum:]]{40}$ ]]; do |
190 | [[ -n $token ]] && echo "Not a valid token" | 192 | [[ -n $token ]] && echo "Not a valid token" |
191 | read -p "Paste your token here (Ctrl-C to skip): " token < /dev/tty | 193 | read -r -p "Paste your token here (Ctrl-C to skip): " token < /dev/tty |
192 | done | 194 | done |
193 | _configure token $token | 195 | _configure token "$token" |
194 | } | 196 | } |
195 | 197 | ||
196 | # check configuration is fine with user setting | 198 | # check configuration is fine with user setting |
197 | _validate_config(){ | 199 | _validate_config(){ |
198 | source $CONFIG 2> /dev/null | 200 | # shellcheck source=/dev/null |
201 | source "$CONFIG" 2> /dev/null | ||
199 | [[ $1 =~ ^(c|config|h|help|u|user|update|version) ]] && return 0 | 202 | [[ $1 =~ ^(c|config|h|help|u|user|update|version) ]] && return 0 |
200 | if [[ -z $user ]]; then | 203 | if [[ -z $user ]]; then |
201 | echo 'Hi fellow! To access your gists, I need your Github username' | 204 | echo 'Hi fellow! To access your gists, I need your Github username' |
202 | echo "Also a personal token with scope which allows "gist"!'" | 205 | echo "Also a personal token with scope which allows \"gist\"!" |
203 | echo | 206 | echo |
204 | _ask_username && _ask_token && init=true | 207 | _ask_username && _ask_token && init=true |
205 | elif [[ -z $token && $1 =~ ^(n|new|e|edit|D|delete)$ ]]; then | 208 | elif [[ -z $token && $1 =~ ^(n|new|e|edit|D|delete)$ ]]; then |
@@ -218,8 +221,6 @@ _validate_config(){ | |||
218 | # load configuration | 221 | # load configuration |
219 | _apply_config() { | 222 | _apply_config() { |
220 | _validate_config "$@" || return 1 | 223 | _validate_config "$@" || return 1 |
221 | |||
222 | AUTH_HEADER="Authorization: token $token" | ||
223 | INDEX=$folder/index; [[ -e $INDEX ]] || touch $INDEX | 224 | INDEX=$folder/index; [[ -e $INDEX ]] || touch $INDEX |
224 | } | 225 | } |
225 | 226 | ||
@@ -231,17 +232,18 @@ _check_repo_status() { | |||
231 | echo "\e[32m[Not cloned yet]\e[0m"; | 232 | echo "\e[32m[Not cloned yet]\e[0m"; |
232 | fi | 233 | fi |
233 | else | 234 | else |
234 | cd $1 | 235 | cd "$1" || exit |
235 | if [[ -n $(git status --short) ]] &>/dev/null; then | 236 | if [[ -n $(git status --short) ]] &>/dev/null; then |
236 | echo "\e[36m[working]\e[0m" | 237 | echo "\e[36m[working]\e[0m" |
237 | else | 238 | else |
238 | [[ $(_blob_code $1) != $2 ]] 2>/dev/null && echo "\e[31m[outdated]\e[0m" | 239 | [[ $(_blob_code "$1") != "$2" ]] 2>/dev/null && echo "\e[31m[outdated]\e[0m" |
239 | [[ -n $(git cherry) ]] 2>/dev/null && echo "\e[31m[ahead]\e[0m" | 240 | [[ -n $(git cherry) ]] 2>/dev/null && echo "\e[31m[ahead]\e[0m" |
240 | fi | 241 | fi |
241 | fi | 242 | fi |
242 | } | 243 | } |
243 | 244 | ||
244 | # Show the list of gist, but not updated time | 245 | # Show the list of gist, but not updated time |
246 | # show username for starred gist | ||
245 | _show_list() { | 247 | _show_list() { |
246 | if [[ ! -e $INDEX ]]; then | 248 | if [[ ! -e $INDEX ]]; then |
247 | echo 'No local file found for last update, please run command:' | 249 | echo 'No local file found for last update, please run command:' |
@@ -252,14 +254,15 @@ _show_list() { | |||
252 | [[ $mark == 's' ]] && filter='/^ *[^ s]/ d; /^$/ d' | 254 | [[ $mark == 's' ]] && filter='/^ *[^ s]/ d; /^$/ d' |
253 | 255 | ||
254 | sed -e "$filter" $INDEX \ | 256 | sed -e "$filter" $INDEX \ |
255 | | while read index link blob_code file_num comment_num author description; do | 257 | | while read -r index link blob_code file_num comment_num author description; do |
256 | [[ $1 == "s" ]] && local name=$author | 258 | [[ $mark == 's' ]] && local name=$author |
257 | local repo=$folder/$(echo $link | sed 's#.*/##') | 259 | #local repo; repo=$folder/$(echo $link | sed 's#.*/##') |
258 | local extra=$(_check_repo_status $repo $blob_code) | 260 | local repo; repo=$folder/${link##*/} |
261 | local extra; extra=$(_check_repo_status "$repo" "$blob_code") | ||
259 | [[ -z $extra ]] && extra="$file_num $comment_num" | 262 | [[ -z $extra ]] && extra="$file_num $comment_num" |
260 | 263 | ||
261 | echo -e "$(printf "% 3s" $index)" $link $name $extra $description \ | 264 | echo -e "$(printf "% 3s" "$index") $link $name $extra $description" \ |
262 | | cut -c -$(tput cols) | 265 | | cut -c -"$(tput cols)" |
263 | done | 266 | done |
264 | 267 | ||
265 | [[ $hint == 'true' ]] && echo -e '\nrun "gist fetch" to update gists or "gist help" for more details' > /dev/tty \ | 268 | [[ $hint == 'true' ]] && echo -e '\nrun "gist fetch" to update gists or "gist help" for more details' > /dev/tty \ |
@@ -268,20 +271,20 @@ _show_list() { | |||
268 | 271 | ||
269 | # TODO support filenames, file contents | 272 | # TODO support filenames, file contents |
270 | _grep_content() { | 273 | _grep_content() { |
271 | _show_list | grep -i $1 | 274 | _show_list | grep -i "$1" |
272 | } | 275 | } |
273 | 276 | ||
274 | # Open Github repository import page | 277 | # Open Github repository import page |
275 | _import_to_github() { | 278 | _import_to_github() { |
276 | _gist_id $1 | 279 | _gist_id "$1" |
277 | echo put the folowing URL into web page: | 280 | echo put the folowing URL into web page: |
278 | echo -n git@github.com:$GIST_ID.git | 281 | echo -n "git@github.com:$GIST_ID.git" |
279 | python -mwebbrowser https://github.com/new/import | 282 | python -mwebbrowser https://github.com/new/import |
280 | } | 283 | } |
281 | 284 | ||
282 | _push_to_remote() { | 285 | _push_to_remote() { |
283 | _gist_id $1 | 286 | _gist_id "$1" |
284 | cd $folder/$GIST_ID && git add . \ | 287 | cd "$folder/$GIST_ID" && git add . \ |
285 | && git commit --allow-empty-message -m '' && git push origin master | 288 | && git commit --allow-empty-message -m '' && git push origin master |
286 | } | 289 | } |
287 | 290 | ||
@@ -304,11 +307,11 @@ for gist in raw: | |||
304 | _parse_response() { | 307 | _parse_response() { |
305 | _parse_gists \ | 308 | _parse_gists \ |
306 | | tac | sed -e 's/, /,/g' | nl -s' ' \ | 309 | | tac | sed -e 's/, /,/g' | nl -s' ' \ |
307 | | while read index link file_url_array public file_num comment_num author description; do | 310 | | while read -r index link file_url_array public file_num comment_num author description; do |
308 | local blob_code=$(echo $file_url_array | tr ',' '\n' | sed -E -e 's#.*raw/(.*)/.*#\1#' | sort | cut -c -7 | paste -s -d '-' -) | 311 | local blob_code; blob_code=$(echo "$file_url_array" | tr ',' '\n' | sed -E -e 's#.*raw/(.*)/.*#\1#' | sort | cut -c -7 | paste -s -d '-' -) |
309 | [[ $public == 'False' ]] && local mark=p | 312 | [[ $public == 'False' ]] && local mark=p |
310 | [[ -n $1 ]] && local index=$1 | 313 | [[ -n $1 ]] && local index=$1 |
311 | echo $mark$index $link $blob_code $file_num $comment_num $author $description | tr -d '"' | 314 | echo "$mark$index $link $blob_code $file_num $comment_num $author $description" | tr -d '"' |
312 | done | 315 | done |
313 | } | 316 | } |
314 | 317 | ||
@@ -332,22 +335,22 @@ _fetch_gists() { | |||
332 | sed -i'' -e "$filter" $INDEX && echo "$result" >> $INDEX | 335 | sed -i'' -e "$filter" $INDEX && echo "$result" >> $INDEX |
333 | mark=$mark _show_list | 336 | mark=$mark _show_list |
334 | 337 | ||
335 | [[ $auto_sync == 'true' ]] && (_sync_repos $1 > /dev/null 2>&1 &) | 338 | [[ $auto_sync == 'true' ]] && (_sync_repos "$1" > /dev/null 2>&1 &) |
336 | } | 339 | } |
337 | 340 | ||
338 | _query_user() { | 341 | _query_user() { |
339 | local route="users/$1/gists" | 342 | local route="users/$1/gists" |
340 | result=$(http_method GET $GITHUB_API/$route | _parse_response) | 343 | result=$(http_method GET $GITHUB_API/"$route" | _parse_response) |
341 | [[ -z $result ]] && echo "Failed to query $1's gists" && return 1 | 344 | [[ -z $result ]] && echo "Failed to query $1's gists" && return 1 |
342 | 345 | ||
343 | echo "$result" \ | 346 | echo "$result" \ |
344 | | while read index link blob_code file_num extra description; do | 347 | | while read -r index link blob_code file_num extra description; do |
345 | echo $link $file_num $extra $description | cut -c -$(tput cols) | 348 | echo "$link $file_num $extra $description" | cut -c -"$(tput cols)" |
346 | done | 349 | done |
347 | } | 350 | } |
348 | 351 | ||
349 | _blob_code() { | 352 | _blob_code() { |
350 | cd $1 && git ls-tree master | cut -d' ' -f3 | cut -c-7 | sort | paste -sd '-' | 353 | cd "$1" && git ls-tree master | cut -d' ' -f3 | cut -c-7 | sort | paste -sd '-' |
351 | } | 354 | } |
352 | 355 | ||
353 | # update local git repos | 356 | # update local git repos |
@@ -355,16 +358,16 @@ _blob_code() { | |||
355 | _sync_repos() { | 358 | _sync_repos() { |
356 | # clone repos which are not in the local | 359 | # clone repos which are not in the local |
357 | comm -13 <(find $folder -maxdepth 1 -type d | sed -e '1d; s#.*/##' | sort) \ | 360 | comm -13 <(find $folder -maxdepth 1 -type d | sed -e '1d; s#.*/##' | sort) \ |
358 | <(cat $INDEX | cut -d' ' -f2 | sed -e 's#.*/##' | sort) \ | 361 | <(cut -d' ' -f2 < "$INDEX" | sed -e 's#.*/##' | sort) \ |
359 | | xargs -I{} --max-procs 8 git clone git@github.com:{}.git $folder/{} | 362 | | xargs -I{} --max-procs 8 git clone git@github.com:{}.git $folder/{} |
360 | 363 | ||
361 | # pull if remote repo has different blob objects | 364 | # pull if remote repo has different blob objects |
362 | cat $INDEX | cut -d' ' -f2,3 \ | 365 | cut -d' ' -f2,3 < "$INDEX" \ |
363 | | while read url blob_code_remote; do | 366 | | while read -r url blob_code_remote; do |
364 | local repo=$folder/$(echo $url | sed -e 's#.*/##') | 367 | local repo; repo=$folder/${url##*/} |
365 | local blob_code_local=$(_blob_code $repo) | 368 | local blob_code_local; blob_code_local=$(_blob_code "$repo") |
366 | cd $repo \ | 369 | cd "$repo" \ |
367 | && [[ $blob_code_local != $blob_code_remote ]] \ | 370 | && [[ $blob_code_local != "$blob_code_remote" ]] \ |
368 | && [[ $(git rev-parse origin/master) == $(git rev-parse master) ]] \ | 371 | && [[ $(git rev-parse origin/master) == $(git rev-parse master) ]] \ |
369 | && git pull | 372 | && git pull |
370 | done | 373 | done |
@@ -384,13 +387,13 @@ _gist_id() { | |||
384 | } | 387 | } |
385 | 388 | ||
386 | _goto_gist() { | 389 | _goto_gist() { |
387 | _gist_id $1 || return 1 | 390 | _gist_id "$1" || return 1 |
388 | 391 | ||
389 | if [[ ! -d $folder/$GIST_ID ]]; then | 392 | if [[ ! -d $folder/$GIST_ID ]]; then |
390 | echo 'Cloning gist as repo...' | 393 | echo 'Cloning gist as repo...' |
391 | git clone git@github.com:$GIST_ID.git $folder/$GIST_ID | 394 | git clone "git@github.com:$GIST_ID".git "$folder/$GIST_ID" |
392 | 395 | ||
393 | if [[ $? -eq 0 ]]; then | 396 | if $?; then |
394 | echo 'Repo is cloned' > /dev/tty | 397 | echo 'Repo is cloned' > /dev/tty |
395 | else | 398 | else |
396 | echo 'Failed to clone the gist' > /dev/tty | 399 | echo 'Failed to clone the gist' > /dev/tty |
@@ -398,8 +401,8 @@ _goto_gist() { | |||
398 | fi | 401 | fi |
399 | fi | 402 | fi |
400 | 403 | ||
401 | [[ $2 != '--no-action' ]] && cd $folder/$GIST_ID && eval "$action" | 404 | [[ $2 != '--no-action' ]] && cd "$folder/$GIST_ID" && eval "$action" |
402 | echo $folder/$GIST_ID | 405 | echo "$folder/$GIST_ID" |
403 | } | 406 | } |
404 | 407 | ||
405 | _delete_gist() { | 408 | _delete_gist() { |
@@ -409,7 +412,7 @@ _delete_gist() { | |||
409 | 412 | ||
410 | for i in "$@"; do | 413 | for i in "$@"; do |
411 | _gist_id "$i" | 414 | _gist_id "$i" |
412 | http_method DELETE $GITHUB_API/gists/$GIST_ID \ | 415 | http_method DELETE "$GITHUB_API/gists/$GIST_ID" \ |
413 | && echo "$i" deleted \ | 416 | && echo "$i" deleted \ |
414 | && sed -E -i'' -e "/^$i / d" $INDEX | 417 | && sed -E -i'' -e "/^$i / d" $INDEX |
415 | done | 418 | done |
@@ -418,9 +421,9 @@ _delete_gist() { | |||
418 | # remove repos which are not in user gists anymore | 421 | # remove repos which are not in user gists anymore |
419 | _clean_repos() { | 422 | _clean_repos() { |
420 | comm -23 <(find $folder -maxdepth 1 -type d | sed -e '1d; s#.*/##' | sort) \ | 423 | comm -23 <(find $folder -maxdepth 1 -type d | sed -e '1d; s#.*/##' | sort) \ |
421 | <(cat $INDEX 2> /dev/null | cut -d' ' -f2 | sed -e 's#.*/##' | sort) \ | 424 | <(cut -d' ' -f2 < "$INDEX" | sed -e 's#.*/##' | sort 2> /dev/null ) \ |
422 | | while read dir; do | 425 | | while read -r dir; do |
423 | mv $folder/$dir /tmp && echo move $folder/$dir to /tmp | 426 | mv $folder/"$dir" /tmp && echo move $folder/"$dir" to /tmp |
424 | done | 427 | done |
425 | } | 428 | } |
426 | 429 | ||
@@ -454,18 +457,18 @@ for comment in raw: | |||
454 | } | 457 | } |
455 | 458 | ||
456 | _show_detail() { | 459 | _show_detail() { |
457 | _gist_id $1 | 460 | _gist_id "$1" |
458 | http_method GET $GITHUB_API/gists/$GIST_ID \ | 461 | http_method GET "$GITHUB_API/gists/$GIST_ID" \ |
459 | | _parse_gist | 462 | | _parse_gist |
460 | 463 | ||
461 | http_method GET $GITHUB_API/gists/$GIST_ID/comments \ | 464 | http_method GET "$GITHUB_API/gists/$GIST_ID"/comments \ |
462 | | _parse_comment | 465 | | _parse_comment |
463 | } | 466 | } |
464 | 467 | ||
465 | # set filename/description/permission for a new gist | 468 | # set filename/description/permission for a new gist |
466 | _set_gist() { | 469 | _set_gist() { |
467 | public=True | 470 | public=True |
468 | while [[ -n $@ ]]; do case $1 in | 471 | while [[ -n "$*" ]]; do case $1 in |
469 | -d | --desc) | 472 | -d | --desc) |
470 | description="$2" | 473 | description="$2" |
471 | shift; shift;; | 474 | shift; shift;; |
@@ -480,6 +483,7 @@ _set_gist() { | |||
480 | shift;; | 483 | shift;; |
481 | esac | 484 | esac |
482 | done | 485 | done |
486 | # FIXME user array instead | ||
483 | ls $files > /dev/null || return 1 | 487 | ls $files > /dev/null || return 1 |
484 | } | 488 | } |
485 | 489 | ||
@@ -487,11 +491,11 @@ _set_gist() { | |||
487 | _new_file() { | 491 | _new_file() { |
488 | [[ -t 0 ]] && echo "Type a gist. <Ctrl-C> to cancel, <Ctrl-D> when done" > /dev/tty | 492 | [[ -t 0 ]] && echo "Type a gist. <Ctrl-C> to cancel, <Ctrl-D> when done" > /dev/tty |
489 | tmp_file=$(mktemp) | 493 | tmp_file=$(mktemp) |
490 | cat > $tmp_file | 494 | cat > "$tmp_file" |
491 | echo -e '\n' > /dev/tty | 495 | echo -e '\n' > /dev/tty |
492 | [[ -z $1 ]] && read -p 'Type file name: ' filename < /dev/tty | 496 | [[ -z $1 ]] && read -r -p 'Type file name: ' filename < /dev/tty |
493 | mv $tmp_file /tmp/$filename | 497 | mv "$tmp_file" /tmp/"$filename" |
494 | echo /tmp/$filename | 498 | echo /tmp/"$filename" |
495 | } | 499 | } |
496 | 500 | ||
497 | _gist_body(){ | 501 | _gist_body(){ |
@@ -510,14 +514,14 @@ print(json.dumps({'public': $public, 'files': files_json, 'description': descrip | |||
510 | # create a new gist with files | 514 | # create a new gist with files |
511 | _create_gist() { | 515 | _create_gist() { |
512 | _set_gist "$@" || return 1 | 516 | _set_gist "$@" || return 1 |
513 | [[ -z $files ]] && files=$(_new_file $filename) | 517 | [[ -z $files ]] && files=$(_new_file "$filename") |
514 | [[ -z $description ]] && read -p 'Type description: ' description < /dev/tty | 518 | [[ -z $description ]] && read -r -p 'Type description: ' description < /dev/tty |
515 | 519 | ||
516 | echo 'Creating a new gist...' | 520 | echo 'Creating a new gist...' |
517 | http_data=$(mktemp) | 521 | http_data=$(mktemp) |
518 | 522 | ||
519 | echo -e "$files\n$description" \ | 523 | echo -e "$files\n$description" \ |
520 | | _gist_body > $http_data \ | 524 | | _gist_body > "$http_data" \ |
521 | && http_method POST $GITHUB_API/gists \ | 525 | && http_method POST $GITHUB_API/gists \ |
522 | | sed -e '1 s/^/[/; $ s/$/]/' \ | 526 | | sed -e '1 s/^/[/; $ s/$/]/' \ |
523 | | _parse_response $(( $(sed -e '/^s/ d' $INDEX | wc -l) +1 )) \ | 527 | | _parse_response $(( $(sed -e '/^s/ d' $INDEX | wc -l) +1 )) \ |
@@ -525,7 +529,7 @@ _create_gist() { | |||
525 | | cut -d' ' -f2 | sed -E -e 's#.*/##' \ | 529 | | cut -d' ' -f2 | sed -E -e 's#.*/##' \ |
526 | | (xargs -I{} git clone git@github.com:{}.git $folder/{} &> /dev/null &) | 530 | | (xargs -I{} git clone git@github.com:{}.git $folder/{} &> /dev/null &) |
527 | 531 | ||
528 | if [[ $? -eq 0 ]]; then | 532 | if $?; then |
529 | echo 'Gist is created' | 533 | echo 'Gist is created' |
530 | hint=false _show_list | tail -1 | 534 | hint=false _show_list | tail -1 |
531 | else | 535 | else |
@@ -536,19 +540,19 @@ _create_gist() { | |||
536 | # update description of a gist | 540 | # update description of a gist |
537 | # TODO use response to modify index file, do not fetch gists again | 541 | # TODO use response to modify index file, do not fetch gists again |
538 | _edit_gist() { | 542 | _edit_gist() { |
539 | _gist_id $1 | 543 | _gist_id "$1" |
540 | 544 | ||
541 | echo -n 'Type new description: ' | 545 | echo -n 'Type new description: ' |
542 | read DESC < /dev/tty | 546 | read -r DESC < /dev/tty |
543 | 547 | ||
544 | http_data=$(mktemp) | 548 | http_data=$(mktemp) |
545 | echo { \"description\": \"$(echo $DESC | sed -e 's/"/\\"/g')\" } > $http_data | 549 | echo '{' \"description\": \"${$DESC//\"/\\\"}\" '}' > "$http_data" |
546 | http_method PATCH $http_data $GITHUB_API/gists/$GIST_ID > /dev/null \ | 550 | http_method PATCH "$http_data $GITHUB_API/gists/$GIST_ID" > /dev/null \ |
547 | && hint=false _fetch_gists | grep -E "^[ ]+$1" | 551 | && hint=false _fetch_gists | grep -E "^[ ]+$1" |
548 | } | 552 | } |
549 | 553 | ||
550 | usage() { | 554 | usage() { |
551 | sed -E -n -e ' /^$/ q; 7,$ s/^# //p' $0 | 555 | sed -E -n -e ' /^$/ q; 7,$ s/^# //p' "$0" |
552 | } | 556 | } |
553 | 557 | ||
554 | _apply_config "$@" || exit 1 | 558 | _apply_config "$@" || exit 1 |