Initial Commit
This commit is contained in:
@@ -0,0 +1,129 @@
|
||||
#!/usr/bin/env bash
|
||||
# AMMIDS 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'
|
||||
GREEN='\033[0;32m'
|
||||
YELLOW='\033[1;33m'
|
||||
BLUE='\033[0;34m'
|
||||
TITLE='\033[38;2;17;97;2m'
|
||||
NC='\033[0m'
|
||||
|
||||
# --- Path Resolution ---
|
||||
# Standardised for project structure: /init/scripts/xxx.sh
|
||||
# Sets: SCRIPT_DIR, INIT_DIR, ROOT_DIR
|
||||
resolve_paths() {
|
||||
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[1]}")" && pwd)"
|
||||
INIT_DIR="$(dirname "$SCRIPT_DIR")"
|
||||
ROOT_DIR="$(dirname "$INIT_DIR")"
|
||||
export SCRIPT_DIR INIT_DIR ROOT_DIR
|
||||
}
|
||||
|
||||
# --- Logging ---
|
||||
log_info() { echo -e "${GREEN}[INFO]${NC} $1"; }
|
||||
log_warn() { echo -e "${YELLOW}[WARN]${NC} $1"; }
|
||||
log_error() { echo -e "${RED}[ERROR]${NC} $1"; }
|
||||
|
||||
# --- State Management (init.ini) ---
|
||||
write_state() {
|
||||
local key="$1"
|
||||
local value="$2"
|
||||
local init_file="${INIT_DIR}/init.ini"
|
||||
local upper_value
|
||||
upper_value=$(echo "$value" | tr '[:lower:]' '[:upper:]')
|
||||
echo "${key}=${upper_value}" >> "$init_file"
|
||||
}
|
||||
|
||||
load_state() {
|
||||
local init_file="${INIT_DIR}/init.ini"
|
||||
if [[ -f "$init_file" ]]; then
|
||||
source "$init_file"
|
||||
else
|
||||
log_warn "State file not found at ${init_file}. Using defaults."
|
||||
fi
|
||||
}
|
||||
|
||||
clear_state() {
|
||||
local init_file="${INIT_DIR}/init.ini"
|
||||
> "$init_file"
|
||||
}
|
||||
|
||||
# --- Secrets ---
|
||||
generate_secret() {
|
||||
local length="${1:-32}"
|
||||
if command -v openssl &> /dev/null; then
|
||||
openssl rand -hex "$((length / 2))"
|
||||
elif command -v pwgen &> /dev/null; then
|
||||
pwgen -s "$length" 1
|
||||
else
|
||||
tr -dc 'A-Za-z0-9' < /dev/urandom | head -c "$length"
|
||||
fi
|
||||
}
|
||||
|
||||
# --- File Permissions ---
|
||||
fix_ownership() {
|
||||
local target="$1"
|
||||
local uid="${2:-${USER_ID:-1000}}"
|
||||
local gid="${3:-${GROUP_ID:-1000}}"
|
||||
|
||||
if [[ -e "$target" ]] && [[ "$(stat -c '%U' "$target" 2>/dev/null)" != "$USER" ]]; then
|
||||
log_warn "Fixing ownership on $target..."
|
||||
sudo chown -R "${uid}:${gid}" "$target"
|
||||
fi
|
||||
}
|
||||
|
||||
# --- Dependency Checks ---
|
||||
require_command() {
|
||||
local cmd="$1"
|
||||
if ! command -v "$cmd" &> /dev/null; then
|
||||
log_error "Required command '${cmd}' not found. Install it and retry."
|
||||
exit 1
|
||||
fi
|
||||
}
|
||||
|
||||
require_file() {
|
||||
local file="$1"
|
||||
local description="${2:-File}"
|
||||
if [[ ! -f "$file" ]]; then
|
||||
log_error "${description} not found at ${file}"
|
||||
exit 1
|
||||
fi
|
||||
}
|
||||
|
||||
require_env() {
|
||||
local file="${ROOT_DIR}/.env"
|
||||
if [[ ! -f "$file" ]]; then
|
||||
log_error ".env file not found. Run set_env.sh first."
|
||||
exit 1
|
||||
fi
|
||||
source "$file"
|
||||
}
|
||||
|
||||
require_docker() {
|
||||
require_command "docker"
|
||||
require_command "docker compose"
|
||||
}
|
||||
|
||||
# --- Volume Path Resolution ---
|
||||
resolve_volume_path() {
|
||||
VOLUME_PATH="${VOLUME_PATH:-./matrix}"
|
||||
if [[ "$VOLUME_PATH" != /* ]]; then
|
||||
VOLUME_PATH="${ROOT_DIR}/${VOLUME_PATH}"
|
||||
fi
|
||||
export VOLUME_PATH
|
||||
}
|
||||
|
||||
# Add to functions.sh
|
||||
|
||||
|
||||
quiet_echo() {
|
||||
[[ "${QUIET_MODE:-0}" == "1" ]] || echo "$1"
|
||||
}
|
||||
Reference in New Issue
Block a user