Tidy up functions.

This commit is contained in:
2026-07-23 23:30:25 +01:00
parent 4e1fb4b194
commit a3247cc783
+6 -26
View File
@@ -1,15 +1,6 @@
#!/usr/bin/env bash #!/usr/bin/env bash
# AMMIDS Shared Functions Library # MIMIC Shared Functions Library
# Location: init/scripts/functions.sh
# Sourced by all other scripts in this directory
# Prevent direct execution
if [[ "${BASH_SOURCE[0]}" == "${0}" ]]; then
echo "This is a library, not a script. Source it instead."
exit 1
fi
# --- Color Codes ---
RED='\033[0;31m' RED='\033[0;31m'
GREEN='\033[0;32m' GREEN='\033[0;32m'
YELLOW='\033[1;33m' YELLOW='\033[1;33m'
@@ -17,10 +8,11 @@ BLUE='\033[0;34m'
TITLE='\033[38;2;17;97;2m' TITLE='\033[38;2;17;97;2m'
NC='\033[0m' NC='\033[0m'
# --- Path Resolution --- if [[ "${BASH_SOURCE[0]}" == "${0}" ]]; then
# Standardised for project structure: /init/scripts/xxx.sh echo "This is a library, not a script. Source it instead."
# Sets: SCRIPT_DIR, INIT_DIR, ROOT_DIR exit 1
# --- Path Resolution --- fi
resolve_paths() { resolve_paths() {
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[1]}")" && pwd)" SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[1]}")" && pwd)"
@@ -40,12 +32,10 @@ resolve_paths() {
export SCRIPT_DIR INIT_DIR ROOT_DIR ENV_FILE COMPOSE_FILE export SCRIPT_DIR INIT_DIR ROOT_DIR ENV_FILE COMPOSE_FILE
} }
# --- Logging ---
log_info() { echo -e "${GREEN}[INFO]${NC} $1"; } log_info() { echo -e "${GREEN}[INFO]${NC} $1"; }
log_warn() { echo -e "${YELLOW}[WARN]${NC} $1"; } log_warn() { echo -e "${YELLOW}[WARN]${NC} $1"; }
log_error() { echo -e "${RED}[ERROR]${NC} $1"; } log_error() { echo -e "${RED}[ERROR]${NC} $1"; }
# --- State Management (init.ini) ---
write_state() { write_state() {
local key="$1" local key="$1"
local value="$2" local value="$2"
@@ -95,7 +85,6 @@ clear_state() {
> "$init_file" > "$init_file"
} }
# --- Secrets ---
generate_secret() { generate_secret() {
local length="${1:-32}" local length="${1:-32}"
if command -v openssl &> /dev/null; then if command -v openssl &> /dev/null; then
@@ -107,7 +96,6 @@ generate_secret() {
fi fi
} }
# --- Interactive Prompts ---
prompt_var() { prompt_var() {
local var_name="$1" local var_name="$1"
local prompt_text="$2" local prompt_text="$2"
@@ -160,9 +148,6 @@ auto_var() {
fi fi
} }
# --- File Permissions ---
fix_ownership() { fix_ownership() {
local target="$1" local target="$1"
local uid="${2:-${USER_ID:-1000}}" local uid="${2:-${USER_ID:-1000}}"
@@ -174,7 +159,6 @@ fix_ownership() {
fi fi
} }
# --- Dependency Checks ---
require_command() { require_command() {
local cmd="$1" local cmd="$1"
if ! command -v "$cmd" &> /dev/null; then if ! command -v "$cmd" &> /dev/null; then
@@ -218,7 +202,6 @@ require_env() {
fi fi
} }
# --- Volume Path Resolution ---
resolve_volume_path() { resolve_volume_path() {
VOLUME_PATH="${VOLUME_PATH:-./matrix}" VOLUME_PATH="${VOLUME_PATH:-./matrix}"
if [[ "$VOLUME_PATH" != /* ]]; then if [[ "$VOLUME_PATH" != /* ]]; then
@@ -227,9 +210,6 @@ resolve_volume_path() {
export VOLUME_PATH export VOLUME_PATH
} }
# Add to functions.sh
quiet_echo() { quiet_echo() {
[[ "${QUIET_MODE:-0}" == "1" ]] || echo "$1" [[ "${QUIET_MODE:-0}" == "1" ]] || echo "$1"
} }