aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authortypebrook <typebrook@gmail.com>2020-05-08 11:29:12 +0800
committertypebrook <typebrook@gmail.com>2020-05-08 13:23:57 +0800
commit0cbb0e7ef11db50c4e96a19e878a79830bf4fd42 (patch)
tree9d924ca68745e0da3f4bc0067f6ff913287627da
parent419e521de3f8a455f9b1abbb25a7ffcb1635779b (diff)
Improve removing temporary files
- Specify directory instead of each files to remove - background job cannot be trapped, so need to call script again
-rwxr-xr-xgist13
1 files changed, 8 insertions, 5 deletions
diff --git a/gist b/gist
index c2769eb..510deba 100755
--- a/gist
+++ b/gist
@@ -73,7 +73,10 @@ protocol=https
73# Shell configuration 73# Shell configuration
74set -o pipefail 74set -o pipefail
75[[ $TRACE == 'true' ]] && set -x 75[[ $TRACE == 'true' ]] && set -x
76trap 'rm -f "$http_data" "$tmp_file"' EXIT 76
77# clean temporary files
78tmp_dir=$(mktemp -d)
79trap "rm -r $tmp_dir" EXIT
77 80
78# Mac compatibility 81# Mac compatibility
79tac() { 82tac() {
@@ -641,7 +644,7 @@ _set_gist() {
641 644
642# Let user type the content of gist before setting filename 645# Let user type the content of gist before setting filename
643_new_file() { 646_new_file() {
644 tmp_file=$(mktemp) 647 tmp_file=$(mktemp -p $tmp_dir)
645 if [[ -z $INPUT ]]; then 648 if [[ -z $INPUT ]]; then
646 echo "Type a gist. <Ctrl-C> to cancel, <Ctrl-D> when done" > /dev/tty 649 echo "Type a gist. <Ctrl-C> to cancel, <Ctrl-D> when done" > /dev/tty
647 cat > "$tmp_file" 650 cat > "$tmp_file"
@@ -676,7 +679,7 @@ _create_gist() {
676 [[ -z $description ]] && read -e -r -p 'Type description: ' description < /dev/tty 679 [[ -z $description ]] && read -e -r -p 'Type description: ' description < /dev/tty
677 680
678 echo 'Creating a new gist...' 681 echo 'Creating a new gist...'
679 http_data=$(mktemp) 682 http_data=$(mktemp -p $tmp_dir)
680 echo -e "${files[*]}\n$description" \ 683 echo -e "${files[*]}\n$description" \
681 | _gist_body > "$http_data" \ 684 | _gist_body > "$http_data" \
682 && http_method POST $GITHUB_API/gists \ 685 && http_method POST $GITHUB_API/gists \
@@ -708,7 +711,7 @@ _edit_gist() {
708 DESC="$2" 711 DESC="$2"
709 fi 712 fi
710 713
711 http_data=$(mktemp) 714 http_data=$(mktemp -p $tmp_dir)
712 echo '{' \"description\": \""${DESC//\"/\\\"}"\" '}' > "$http_data" 715 echo '{' \"description\": \""${DESC//\"/\\\"}"\" '}' > "$http_data"
713 new_record=$(http_method PATCH "$GITHUB_API/gists/$GIST_ID" \ 716 new_record=$(http_method PATCH "$GITHUB_API/gists/$GIST_ID" \
714 | sed -e '1 s/^/[/; $ s/$/]/' \ 717 | sed -e '1 s/^/[/; $ s/$/]/' \
@@ -749,7 +752,7 @@ _tag_gist() {
749 local tags="$(sed -e 's/,//g; s/#/ /g; s/^ //g' <<<"$tags_string")" 752 local tags="$(sed -e 's/,//g; s/#/ /g; s/^ //g' <<<"$tags_string")"
750 read -e -p 'Edit tags: ' -i "$tags" -r -a new_tags < /dev/tty 753 read -e -p 'Edit tags: ' -i "$tags" -r -a new_tags < /dev/tty
751 local hashtags="${new_tags[@]/#/#}" 754 local hashtags="${new_tags[@]/#/#}"
752 (_edit_gist $1 "${description}${hashtags:+ }${hashtags}" &>/dev/null &) 755 ($0 edit $1 "${description}${hashtags:+ }${hashtags}" &>/dev/null &)
753 # if user want to filter gists with given tags 756 # if user want to filter gists with given tags
754 else 757 else
755 local pattern="($(sed -E 's/([^ ]+)/#\1/g; s/ /[[:space:]]|/g; s/\./[^ ]/g' <<<"$@") )" 758 local pattern="($(sed -E 's/([^ ]+)/#\1/g; s/ /[[:space:]]|/g; s/\./[^ ]/g' <<<"$@") )"