feat: extract .env validators to common.sh and add validate_env()
Move 10 validation functions from configure_env.sh to lib/common.sh as shared utilities. Define variable-to-validator mapping using parallel arrays (bash 3.2 compatible). validate_env() checks all ~50 .env variables against their expected format and reports all failures at once. Wired into preflight.sh (Check 6b) and bitwarden_to_env.sh (post-restore). configure_env.sh now sources validators from common.sh instead of defining its own copies. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -11,6 +11,10 @@ PROJECT_ROOT="${SCRIPT_DIR}/.."
|
||||
ENV_FILE="${PROJECT_ROOT}/.env"
|
||||
ENV_EXAMPLE="${PROJECT_ROOT}/.env.example"
|
||||
|
||||
# shellcheck source=../lib/common.sh
|
||||
# shellcheck disable=SC1091
|
||||
source "${PROJECT_ROOT}/lib/common.sh"
|
||||
|
||||
# Colors
|
||||
if [[ -t 1 ]]; then
|
||||
C_RESET='\033[0m'; C_BOLD='\033[1m'; C_GREEN='\033[0;32m'
|
||||
@@ -52,47 +56,10 @@ while IFS= read -r line; do
|
||||
done < "$ENV_FILE"
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# Validation functions
|
||||
# Validation functions — sourced from lib/common.sh (validate_ip, validate_port,
|
||||
# validate_email, validate_path, validate_url, validate_bool, validate_integer,
|
||||
# validate_nonempty, validate_password, validate_ssl_mode)
|
||||
# ---------------------------------------------------------------------------
|
||||
validate_ip() {
|
||||
[[ "$1" =~ ^[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+$ ]]
|
||||
}
|
||||
|
||||
validate_port() {
|
||||
[[ "$1" =~ ^[0-9]+$ ]] && [[ "$1" -ge 1 ]] && [[ "$1" -le 65535 ]]
|
||||
}
|
||||
|
||||
validate_email() {
|
||||
[[ "$1" == *@* ]]
|
||||
}
|
||||
|
||||
validate_path() {
|
||||
[[ "$1" == /* ]]
|
||||
}
|
||||
|
||||
validate_url() {
|
||||
[[ "$1" =~ ^https?:// ]]
|
||||
}
|
||||
|
||||
validate_bool() {
|
||||
[[ "$1" == "true" ]] || [[ "$1" == "false" ]]
|
||||
}
|
||||
|
||||
validate_integer() {
|
||||
[[ "$1" =~ ^[0-9]+$ ]]
|
||||
}
|
||||
|
||||
validate_nonempty() {
|
||||
[[ -n "$1" ]]
|
||||
}
|
||||
|
||||
validate_password() {
|
||||
[[ ${#1} -ge 8 ]]
|
||||
}
|
||||
|
||||
validate_ssl_mode() {
|
||||
[[ "$1" == "letsencrypt" ]] || [[ "$1" == "existing" ]]
|
||||
}
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# Prompt function
|
||||
|
||||
Reference in New Issue
Block a user