-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathgithub_short_url.sh
More file actions
executable file
·52 lines (45 loc) · 1.83 KB
/
github_short_url.sh
File metadata and controls
executable file
·52 lines (45 loc) · 1.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#!/bin/bash
# github_short_url.sh - create a short github url
# HOWTO: https://github.blog/2011-11-10-git-io-github-url-shortener/
if [ $# -lt 1 ] || [ $# -gt 2 ]; then
echo "ERROR: You must supply a github url to shorten"
exit 1
fi
short=()
url="$1"
vanity="$2"
shortener="https://git.io"
if ! [[ "$url" =~ (github.(com|blog)|githubusercontent.com) ]]; then
echo "ERROR: Only github.com URLs are allowed!"
exit 1
else
if [ ! -z ${vanity+x} ]; then
# vanity url was requested
if [[ $OSTYPE =~ "darwin" ]]; then
# work around for ancient version of bash on macOS
while IFS= read -r line; do
short+=( "$line" )
done < <( curl -si "$shortener" -H "User-Agent: curl/7.58.0" -F "url=${url}" -F "code=${vanity}" | grep -E "(Status|Location): " )
else
# mapfile, only for bash v4.0+
mapfile -t short < <( curl -i "$shortener" -H "User-Agent: curl/7.58.0" -F "url=${url}" -F "code=${vanity}" | grep -E "(Status|Location): " )
fi
else
if [[ $OSTYPE =~ "darwin" ]]; then
# work around for ancient version of bash on macOS
while IFS= read -r line; do
short+=( "$line" )
done < <( curl -si "$shortener" -H "User-Agent= curl/7.58.0" -F "url=${url}" | grep -E "(Status|Location): " )
else
# mapfile, only for bash v4.0+
mapfile -t short < <( curl -H "User-Agent= curl/7.58.0" -i "$shortener" -F "url=${url}" | grep -E "(Status|Location): " )
fi
fi
if [[ ${short[0]} =~ "201" ]]; then
echo "Link created: $(echo "${short[1]}" | awk '{print $2}')"
else
# echo "ERROR: Link creation failed! Code $(echo ${short[0]} | sed 's|Status: ||g')"
echo "ERROR: Link creation failed! Code ${short[0]//Status: /}"
fi
fi
#EOF