segfault/provision/system/funcs

125 lines
2.2 KiB
Plaintext
Raw Normal View History

2022-05-10 15:39:52 +00:00
#! /usr/bin/env bash
# shellcheck disable=SC2034 # unused variable warning for ansi colors
2022-08-01 10:04:04 +00:00
CY="\e[1;33m" # yellow
CG="\e[1;32m" # green
CR="\e[1;31m" # red
CC="\e[1;36m" # cyan
CM="\e[1;35m" # magenta
CW="\e[1;37m" # magenta
CF="\e[2m" # faint
CN="\e[0m" # none
2022-05-10 15:39:52 +00:00
2022-08-01 10:04:04 +00:00
CBG="\e[42;1m" # Background Green
2022-05-10 15:39:52 +00:00
# night-mode
2022-08-01 10:04:04 +00:00
CDY="\e[0;33m" # yellow
CDG="\e[0;32m" # green
CDR="\e[0;31m" # red
CDC="\e[0;36m" # cyan
CDM="\e[0;35m" # magenta
2022-05-10 15:39:52 +00:00
# Clear from cursor to end of line
2022-08-01 10:04:04 +00:00
CL="\e[0K"
2022-05-10 15:39:52 +00:00
if [[ -z $SF_DEBUG ]]; then
DEBUGF(){ :;}
DEBUGF_R(){ :;}
else
DEBUGF(){ echo -e "${CY}DEBUG:${CN} $*";}
DEBUGF_R(){ echo -e "${CY}DEBUG:${CN} ${CR}$*${CN}";}
fi
ERREXIT()
{
local code
code="$1"
2022-07-25 12:42:33 +00:00
# shellcheck disable=SC2181 #(style): Check exit code directly with e.g
2022-05-10 15:39:52 +00:00
[[ $? -ne 0 ]] && code="$?"
[[ -z $code ]] && code=99
shift 1
[[ -n $1 ]] && echo -e >&2 "${CR}ERROR:${CN} $*"
exit "$code"
}
WARN()
{
local code
code="$1"
[[ -z $code ]] && code=255
shift 1
echo -e >&2 "${CY}WARNING(${code}):${CN} $*"
}
INFO()
{
2022-05-19 11:17:21 +00:00
echo -e >&2 "--> ${CM}$*${CN}"
2022-05-10 15:39:52 +00:00
}
NEED_ROOT()
{
[[ "$(id -u)" -ne 0 ]] && ERREXIT 255 "Error: Run this scrpt as root"
}
IS_APT_INSTALLED()
{
[[ "$(apt -qq list "$*" 2>/dev/null)" = *"[installed]" ]] && return 0 || return 1
2022-11-28 14:36:13 +00:00
}
MD5F ()
{
local f;
local res;
f="$1";
[[ ! -f "$f" ]] && return;
res=$(md5sum "$f");
echo "${res%% *}"
}
2022-11-29 10:26:12 +00:00
# Load .env file into bash variables.
# .env variables are not quotes. Thus we can not use 'source .env'
# in case a variable value contains a whitespace. (dare you, team docker!).
# Instead load & parse them one by one and run through eval to set CFG_
ENV_LOAD()
{
local file
local old_ifs
local arr
local mode
file="$1"
mode="${2,,}"
old_ifs="$IFS"
[[ ! -f "$file" ]] && return 255
IFS=$'\n'
arr=($(<"${file}"))
local i
local name
local val
i=0
# Note '.env' values are not 'quoted' but bash need them quoted "strings".
while [[ $i -lt ${#arr[@]} ]]; do
str=${arr[$i]}
((i++))
[[ "${str:0:3}" != 'SF_' ]] && continue
name="${str%%=*}"
[[ $mode == "add" ]] && {
eval '[[ -n $'"$name"' ]]' && continue
}
val="${str#*=}"
# Escape ' with '"'"'
val=${val//\'/\'\"\'\"\'}
eval "$name"=\'${val}\'
done
IFS="$old_ifs"
}