Skip to content

Instantly share code, notes, and snippets.

@lilactown
Last active February 28, 2017 03:49
Show Gist options
  • Save lilactown/326f5b0d17335349fbd511bc61861411 to your computer and use it in GitHub Desktop.
Save lilactown/326f5b0d17335349fbd511bc61861411 to your computer and use it in GitHub Desktop.
A script to download an Erlang program from Gist/Hastebin and open a REPL with it compiled and loaded
#!/bin/bash
##########################
# Description:
# Downloads an Erlang program from a Gist or Hastebin, then opens an Erlang shell with it
# compiled and loaded.
#
# Installation:
# Download and install this script in /usr/local/bin
#
# Usage:
# erl-get https://gist.github.com/somebodys/gist-thing-url
#
ERL_GET_DIR="/tmp/erl-get"
re="-module\((.+)\)"
if ! [ -d ${ERL_GET_DIR} ]; then
mkdir ${ERL_GET_DIR}
fi
if [[ $1 =~ gist.github.com ]]; then
# is gist
URL="$1/raw/";
elif [[ $1 =~ hastebin.com/(.+) ]]; then
# is hastebin
URL="https://hastebin.com/raw/${BASH_REMATCH[1]}";
else
echo "Invalid Gist/Hastebin URL"
echo "erl-get parses the URL you enter to obtain the raw code from the gist/hastebin"
echo ""
echo "Usage:"
echo " erl-get <URL to Hastebin/Gist>"j
exit 1;
fi
printf "== Downloading ${URL}\n"
(cd ${ERL_GET_DIR} && curl -Lso tmp.erl ${URL})
LINE=`cat ${ERL_GET_DIR}/tmp.erl | grep -- -module`
if [[ $LINE =~ $re ]]; then
MODULE_NAME="${BASH_REMATCH[1]}";
printf "== Module name: '${MODULE_NAME}'\n== Type 'l(${MODULE_NAME}).' in the shell to access it\n";
FILE_NAME="${ERL_GET_DIR}/${MODULE_NAME}.erl"
mv ${ERL_GET_DIR}/tmp.erl $FILE_NAME;
erlc -o ${ERL_GET_DIR} ${FILE_NAME} && erl -pa ${ERL_GET_DIR}
else
echo "Could not parse Erlang module name\n" >&2
fi
@lilactown
Copy link
Author

lilactown commented Feb 28, 2017

Installation

Tested on macOS 10.10.5:

curl -Lo /usr/local/bin/erl-get https://gist.githubusercontent.com/Lokeh/326f5b0d17335349fbd511bc61861411/raw/ && chmod +x /usr/local/bin/erl-get

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment