phoebus_os/installer/lib/state.sh
Phoebus 38214ea9e7 chore: import Phoebus OS installer and Rust migration
Initial public import of the proven Bash installer (installer/) and the production Rust workspace (installer-rs/). The reference ISO, build output and local agent transcripts are gitignored.

Verified: installer/tests/run_tests.sh and cargo test --workspace both pass.
2026-09-20 02:07:33 -07:00

135 lines
3.4 KiB
Bash

#!/usr/bin/env bash
# shellcheck shell=bash
#
# Installer configuration state.
#
# All user choices live in the associative array CFG. The state is deliberately
# kept separate from the UI so that an unattended installation can be driven
# from a plain configuration file.
declare -gA CFG=()
declare -g CONFIG_FILE="${CONFIG_FILE:-}"
# config_default KEY VALUE
# Set KEY to VALUE only when it has not been set yet. This lets a user
# config file loaded later override the shipped defaults.
config_default() {
local key="$1" value="${2-}"
if [[ -z "${CFG[$key]+set}" ]]; then
CFG[$key]="$value"
fi
}
# config_set KEY VALUE
config_set() {
local key="$1" value="${2-}"
CFG[$key]="$value"
}
# config_get KEY [DEFAULT]
config_get() {
local key="$1" default="${2-}"
if [[ -n "${CFG[$key]+set}" ]]; then
printf '%s' "${CFG[$key]}"
else
printf '%s' "$default"
fi
}
# config_is KEY VALUE - boolean helper
config_is() {
[[ "$(config_get "$1")" == "$2" ]]
}
config_bool() {
case "$(config_get "$1")" in
yes|true|1|on) return 0 ;;
*) return 1 ;;
esac
}
# config_load_defaults [FILE]
config_load_defaults() {
local file="${1:-$PHOEBUS_ROOT/config/defaults.conf}"
[[ -r "$file" ]] || return 1
# shellcheck source=/dev/null
source "$file"
}
# Decode a value from a config file. Supports plain, single- and double-quoted
# values. This is not a general shell evaluator on purpose: configuration files
# are data, and must never execute code.
config_decode_value() {
local raw="$1"
case "$raw" in
\'*\')
raw="${raw#\'}"
raw="${raw%\'}"
raw="${raw//\'\\\'\'/\'}"
;;
\"*\")
raw="${raw#\"}"
raw="${raw%\"}"
;;
esac
printf '%s' "$raw"
}
# config_load_file FILE
config_load_file() {
local file="$1" line key value
[[ -r "$file" ]] || return 1
while IFS= read -r line || [[ -n "$line" ]]; do
[[ "$line" =~ ^[[:space:]]*# ]] && continue
[[ "$line" =~ ^[[:space:]]*$ ]] && continue
if [[ "$line" =~ ^([A-Za-z_][A-Za-z0-9_]*)=(.*)$ ]]; then
key="${BASH_REMATCH[1]}"
value="$(config_decode_value "${BASH_REMATCH[2]}")"
CFG[$key]="$value"
fi
done <"$file"
CONFIG_FILE="$file"
}
# config_save_file FILE
config_save_file() {
local file="$1" key
local dir
dir="$(dirname -- "$file")"
mkdir -p "$dir"
{
printf '# Phoebus OS installer configuration\n'
printf '# Generated: %s\n' "$(date -u +%Y-%m-%dT%H:%M:%SZ 2>/dev/null || echo unknown)"
printf '# NOTE: never place passwords, passphrases or API keys in this file.\n\n'
local -a keys
mapfile -t keys < <(printf '%s\n' "${!CFG[@]}" | sort)
for key in "${keys[@]}"; do
case "$key" in
LUKS_PASSPHRASE|ROOT_PASSWORD|USER_PASSWORD|AI_API_KEY)
continue ;;
esac
printf "%s='%s'\n" "$key" "${CFG[$key]//\'/\'\\\'\'}"
done
} >"$file"
}
# config_summary - stable, sorted KEY=value dump (for logs, values redacted)
config_dump() {
local key
local -a keys
mapfile -t keys < <(printf '%s\n' "${!CFG[@]}" | sort)
for key in "${keys[@]}"; do
printf '%s=%s\n' "$key" "$(redact_value "$key" "${CFG[$key]}")"
done
}
# redact_value KEY VALUE - never emit secret material
redact_value() {
local key="$1" value="$2"
case "$key" in
*PASSWORD*|*PASSPHRASE*|*API_KEY*|*TOKEN*|*SECRET*)
if [[ -n "$value" ]]; then printf '<redacted>'; else printf ''; fi
;;
*) printf '%s' "$value" ;;
esac
}