Last active
May 9, 2017 03:11
-
-
Save syusui-s/2259d681f2083897f559a7dad180ec18 to your computer and use it in GitHub Desktop.
Yo command
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/sh | |
OPTSTRING='hp:l:' | |
ACCESSTOKEN_BASENAME='.yoaccesstoken' | |
ACCESSTOKEN_PATH="${HOME}/${ACCESSTOKEN_BASENAME}" | |
error() { | |
echo "$*" > /dev/stderr | |
exit 1 | |
} | |
print_help() { | |
local app="$(basename "$0")" | |
cat << EOF | |
Usage: ${app} [options] <recipient> <text> | |
Just Yo. | |
Options: | |
-h display this help | |
-l <url> send a link | |
-p <pair> response pair | |
Example: | |
$ ${app} "YOFRIENDS" "Let's have a beer together" | |
EOF | |
} | |
check_token() { | |
if ! [ -f "${ACCESSTOKEN_PATH}" ]; then | |
cat << EOF | |
There is no '${ACCESSTOKEN_PATH}'. | |
You have to create a '${ACCESSTOKEN_BASENAME}' in home directory. | |
Get your Access Token at https://accesstoken.justyo.co/ and | |
write it into '${ACCESSTOKEN_BASENAME}'. | |
EOF | |
exit 1 | |
fi | |
return 0 | |
} | |
get_token() { | |
access_token="$(cat "${ACCESSTOKEN_PATH}" | tr -d '[[:space:]]')" | |
if [ $? -ne 0 ]; then exit 1; fi | |
} | |
### | |
check_token | |
get_token | |
while getopts "${OPTSTRING}" option; do | |
case ${option} in | |
h) | |
print_help | |
exit 1 | |
;; | |
l) | |
link="$2" | |
if ! [ $(echo -n "${text}" | wc -m) -le 30 ]; then | |
error "Too long text. A text length should be <=30." | |
fi | |
shift 2 | |
;; | |
p) | |
response_pair="$2" | |
if ! echo "$response_pair" | grep -q '^.*\..*$'; then | |
error "Response pair is invalid format." | |
fi | |
shift 2 | |
;; | |
\?) | |
exit 1 | |
;; | |
esac | |
done | |
recipient="$1" | |
text="`echo $2 | sed 's/[&?=]/\\\0/g'`" | |
if [ -z "${recipient}" ]; then | |
error "No user specified." | |
fi | |
# build data | |
data="access_token=${access_token}" | |
data="${data}\nusername=${recipient}" | |
if [ -n "${response_pair}" ]; then | |
data="${data}\nresponse_pair=${response_pair}" | |
fi | |
if [ -n "${link}" ]; then | |
data="${data}\nlink=${link}" | |
fi | |
if [ -n "${text}" ]; then | |
data="${data}\ntext=${text}" | |
fi | |
echo -e "${data}" | sed -e 's/^.*$/--data-urlencode "\0"/g' | xargs curl "https://api.justyo.co/yos/" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment