Skip to content

Commit cbe57f9

Browse files
author
Cerem Cem ASLAN
committed
fixes #2
1 parent 93a3303 commit cbe57f9

File tree

1 file changed

+25
-16
lines changed

1 file changed

+25
-16
lines changed

create-gist.sh

Lines changed: 25 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
#!/bin/bash
22
# based on: https://gist.github.com/s-leroux/7cb7424d33ba3753e907cc2553bcd1ba
33
# modified by: cca
4-
4+
set -u -o pipefail
5+
set_dir(){ _dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"; }; set_dir
6+
safe_source () { source $1; set_dir; }
7+
# end of bash boilerplate
58

69
print_usage(){
710
cat << USAGE
@@ -18,13 +21,13 @@ USAGE
1821
}
1922

2023
# 0. Your file name
21-
FNAME=$1
24+
FNAME=${1:-}
2225
if [[ -f $FNAME ]]; then
2326
CONTENT=$(cat $FNAME)
24-
GITHUB_USERNAME=$2
27+
GITHUB_USERNAME=${2:-}
2528
else
2629
CONTENT=$(timeout 2 cat -)
27-
GITHUB_USERNAME=$1
30+
GITHUB_USERNAME=${1-}
2831
FNAME="stdin"
2932
if [[ "$CONTENT" == "" ]]; then
3033
print_usage
@@ -38,12 +41,14 @@ fi
3841
# Replace tabs by \t
3942
# Replace " by \"
4043
# Replace EOL by \n
41-
CONTENT=$(echo "${CONTENT}" | sed -e 's/\r//' -e's/\t/\\t/g' -e 's/"/\\"/g' | awk '{ printf($0 "\\n") }')
44+
CONTENT=$(echo "${CONTENT}" | sed -e 's/\\/\\\\/g' -e 's/\r//' -e's/\t/\\t/g' -e 's/"/\\"/g' | awk '{ printf($0 "\\n") }')
4245

4346
read -p "Give a description: " DESCRIPTION
4447

4548
# 2. Build the JSON request
46-
read -r -d '' DESC <<EOF
49+
tmp_file=$(mktemp)
50+
51+
cat > $tmp_file <<EOF
4752
{
4853
"description": "$DESCRIPTION",
4954
"public": true,
@@ -56,18 +61,22 @@ read -r -d '' DESC <<EOF
5661
EOF
5762

5863
# 3. Use curl to send a POST request
59-
6064
if [[ "$GITHUB_USERNAME" != "" ]]; then
6165
# REGISTERED USER
62-
OUTPUT=$(curl -u "${GITHUB_USERNAME}" -X POST -d "${DESC}" "https://api.github.com/gists")
63-
else
64-
# ANONYMOUS GIST :
65-
OUTPUT=$(curl -X POST -d "${DESC}" "https://api.github.com/gists")
66+
USER_PARAM="-u ${GITHUB_USERNAME}"
6667
fi
6768

68-
echo "$OUTPUT"
69+
OUTPUT=$(curl ${USER_PARAM:-} -X POST -d @$tmp_file "https://api.github.com/gists")
70+
uploaded_url=$(echo "$OUTPUT" | grep 'html_url' | grep 'gist')
71+
72+
if [[ ! -z ${uploaded_url:-} ]]; then
73+
echo "URL: "
74+
echo "-----------------"
75+
echo $uploaded_url
76+
else
77+
echo "---------------- ERROR -----------------------"
78+
echo "$OUTPUT"
79+
echo "-----------------"
80+
fi
6981

70-
echo "-----------------"
71-
echo " URL: "
72-
echo "-----------------"
73-
echo "$OUTPUT" | grep 'html_url' | grep 'gist'
82+
rm $tmp_file

0 commit comments

Comments
 (0)