From b1900495cb5d9bb628b1cd2b65b62137a1b93391 Mon Sep 17 00:00:00 2001 From: Zen Date: Thu, 3 Mar 2022 13:45:33 -0600 Subject: [PATCH] added more utility commands to lead (say, ask_for, remember, forget) --- ingredients/gold | 2 +- ingredients/iron | 2 +- ingredients/lead | 86 ++++++++++++++++++++++++++++++------------------ ingredients/tin | 6 ++-- recipes/ao.sh | 4 +-- 5 files changed, 61 insertions(+), 39 deletions(-) diff --git a/ingredients/gold b/ingredients/gold index ba52f7d..59cd2ff 100644 --- a/ingredients/gold +++ b/ingredients/gold @@ -140,7 +140,7 @@ configure_lightning() { read clboss_enable case $clboss_enable in "y" | "Y") - if ! check_exists clboss; then + if ! check_for clboss; then install_clboss fi sed -i "s/#plugin/plugin/" $HOME/.lightning/config diff --git a/ingredients/iron b/ingredients/iron index f0f6179..891ac14 100644 --- a/ingredients/iron +++ b/ingredients/iron @@ -21,7 +21,7 @@ if [ -d $NVM_DIR ]; then fi set_node_to() { - if check_exists nvm; then + if check_for nvm; then if [ ! $(node -v) = $1 ]; then nvm install $1 nvm alias default $1 diff --git a/ingredients/lead b/ingredients/lead index c574a0a..336b6ca 100644 --- a/ingredients/lead +++ b/ingredients/lead @@ -16,6 +16,7 @@ RED="\e[0;31m" GREEN="\e[0;32m" BLUE="\e[0;34m" BOLD="\e[1m" +ITALIC="\e[3m" ULINE="\e[4m" RESET="\e[0m" @@ -56,7 +57,8 @@ RESET="\e[0m" # 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 +# 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 # Make sure that ctrl+C consistently exits the script trap "exit" INT @@ -64,6 +66,37 @@ if [[ $SHLVL -gt 1 ]]; then trap 'say "${RED}Oops...${RESET} Something went wrong on line $LINENO of this script. Exit code was $?" >&2' ERR 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 + +# --------------- Input/Output --------------- + +# 'say' is a simple function that imitates "echo" +# This needs to be built out way more! Replace echo for POSIX compatibility +say() { + printf "%b\n" "${1}" +} + +ask_for() { + if [ ${#} -eq 0 ]; then + say "To use this command, you need to pass the variable you want," + say "and then add as string of text to ask for it if you want. Example:\n" + say "ask_for RESPONSE \"Could you give me a RESPONSE please?\"" + say "" + say "Afterwards, you'll be able to use \$RESPONSE as a variable," + say "and ${ITALIC}say \$RESPONSE${RESET} will respond with your entry" + return 0 + fi + + if [ -n "${2}" ]; then + printf "%b" "${2}" + fi + + read ${1} +} # --------------- Environment Setup --------------- @@ -82,20 +115,10 @@ source_env() { # This one takes no arguments (words after the function name source_env -# --------------- 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 - -# 'say' is a simple function that imitates "echo" -# This needs to be built out way more! Replace echo for POSIX compatibility -say() { - printf "%b\n" "${1}" -} +# --------------- Program Installation --------------- # Checks to see if we can use a command or not -check_exists() { +check_for() { command -v "$1" >/dev/null } @@ -105,8 +128,8 @@ install_if_needed() { do case $DISTRO in "debian") - # TODO Better installation detection than check_exists - if [ -z $(check_exists $package) ]; then + # TODO Better installation detection than check_for + if [ -z $(check_for $package) ]; then say "installing $package" sudo apt install -y $package else @@ -122,8 +145,8 @@ install_if_needed() { fi ;; "fedora") - # TODO Better installation detection than "check_exists" - if [ -z $(check_exists $package) ]; then + # TODO Better installation detection than "check_for" + if [ -z $(check_for $package) ]; then say "installing $package" sudo dnf install -y $package else @@ -131,8 +154,8 @@ install_if_needed() { fi ;; "mac") - # TODO Better installation detection than "check_exists" - if [ -z $(check_exists $package) ]; then + # TODO Better installation detection than "check_for" + if [ -z $(check_for $package) ]; then say "installing $package" brew install $package else @@ -164,15 +187,15 @@ forget() { } remember() { - KEY=$(echo ${1} | cut -d'=' -f 1) - VALUE=$(echo ${1} | cut -d'=' -f 2) + KEY=$(say ${1} | cut -d'=' -f 1) + VALUE=$(say ${1} | cut -d'=' -f 2) if [[ ! $KEY =~ ^[A-Z_]+$ ]]; then - echo "Keys must consist only of capital letters and underscores" + say "Keys must consist only of capital letters and underscores" fi if [[ ! $VALUE =~ ^[A-Za-z0-9/_.:]+$ ]]; then - echo "Valid characters for env values: letters, numbers, \".\",\"/\",\"_\"",\":\" + say "Valid characters for env values: letters, numbers, \".\",\"/\",\"_\"",\":\" fi # If we're setting a valid key/value pair @@ -180,14 +203,13 @@ remember() { DOTENV_ENTRY=$(cat .env | grep ${KEY}) # If something already exists and we're trying to set it to something new - if [ -n "$DOTENV_ENTRY" ] && [[ ! $DOTENV_ENTRY = $1 ]]; then - echo -e "I'm trying to remember ${BLUE}${1}${RESET}, but..." - echo "" - echo -e "${BLUE}${DOTENV_ENTRY}${RESET}" - echo -e "This has already been defined in the .env file!" - echo "" - echo -en "would you like to overwrite it? ${BLUE}(y/n)${RESET} " - read overwrite + if [ -n "$DOTENV_ENTRY" ] && [ "$DOTENV_ENTRY" != "$1" ]; then + say "I'm trying to remember ${BLUE}${1}${RESET}, but..." + say "" + say "${BLUE}${DOTENV_ENTRY}${RESET}" + say "This has already been defined in the .env file!" + say "" + ask_for overwrite "would you like to overwrite it? ${BLUE}(y/n)${RESET} " case $overwrite in "y" | "Y") unset ${KEY} @@ -196,7 +218,7 @@ remember() { export ${1} ;; esac - else + elif [ -z "$DOTENV_ENTRY" ]; then unset ${KEY} echo "${1}" >> .env export ${1} diff --git a/ingredients/tin b/ingredients/tin index 21550b6..95486c3 100644 --- a/ingredients/tin +++ b/ingredients/tin @@ -14,9 +14,9 @@ sha256_check() { # Args: # - if check_exists sha256sum; then + if check_for sha256sum; then echo "${1} ${2}" | sha256sum -c - elif check_exists sha256; then + elif check_for sha256; then if [ "$(uname)" = "FreeBSD" ]; then sha256 -c "${1}" "${2}" else @@ -35,7 +35,7 @@ http_get() { # if [ -f "${2}" ]; then echo "File ${2} already exists; not downloading again" - elif check_exists curl; then + elif check_for curl; then curl --insecure --retry 5 "${1}" -o "${2}" else wget --no-check-certificate "${1}" -O "${2}" diff --git a/recipes/ao.sh b/recipes/ao.sh index bf52ab4..42132d2 100755 --- a/recipes/ao.sh +++ b/recipes/ao.sh @@ -131,12 +131,12 @@ if [ $AO = "3" ] || [ $AO = 'react' ]; then echo -e "${BOLD}Installing Bitcoin Ecosystem${RESET}" echo "" - if ! check_exists bitcoind; then + if ! check_for bitcoind; then echo -e "Building bitcoind from source... might take a while!" install_bitcoin fi - if ! check_exists lightningd; then + if ! check_for lightningd; then echo -e "Building lightningd from source... here we go again" install_lightning fi