70 lines
1.8 KiB
Bash
70 lines
1.8 KiB
Bash
|
|
#!/usr/bin/env bash
|
||
|
|
# Configuration Utility Functions
|
||
|
|
# Consolidated functions from small config-related scripts
|
||
|
|
# Usage: source "$(dirname "${BASH_SOURCE[0]}")/../utils/config-utils.sh"
|
||
|
|
|
||
|
|
set -euo pipefail
|
||
|
|
|
||
|
|
# Load shared modules
|
||
|
|
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
|
||
|
|
PROJECT_ROOT="$(cd "$SCRIPT_DIR/../.." && pwd)"
|
||
|
|
source "$SCRIPT_DIR/../lib/ip-config.sh" 2>/dev/null || true
|
||
|
|
source "$SCRIPT_DIR/../lib/logging.sh" 2>/dev/null || true
|
||
|
|
|
||
|
|
# Validate config file
|
||
|
|
config_validate() {
|
||
|
|
local config_file="$1"
|
||
|
|
|
||
|
|
if [ -f "$config_file" ]; then
|
||
|
|
log_success "Configuration file exists: $config_file"
|
||
|
|
return 0
|
||
|
|
else
|
||
|
|
log_error "Configuration file missing: $config_file"
|
||
|
|
return 1
|
||
|
|
fi
|
||
|
|
}
|
||
|
|
|
||
|
|
# Backup config file
|
||
|
|
config_backup() {
|
||
|
|
local config_file="$1"
|
||
|
|
local backup_file="${config_file}.backup.$(date +%Y%m%d_%H%M%S)"
|
||
|
|
|
||
|
|
if [ -f "$config_file" ]; then
|
||
|
|
cp "$config_file" "$backup_file"
|
||
|
|
log_success "Backed up: $config_file → $backup_file"
|
||
|
|
return 0
|
||
|
|
else
|
||
|
|
log_error "Config file not found: $config_file"
|
||
|
|
return 1
|
||
|
|
fi
|
||
|
|
}
|
||
|
|
|
||
|
|
# Get config value
|
||
|
|
config_get() {
|
||
|
|
local config_file="$1"
|
||
|
|
local key="$2"
|
||
|
|
|
||
|
|
if [ -f "$config_file" ]; then
|
||
|
|
grep "^${key}=" "$config_file" | cut -d'=' -f2- | sed 's/^["'\'']//;s/["'\'']$//'
|
||
|
|
fi
|
||
|
|
}
|
||
|
|
|
||
|
|
# Set config value
|
||
|
|
config_set() {
|
||
|
|
local config_file="$1"
|
||
|
|
local key="$2"
|
||
|
|
local value="$3"
|
||
|
|
|
||
|
|
if [ -f "$config_file" ]; then
|
||
|
|
if grep -q "^${key}=" "$config_file"; then
|
||
|
|
sed -i "s|^${key}=.*|${key}=${value}|" "$config_file"
|
||
|
|
else
|
||
|
|
echo "${key}=${value}" >> "$config_file"
|
||
|
|
fi
|
||
|
|
log_success "Set ${key}=${value} in $config_file"
|
||
|
|
else
|
||
|
|
log_error "Config file not found: $config_file"
|
||
|
|
return 1
|
||
|
|
fi
|
||
|
|
}
|