mirror of
https://github.com/hackerschoice/segfault.git
synced 2024-06-29 18:21:22 +00:00
123 lines
2.1 KiB
Bash
123 lines
2.1 KiB
Bash
#! /usr/bin/env bash
|
|
|
|
# shellcheck disable=SC2034 # unused variable warning for ansi colors
|
|
|
|
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
|
|
|
|
CBG="\e[42;1m" # Background Green
|
|
|
|
# night-mode
|
|
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
|
|
|
|
# Clear from cursor to end of line
|
|
CL="\e[0K"
|
|
|
|
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"
|
|
# shellcheck disable=SC2181 #(style): Check exit code directly with e.g
|
|
[[ $? -ne 0 ]] && code="$?"
|
|
: "${code:=99}"
|
|
|
|
shift 1
|
|
[[ -n $1 ]] && echo -e >&2 "${CR}ERROR:${CN} $*"
|
|
|
|
exit "$code"
|
|
}
|
|
|
|
WARN()
|
|
{
|
|
local code
|
|
code="$1"
|
|
: "${code:=255}"
|
|
|
|
shift 1
|
|
echo -e >&2 "${CY}WARNING(${code}):${CN} $*"
|
|
}
|
|
|
|
INFO()
|
|
{
|
|
echo -e >&2 "--> ${CM}$*${CN}"
|
|
}
|
|
|
|
|
|
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
|
|
}
|
|
|
|
MD5F ()
|
|
{
|
|
local f;
|
|
local res;
|
|
f="$1";
|
|
[[ ! -f "$f" ]] && return;
|
|
res=$(md5sum "$f");
|
|
echo "${res%% *}"
|
|
}
|
|
|
|
# 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 IFS
|
|
local arr
|
|
local mode
|
|
file="$1"
|
|
mode="${2,,}"
|
|
|
|
[[ ! -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
|
|
|
|
}
|
|
|