# We only want these to activate if we're running a recipe, so we
# check to see if we're running a "shell inside of a shell"
if [[ $SHLVL -gt 1 ]]; then
if [ $SHLVL -gt 1 ]; then
# Make sure that ctrl+C consistently exits the script
trap "exit" INT
# Give informative error messages when we receive ERR
trap 'say "${RED}Oops...${RESET} Something went wrong on line $LINENO of this script. Exit code was $?" >&2' ERR
# Give informative error messages when we receive unguarded EXIT codes
set -e
handle_exit() {
case $? in
0)
exit
;;
*)
printf "${RED}Oops...${RESET} Something went wrong on line ${LINENO} of this script. Exit code was $?\n"
;;
esac
}
trap handle_exit EXIT
fi
# --------------- Functions ---------------
@ -121,14 +135,21 @@ source_env
check_for() {
command -v "$1" > /dev/null
}
if [ "$EUID" -eq 0 ]; then
echo -e "${RED}Woah there!${RESET} Seems you're running this script as a superuser."
echo ""
echo "That might cause some issues with permissions and whatnot. Run this script as your default user (without sudo) and I'll ask you when I need superuser permissions"
echo ""
if [ "$(id -u)" -eq 0 ]; then
say "${RED}Woah there!${RESET} Seems you're running this script as a superuser."
say ""
say "That might cause some issues with permissions and whatnot. Run this script as your default user (without sudo) and I'll ask you when I need superuser permissions"
say ""
exit 1
fi
# I like using source as a keyword!
if ! check_for source; then
source() {
. ${1}
}
fi
# This one installs utilities to your OS (If you need them!)
install_if_needed() {
@ -137,11 +158,11 @@ install_if_needed() {
case $DISTRO in
"debian")
# TODO Better installation detection than check_for
if [ -z $(check_for $package) ]; then
if dpkg -l | grep -q "$package"; then
say "$package already installed!"
else
say "installing $package"
sudo apt install -y $package
else
say "$package already installed!"
fi
;;
"arch")
@ -195,7 +216,7 @@ forget() {
}
remember() {
# Optionally choose to output somewhere other than Alchemy/
# Optionally choose to output somewhere other than Alchemy
if [ "${2}" = "to" ]; then
ENV_LOCATION=${3}
else
@ -205,20 +226,21 @@ remember() {
KEY=$(say ${1} | cut -d'=' -f 1)
VALUE=$(say ${1} | cut -d'=' -f 2)
if [[ ! $KEY =~ ^[A-Z_]+$ ]]; then
if say ${KEY} | grep -Eqv "^[A-Z_]+$"; then
say "Keys must consist only of capital letters and underscores"
return 1
fi
VALID_CHARS='A-Za-z0-9/_.:=-'
if [[ ! $VALUE =~ ^[$VALID_CHARS]+$ ]]; then
if say ${VALUE} | grep -Eqv "^[${VALID_CHARS}]+$"; then
say "illegal VALUE: $VALUE"
say "for key $KEY"
say "Valid characters for env values: letters, numbers, \".\",\"/\",\"_\"",\":\", \"-\"
fi
# If we're setting a valid key/value pair
if [[ ${1} =~ ^[A-Z_]+\=[$VALID_CHARS]+$ ]]; then
if say ${1} | grep -Eq "^[A-Z_]+\=[$VALID_CHARS]+$"; then