Added 01_util.funcitons, fixed include

This commit is contained in:
Gal@Shefet 2023-07-11 23:17:21 -04:00
parent 8320cec425
commit 469c5345ac
9 changed files with 73 additions and 9 deletions

64
01_util.functions Normal file
View File

@ -0,0 +1,64 @@
ANSI_Print() { printf "\E[${ANSI_Code}m%s\E[0m" "$@"; }
ANSI_Red() { ANSI_Code=31 ANSI_Print "$@"; }
ANSI_Green() { ANSI_Code=32 ANSI_Print "$@"; }
ANSI_Yellow() { ANSI_Code=33 ANSI_Print "$@"; }
ANSI_Blue() { ANSI_Code=34 ANSI_Print "$@"; }
ANSI_Indiego() { ANSI_Code=35 ANSI_Print "$@"; }
ANSI_Cyan() { ANSI_Code=36 ANSI_Print "$@"; }
warn() {
(>&2 printf '%s %s\n' $(ANSI_Yellow 'WARNING:') "$(echo "$@")" )
}
# The way to call this is:
# eval $(error 1 "message") || exit $?
# where 1 is the return/exit code and "message" is the message to be
# displayed
error() {
printf "(>&2 \
printf '%%s %%b' '$(ANSI_Red 'ERROR:')' '$(printf "%b " "${@:2}")') \
&& echo '' && return ${1} 2>/dev/null || exit ${1}"
}
require_sudo() {
if [[ $EUID -ne 0 && -z $(printf "%s\n" $(id --groups --name) | grep '^sudo$') ]]; then
eval $(error 1 "This script should be run by a sudo memeber or as the root user")
fi
}
require_root() {
if [[ $EUID -ne 0 ]]; then
eval $(error 1 "This script should only be run using sudo or as the root user")
fi
}
recommend_root() {
if [[ $EUID -ne 0 ]]; then
warn "This script works better using sudo or as the root user"
return 3
fi
}
function setacl() {
if [[ $# -lt 4 ]]; then
eval $(error 1 \
"setacl missing arguments, called with '$1' ${@:2}" \
$(printf "%s\n" \
"Usage:" \
"\tsetacl <Options> <Ownership> <Permissions> <Path> [<Additional Paths>...]" \
)
)
return 1
fi
local Options=${1}
local Ownership=${2}
local Permissions=${3}
[[ -n "${SZ_DEBUG}" ]] \
&& echo "chown $Options $Ownership \"${@:4}\""
chown $Options $Ownership "${@:4}"
[[ -n "${SZ_DEBUG}" ]] \
&& echo "chmod $Options $Permissions \"${@:4}\""
chmod $Options $Permissions "${@:4}"
}

View File

@ -2,7 +2,7 @@
SCRIPT_DIR=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd ) SCRIPT_DIR=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )
USER_HOME=$HOME USER_HOME=$HOME
[[ -n "${SUDO_USER}" ]] && USER_HOME="$(eval "echo ~${SUDO_USER}")" [[ -n "${SUDO_USER}" ]] && USER_HOME="$(eval "echo ~${SUDO_USER}")"
. ${SHRC_D:-$USER_HOME/.sz.shrc.d}/01_util.functions . ${SHRC_D:-$SCRIPT_DIR}/01_util.functions
set -e set -e

View File

@ -2,7 +2,7 @@
SCRIPT_DIR=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd ) SCRIPT_DIR=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )
USER_HOME=$HOME USER_HOME=$HOME
[[ -n "${SUDO_USER}" ]] && USER_HOME="$(eval "echo ~${SUDO_USER}")" [[ -n "${SUDO_USER}" ]] && USER_HOME="$(eval "echo ~${SUDO_USER}")"
. ${SHRC_D:-$USER_HOME/.sz.shrc.d}/01_util.functions . ${SHRC_D:-$SCRIPT_DIR}/01_util.functions
set -e set -e
@ -13,7 +13,7 @@ setup_catalog() {
"label": "truecharts", "label": "truecharts",
"repository": "https://github.com/truecharts/catalog", "repository": "https://github.com/truecharts/catalog",
"branch": "main", "branch": "main",
"preferred_trains": ["stable", "dependency", "enterprise"] "preferred_trains": ["stable", "operators", "enterprise"]
}' }'
printf '\n' printf '\n'
} }

