You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
113 lines
3.6 KiB
113 lines
3.6 KiB
#!/bin/sh |
|
# Bare Metal Alchemist, 2022 |
|
|
|
############################################# |
|
# Lead - ♄ # |
|
############################################# |
|
|
|
# The most basic ingredient in an Alchemy recipe, lead is used in all |
|
# recipes in this repository to standardize some simple things that I |
|
# rely on to make scripts concise and pleasant to use |
|
|
|
# --------------- Escape Codes --------------- |
|
# These constants are used to add color and text formatting to the |
|
# terminal's output -- Further reading: ANSI Escape Codes |
|
RED="\e[0;31m" |
|
GREEN="\e[0;32m" |
|
BLUE="\e[0;34m" |
|
BOLD="\e[1m" |
|
ULINE="\e[4m" |
|
RESET="\e[0m" |
|
|
|
# --------------- Traps --------------- |
|
|
|
# Traps, in the case of shell scripting, listen for certain signals |
|
# that are broadcast by the system and execute a command in response |
|
|
|
# We only want these to activate if we're running a recipe |
|
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 'echo -e "${RED}Oops...${RESET} Something went wrong on line $LINENO of this script. Exit code was $?" >&2' ERR |
|
fi |
|
|
|
|
|
# --------------- Environment Setup --------------- |
|
|
|
# If there's an env file, export it's contents to the environment |
|
if [ -f .env ]; then |
|
export $(grep -v '^#' .env | xargs) |
|
else |
|
if [ -z $ALCHEMY ]; then |
|
echo "No .env file, this might cause some issues..." |
|
fi |
|
fi |
|
|
|
|
|
# --------------- Functions --------------- |
|
|
|
# Coding Moment: generally, whenever you see something with brackets |
|
# at the end of it, like this() or like(this), it's a function! |
|
# It takes inputs and gives (or does) something as an output |
|
|
|
# Checks to see if we can use a command or not |
|
check_exists() { |
|
command -v "$1" >/dev/null |
|
} |
|
|
|
# This one installs utilities to your OS (If you need them!) |
|
install_if_needed() { |
|
for package in "$@" # $@ means "all the arguments you passed |
|
do |
|
case $DISTRO in |
|
"debian") |
|
# TODO Better installation detection than check_exists |
|
if [ -z $(check_exists $package) ]; then |
|
echo "installing" $package |
|
sudo apt install -y $package |
|
else |
|
echo $package 'already installed!' |
|
fi |
|
;; |
|
"arch") |
|
if pacman -Qi $package &>/dev/null; then |
|
echo $package 'already installed!' |
|
else |
|
echo "installing" $package |
|
sudo pacman -S $package --noconfirm --needed |
|
fi |
|
;; |
|
"fedora") |
|
# TODO Better installation detection than "check_exists" |
|
if [ -z $(check_exists $package) ]; then |
|
echo "installing" $package |
|
sudo dnf install -y $package |
|
else |
|
echo $package 'already installed!' |
|
fi |
|
;; |
|
"mac") |
|
# TODO Better installation detection than "check_exists" |
|
if [ -z $(check_exists $package) ]; then |
|
echo "installing" $package |
|
brew install $package |
|
else |
|
echo $package 'already installed!' |
|
fi |
|
;; |
|
esac |
|
done |
|
} |
|
|
|
# This one looks like gibberish because we're using regex, don't worry. |
|
# It takes values and stores them away in the env for later reference |
|
remember() { |
|
KEY=$(echo ${1} | cut -d'=' -f 1) |
|
if [[ ${1} =~ ^[A-Z_]+\=[A-Za-z0-9/._]*$ ]] && [[ -z $(env | grep ${KEY}) ]]; then |
|
echo "${1}" >> .env |
|
export ${1} |
|
fi |
|
} |
|
|
|
LEAD=1
|
|
|