-
Notifications
You must be signed in to change notification settings - Fork 0
/
generate
executable file
·63 lines (54 loc) · 1.58 KB
/
generate
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
53
54
55
56
57
58
59
60
61
62
63
#!/usr/bin/env bash
TARGET=$1
SLUG=$2
usage() {
echo
echo " evlfctry.pro — a simple URL shortener that doesn't try to do too much. also, serverless"
echo
echo " usage:"
echo
echo " ./generate <url to redirect> <slug>"
echo
echo " example:"
echo
echo " ./generate https://github.com/evilfactorylabs/gow gow -- will redirect /gow to https://github.com/evilfactorylabs/gow"
echo " ./generate https://github.com/evilfactorylabs/blog -- will redirect /<auto-generated> to https://github.com/evilfactorylabs/blog"
echo
echo " notes:"
echo " auto-generated slug only can be use if you have openssl(1) installed on your machine"
}
# if there is no argument, show usage
if [ -z "$TARGET" ]; then
usage
exit 1
fi
# if the argument is only 1
if [ -z "$SLUG" ]; then
# and openssl is not installed, show error message
if ! command -v openssl &> /dev/null; then
echo
echo " in order to use auto-generated slug, you need to have openssl(1) installed on your machine"
exit 1
fi
# otherwise, generate random string
SLUG=$(openssl rand -hex 3)
fi
# check is the slug exists?
if [ -f "links/$SLUG.md" ]; then
echo
echo "/$SLUG is already exist. please choose another"
echo
exit 1
fi
# create new file & the content
cat << EOF > links/$SLUG.md
---
target: $TARGET
---
EOF
# move that file to staging area
git add links/$SLUG.md
git commit -m "feat: create /$SLUG" -m "url target is $TARGET" > /dev/null 2>&1
# done
echo
echo " successfully creating redirect from /$SLUG to $TARGET"