From 469c5345ac5c67cdc1ba87cd6cb4e3822cf58158 Mon Sep 17 00:00:00 2001 From: "Gal@Shefet" Date: Tue, 11 Jul 2023 23:17:21 -0400 Subject: [PATCH] Added 01_util.funcitons, fixed include --- 01_util.functions | 64 ++++++++++++++++++++++++++++++++++++++++++ delete-all-catalogs.sh | 2 +- setup-catalog.sh | 4 +-- shut-down-procedure.sh | 2 +- start-all-charts.sh | 2 +- start-chart.sh | 2 +- startup-procedure.sh | 2 +- stop-all-chrats.sh | 2 +- wait-for-idle-queue.sh | 2 +- 9 files changed, 73 insertions(+), 9 deletions(-) create mode 100644 01_util.functions diff --git a/01_util.functions b/01_util.functions new file mode 100644 index 0000000..7e8b20e --- /dev/null +++ b/01_util.functions @@ -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 [...]" \ + ) + ) + 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}" +} + diff --git a/delete-all-catalogs.sh b/delete-all-catalogs.sh index 6619e0f..47e5e3f 100755 --- a/delete-all-catalogs.sh +++ b/delete-all-catalogs.sh @@ -2,7 +2,7 @@ SCRIPT_DIR=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd ) USER_HOME=$HOME [[ -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 diff --git a/setup-catalog.sh b/setup-catalog.sh index d139c2d..ebd9361 100755 --- a/setup-catalog.sh +++ b/setup-catalog.sh @@ -2,7 +2,7 @@ SCRIPT_DIR=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd ) USER_HOME=$HOME [[ -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 @@ -13,7 +13,7 @@ setup_catalog() { "label": "truecharts", "repository": "https://github.com/truecharts/catalog", "branch": "main", - "preferred_trains": ["stable", "dependency", "enterprise"] + "preferred_trains": ["stable", "operators", "enterprise"] }' printf '\n' } diff --git a/shut-down-procedure.sh b/shut-down-procedure.sh index 34effc8..e59d5b2 100755 --- a/shut-down-procedure.sh +++ b/shut-down-procedure.sh @@ -2,7 +2,7 @@ SCRIPT_DIR=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd ) USER_HOME=$HOME [[ -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 diff --git a/start-all-charts.sh b/start-all-charts.sh index ca47a3f..45363f3 100755 --- a/start-all-charts.sh +++ b/start-all-charts.sh @@ -2,7 +2,7 @@ SCRIPT_DIR=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd ) USER_HOME=$HOME [[ -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 diff --git a/start-chart.sh b/start-chart.sh index 6045b51..5dd879b 100755 --- a/start-chart.sh +++ b/start-chart.sh @@ -2,7 +2,7 @@ SCRIPT_DIR=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd ) USER_HOME=$HOME [[ -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 diff --git a/startup-procedure.sh b/startup-procedure.sh index 7409e95..4063e63 100755 --- a/startup-procedure.sh +++ b/startup-procedure.sh @@ -2,7 +2,7 @@ SCRIPT_DIR=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd ) USER_HOME=$HOME [[ -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 diff --git a/stop-all-chrats.sh b/stop-all-chrats.sh index b92793b..541c39a 100755 --- a/stop-all-chrats.sh +++ b/stop-all-chrats.sh @@ -2,7 +2,7 @@ SCRIPT_DIR=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd ) USER_HOME=$HOME [[ -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 diff --git a/wait-for-idle-queue.sh b/wait-for-idle-queue.sh index a8954f1..5ba12a2 100755 --- a/wait-for-idle-queue.sh +++ b/wait-for-idle-queue.sh @@ -2,7 +2,7 @@ SCRIPT_DIR=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd ) USER_HOME=$HOME [[ -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