diff options
author | typebrook <typebrook@gmail.com> | 2020-05-09 12:28:06 +0800 |
---|---|---|
committer | typebrook <typebrook@gmail.com> | 2020-05-09 12:28:06 +0800 |
commit | 919982f29a45b76167e193edc5d097bafd62b9fd (patch) | |
tree | 39030b199eb014654ca1d87c23737ac6edf88d8e | |
parent | 39d89791eb3a19f8ac2053fe24c24a2711e33377 (diff) |
Mac compatibility for 'mktemp'
-rwxr-xr-x | gist | 15 |
1 files changed, 11 insertions, 4 deletions
@@ -79,6 +79,13 @@ tmp_dir=$(mktemp -d) | |||
79 | trap "[[ '$DEBUG' == 'true' ]] && find $tmp_dir -type f | xargs tail -n +1 > log; rm -r $tmp_dir" EXIT | 79 | trap "[[ '$DEBUG' == 'true' ]] && find $tmp_dir -type f | xargs tail -n +1 > log; rm -r $tmp_dir" EXIT |
80 | 80 | ||
81 | # Mac compatibility | 81 | # Mac compatibility |
82 | tmp_file() { | ||
83 | if [[ $(uname) == Darwin ]]; then | ||
84 | file=$(mktemp) && mv $(mktemp) $tmp_dir && echo $tmp_dir/$(basename $file) | ||
85 | else | ||
86 | mktemp -p $tmp_dir | ||
87 | fi | ||
88 | } | ||
82 | tac() { | 89 | tac() { |
83 | if [[ $(uname) == Darwin ]]; then | 90 | if [[ $(uname) == Darwin ]]; then |
84 | tail -r | 91 | tail -r |
@@ -124,7 +131,7 @@ http_method() { | |||
124 | [[ $METHOD =~ (POST|PATCH) ]] && data_opt="@$http_data" | 131 | [[ $METHOD =~ (POST|PATCH) ]] && data_opt="@$http_data" |
125 | http -b "$METHOD" "$@" "$header" "$data_opt" ;; | 132 | http -b "$METHOD" "$@" "$header" "$data_opt" ;; |
126 | esac 2>&1 \ | 133 | esac 2>&1 \ |
127 | | tee $(mktemp -p $tmp_dir) \ | 134 | | tee $(tmp_file) \ |
128 | || { echo "Error: no active internet connection" >&2; return 1; } | 135 | || { echo "Error: no active internet connection" >&2; return 1; } |
129 | } | 136 | } |
130 | 137 | ||
@@ -645,7 +652,7 @@ _set_gist() { | |||
645 | 652 | ||
646 | # Let user type the content of gist before setting filename | 653 | # Let user type the content of gist before setting filename |
647 | _new_file() { | 654 | _new_file() { |
648 | tmp_file=$(mktemp -p $tmp_dir) | 655 | tmp_file=$(tmp_file) |
649 | if [[ -z $INPUT ]]; then | 656 | if [[ -z $INPUT ]]; then |
650 | echo "Type a gist. <Ctrl-C> to cancel, <Ctrl-D> when done" > /dev/tty | 657 | echo "Type a gist. <Ctrl-C> to cancel, <Ctrl-D> when done" > /dev/tty |
651 | cat > "$tmp_file" | 658 | cat > "$tmp_file" |
@@ -680,7 +687,7 @@ _create_gist() { | |||
680 | [[ -z $description ]] && read -e -r -p 'Type description: ' description < /dev/tty | 687 | [[ -z $description ]] && read -e -r -p 'Type description: ' description < /dev/tty |
681 | 688 | ||
682 | echo 'Creating a new gist...' | 689 | echo 'Creating a new gist...' |
683 | http_data=$(mktemp -p $tmp_dir) | 690 | http_data=$(tmp_file) |
684 | echo -e "${files[*]}\n$description" \ | 691 | echo -e "${files[*]}\n$description" \ |
685 | | _gist_body > "$http_data" \ | 692 | | _gist_body > "$http_data" \ |
686 | && http_method POST $GITHUB_API/gists \ | 693 | && http_method POST $GITHUB_API/gists \ |
@@ -712,7 +719,7 @@ _edit_gist() { | |||
712 | DESC="$2" | 719 | DESC="$2" |
713 | fi | 720 | fi |
714 | 721 | ||
715 | http_data=$(mktemp -p $tmp_dir) | 722 | http_data=$(tmp_file) |
716 | echo '{' \"description\": \""${DESC//\"/\\\"}"\" '}' > "$http_data" | 723 | echo '{' \"description\": \""${DESC//\"/\\\"}"\" '}' > "$http_data" |
717 | new_record=$(http_method PATCH "$GITHUB_API/gists/$GIST_ID" \ | 724 | new_record=$(http_method PATCH "$GITHUB_API/gists/$GIST_ID" \ |
718 | | sed -e '1 s/^/[/; $ s/$/]/' \ | 725 | | sed -e '1 s/^/[/; $ s/$/]/' \ |