Files
Varaverk/common.sh
T
2026-03-24 01:33:40 +00:00

80 lines
2.6 KiB
Bash

#!/bin/bash
#-----------------------------------------------------------------------------------------------
# --------------------- Common Shared Functions for Unraid Scripts -----------------------------
#-----------------------------------------------------------------------------------------------
#----------------- User Variables, Please adjust in Master.conf as needed ----------------------
#-----------------------------------------------------------------------------------------------
#
# Nothing to adjust here, all user variables should be in Master.conf
#
# This is just a script that has all common functions and logic that are shared
# between the different scripts.
#
# This way we can avoid code duplication and have a single source
# of truth for all common functions and logic.
#-----------------------------------------------------------------------------------------------
#---------------End Of User Variables, Please adjust in Master.conf as needed ------------------
#-----------------------------------------------------------------------------------------------
# Load Master.conf
load_master_conf() {
local script_dir
script_dir="$(cd "$(dirname "${BASH_SOURCE[1]}")" && pwd)"
local master_conf="$script_dir/../Master.conf"
if [ -f "$master_conf" ]; then
source "$master_conf"
echo "Loaded Master.conf from $master_conf"
else
echo "Error: Master.conf not found at $master_conf"
exit 1
fi
}
# Parse CLI args
parse_args() {
DRY_RUN=${DRY_RUN:-false}
for ARG in "$@"; do
if [[ "$ARG" == *=* ]]; then
local VAR_NAME="${ARG%%=*}"
local VAR_VALUE="${ARG#*=}"
if declare -p "$VAR_NAME" &>/dev/null; then
printf -v "$VAR_NAME" '%s' "$VAR_VALUE"
echo "Overriding $VAR_NAME -> $VAR_VALUE"
else
echo "Warning: Unknown variable $VAR_NAME, ignoring."
fi
else
case "$ARG" in
--dry-run|-n) DRY_RUN=true ;;
--help|-h)
echo "Usage: script [--dry-run|-n] [VAR=value ...]"
exit 0 ;;
*)
echo "Warning: Unknown argument $ARG" ;;
esac
fi
done
}
# Validate integer
validate_int() {
local name="$1"
local value="$2"
if ! [[ "${value:-}" =~ ^[0-9]+$ ]]; then
echo "Error: $name must be a non-negative integer."
exit 1
fi
}
# Require variable to be set
require_var() {
local var="$1"
if [[ -z "${!var:-}" ]]; then
echo "Error: Required variable $var is not set."
exit 1
fi
}