View File

@ -2,7 +2,7 @@
SCRIPT_DIR=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd ) SCRIPT_DIR=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )
USER_HOME=$HOME USER_HOME=$HOME
[[ -n "${SUDO_USER}" ]] && USER_HOME="$(eval "echo ~${SUDO_USER}")" [[ -n "${SUDO_USER}" ]] && USER_HOME="$(eval "echo ~${SUDO_USER}")"
. ${SHRC_D:-$USER_HOME/.sz.shrc.d}/01_util.functions . ${SHRC_D:-$SCRIPT_DIR}/01_util.functions
set -e set -e

View File

@ -2,7 +2,7 @@
SCRIPT_DIR=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd ) SCRIPT_DIR=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )
USER_HOME=$HOME USER_HOME=$HOME
[[ -n "${SUDO_USER}" ]] && USER_HOME="$(eval "echo ~${SUDO_USER}")" [[ -n "${SUDO_USER}" ]] && USER_HOME="$(eval "echo ~${SUDO_USER}")"
. ${SHRC_D:-$USER_HOME/.sz.shrc.d}/01_util.functions . ${SHRC_D:-$SCRIPT_DIR}/01_util.functions
set -e set -e

View File

@ -2,7 +2,7 @@
SCRIPT_DIR=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd ) SCRIPT_DIR=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )
USER_HOME=$HOME USER_HOME=$HOME
[[ -n "${SUDO_USER}" ]] && USER_HOME="$(eval "echo ~${SUDO_USER}")" [[ -n "${SUDO_USER}" ]] && USER_HOME="$(eval "echo ~${SUDO_USER}")"
. ${SHRC_D:-$USER_HOME/.sz.shrc.d}/01_util.functions . ${SHRC_D:-$SCRIPT_DIR}/01_util.functions
set -e set -e

View File

@ -2,7 +2,7 @@
SCRIPT_DIR=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd ) SCRIPT_DIR=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )
USER_HOME=$HOME USER_HOME=$HOME
[[ -n "${SUDO_USER}" ]] && USER_HOME="$(eval "echo ~${SUDO_USER}")" [[ -n "${SUDO_USER}" ]] && USER_HOME="$(eval "echo ~${SUDO_USER}")"
. ${SHRC_D:-$USER_HOME/.sz.shrc.d}/01_util.functions . ${SHRC_D:-$SCRIPT_DIR}/01_util.functions
set -e set -e

View File

@ -2,7 +2,7 @@
SCRIPT_DIR=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd ) SCRIPT_DIR=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )
USER_HOME=$HOME USER_HOME=$HOME
[[ -n "${SUDO_USER}" ]] && USER_HOME="$(eval "echo ~${SUDO_USER}")" [[ -n "${SUDO_USER}" ]] && USER_HOME="$(eval "echo ~${SUDO_USER}")"
. ${SHRC_D:-$USER_HOME/.sz.shrc.d}/01_util.functions . ${SHRC_D:-$SCRIPT_DIR}/01_util.functions
set -e set -e

View File

@ -2,7 +2,7 @@
SCRIPT_DIR=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd ) SCRIPT_DIR=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )
USER_HOME=$HOME USER_HOME=$HOME
[[ -n "${SUDO_USER}" ]] && USER_HOME="$(eval "echo ~${SUDO_USER}")" [[ -n "${SUDO_USER}" ]] && USER_HOME="$(eval "echo ~${SUDO_USER}")"
. ${SHRC_D:-$USER_HOME/.sz.shrc.d}/01_util.functions . ${SHRC_D:-$SCRIPT_DIR}/01_util.functions
set -e set -e