# We only want these to activate if we're running a recipe, so we
# 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"
# 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
# Make sure that ctrl+C consistently exits the script
trap "exit" INT
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
fi
# --------------- Functions ---------------
# --------------- Functions ---------------
@ -121,14 +135,21 @@ source_env
check_for() {
check_for() {
command -v "$1" > /dev/null
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."
if [ "$(id -u)" -eq 0 ]; then
echo ""
say "${RED}Woah there!${RESET} Seems you're running this script as a superuser."
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"
say ""
echo ""
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
exit 1
fi
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!)
# This one installs utilities to your OS (If you need them!)
install_if_needed() {
install_if_needed() {
@ -137,11 +158,11 @@ install_if_needed() {
case $DISTRO in
case $DISTRO in
"debian")
"debian")
# TODO Better installation detection than check_for
# 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"
say "installing $package"
sudo apt install -y $package
sudo apt install -y $package
else
say "$package already installed!"
fi
fi
;;
;;
"arch")
"arch")
@ -195,7 +216,7 @@ forget() {
}
}
remember() {
remember() {
# Optionally choose to output somewhere other than Alchemy/
# Optionally choose to output somewhere other than Alchemy
if [ "${2}" = "to" ]; then
if [ "${2}" = "to" ]; then
ENV_LOCATION=${3}
ENV_LOCATION=${3}
else
else
@ -205,20 +226,21 @@ remember() {
KEY=$(say ${1} | cut -d'=' -f 1)
KEY=$(say ${1} | cut -d'=' -f 1)
VALUE=$(say ${1} | cut -d'=' -f 2)
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"
say "Keys must consist only of capital letters and underscores"
return 1
fi
fi
VALID_CHARS='A-Za-z0-9/_.:=-'
VALID_CHARS='A-Za-z0-9/_.:=-'
if [[ ! $VALUE =~ ^[$VALID_CHARS]+$ ]]; then
if say ${VALUE} | grep -Eqv "^[${VALID_CHARS}]+$"; then
say "illegal VALUE: $VALUE"
say "illegal VALUE: $VALUE"
say "for key $KEY"
say "for key $KEY"
say "Valid characters for env values: letters, numbers, \".\",\"/\",\"_\"",\":\", \"-\"
say "Valid characters for env values: letters, numbers, \".\",\"/\",\"_\"",\":\", \"-\"
fi
fi
# If we're setting a valid key/value pair
# 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