diff options
| author | danix <danix@danix.xyz> | 2026-06-11 09:46:01 +0200 |
|---|---|---|
| committer | danix <danix@danix.xyz> | 2026-06-11 09:46:01 +0200 |
| commit | b72a81a3a4becee8c388b9d110cb650ac26f1ebf (patch) | |
| tree | d46fd150b1d127d676ce62528730c9ab0c127daa | |
| parent | 5d5da3a1b50eedb3c30aa209eb24aa7f579dc701 (diff) | |
| download | wallp-b72a81a3a4becee8c388b9d110cb650ac26f1ebf.tar.gz wallp-b72a81a3a4becee8c388b9d110cb650ac26f1ebf.zip | |
feat: conf bootstrap + required-key validation
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
| -rw-r--r-- | tests/wallp.bats | 26 | ||||
| -rw-r--r-- | wallp | 47 |
2 files changed, 73 insertions, 0 deletions
diff --git a/tests/wallp.bats b/tests/wallp.bats index 3445ddb..298067f 100644 --- a/tests/wallp.bats +++ b/tests/wallp.bats @@ -54,3 +54,29 @@ teardown() { [ "$CONF_DEFAULT_H" = "$HOME/p/h.png" ] [ "$CONF_DEFAULT_V" = "/abs/v.png" ] } + +@test "load_conf bootstraps template when conf missing" { + run load_conf + [ "$status" -eq 10 ] + [ -f "$HOME/.config/wallp/wallp.conf" ] + [[ "$output" == *"fill it in"* ]] +} + +@test "load_conf hard-errors on missing required key" { + mkdir -p "$HOME/.config/wallp" + printf '%s\n' 'OUTPUT_H=DP-1' 'OUTPUT_V=DP-3' 'DEFAULT_H=/a.png' \ + > "$HOME/.config/wallp/wallp.conf" + run load_conf + [ "$status" -eq 1 ] + [[ "$output" == *"DEFAULT_V"* ]] +} + +@test "load_conf ok with THEME fallback" { + mkdir -p "$HOME/.config/wallp" + printf '%s\n' 'OUTPUT_H=DP-1' 'OUTPUT_V=DP-3' \ + 'DEFAULT_H=/a.png' 'DEFAULT_V=/b.png' \ + > "$HOME/.config/wallp/wallp.conf" + load_conf + [ "$?" -eq 0 ] + [ "$CONF_THEME" = "sexy-splurge" ] +} @@ -31,6 +31,53 @@ parse_conf() { done < "$file" } +conf_path() { printf '%s\n' "$HOME/.config/wallp/wallp.conf"; } + +write_conf_template() { + local path; path="$(conf_path)" + mkdir -p "$(dirname "$path")" + cat > "$path" <<'EOF' +# wallp config. Fill in real values, then re-run wallp. +# THEME is optional (defaults to sexy-splurge). +THEME=sexy-splurge +# Physical output names (see: swaymsg -t get_outputs / wlr-randr) +OUTPUT_H=DP-1 +OUTPUT_V=DP-3 +# Default wallpapers used by --restore when no saved state exists. +DEFAULT_H=~/Pictures/wallpapers/SFW/horizontal.png +DEFAULT_V=~/Pictures/wallpapers/SFW/vertical.png +EOF +} + +# Hard-error if any required path/output key is empty. Returns 1 on first miss. +require_conf_keys() { + local k var + for k in OUTPUT_H OUTPUT_V DEFAULT_H DEFAULT_V; do + var="CONF_$k" + if [ -z "${!var}" ]; then + echo "wallp: required config key '$k' is missing or empty in $(conf_path)" >&2 + return 1 + fi + done + return 0 +} + +# Returns: 0 ok, 1 invalid conf, 10 bootstrapped (caller should exit 0). +load_conf() { + local path; path="$(conf_path)" + if [ ! -f "$path" ]; then + write_conf_template + echo "wallp: generated config at $path — fill it in and re-run." >&2 + command -v notify-send >/dev/null 2>&1 && \ + notify-send -u normal "wallp" "Config generated at $path. Fill it in and re-run." + return 10 + fi + parse_conf "$path" + [ -z "$CONF_THEME" ] && CONF_THEME="sexy-splurge" + require_conf_keys || return 1 + return 0 +} + main() { return 0 } |
