~cytrogen/gstack

ref: cdd6f7865d0edf741f658a256115cbf77dace61b gstack/bin/gstack-config -rwxr-xr-x 2.1 KiB
cdd6f786 — Garry Tan feat: community wave — 7 fixes, relink, sidebar Write, discoverability (v0.13.5.0) (#641) 10 days ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#!/usr/bin/env bash
# gstack-config — read/write ~/.gstack/config.yaml
#
# Usage:
#   gstack-config get <key>          — read a config value
#   gstack-config set <key> <value>  — write a config value
#   gstack-config list               — show all config
#
# Env overrides (for testing):
#   GSTACK_STATE_DIR  — override ~/.gstack state directory
set -euo pipefail

STATE_DIR="${GSTACK_STATE_DIR:-$HOME/.gstack}"
CONFIG_FILE="$STATE_DIR/config.yaml"

case "${1:-}" in
  get)
    KEY="${2:?Usage: gstack-config get <key>}"
    # Validate key (alphanumeric + underscore only)
    if ! printf '%s' "$KEY" | grep -qE '^[a-zA-Z0-9_]+$'; then
      echo "Error: key must contain only alphanumeric characters and underscores" >&2
      exit 1
    fi
    grep -F "${KEY}:" "$CONFIG_FILE" 2>/dev/null | tail -1 | awk '{print $2}' | tr -d '[:space:]' || true
    ;;
  set)
    KEY="${2:?Usage: gstack-config set <key> <value>}"
    VALUE="${3:?Usage: gstack-config set <key> <value>}"
    # Validate key (alphanumeric + underscore only)
    if ! printf '%s' "$KEY" | grep -qE '^[a-zA-Z0-9_]+$'; then
      echo "Error: key must contain only alphanumeric characters and underscores" >&2
      exit 1
    fi
    mkdir -p "$STATE_DIR"
    # Escape sed special chars in value and drop embedded newlines
    ESC_VALUE="$(printf '%s' "$VALUE" | head -1 | sed 's/[&/\]/\\&/g')"
    if grep -qF "${KEY}:" "$CONFIG_FILE" 2>/dev/null; then
      # Portable in-place edit (BSD sed uses -i '', GNU sed uses -i without arg)
      _tmpfile="$(mktemp "${CONFIG_FILE}.XXXXXX")"
      sed "s/^${KEY}:.*/${KEY}: ${ESC_VALUE}/" "$CONFIG_FILE" > "$_tmpfile" && mv "$_tmpfile" "$CONFIG_FILE"
    else
      echo "${KEY}: ${VALUE}" >> "$CONFIG_FILE"
    fi
    # Auto-relink skills when prefix setting changes (skip during setup to avoid recursive call)
    if [ "$KEY" = "skill_prefix" ] && [ -z "${GSTACK_SETUP_RUNNING:-}" ]; then
      GSTACK_RELINK="$(dirname "$0")/gstack-relink"
      [ -x "$GSTACK_RELINK" ] && "$GSTACK_RELINK" || true
    fi
    ;;
  list)
    cat "$CONFIG_FILE" 2>/dev/null || true
    ;;
  *)
    echo "Usage: gstack-config {get|set|list} [key] [value]"
    exit 1
    ;;
esac