~cytrogen/gstack

ref: a30f7079da13904d32a50b73f1ff04c1ed75ca7c gstack/bin/gstack-config -rwxr-xr-x 1.1 KiB
a30f7079 — Garry Tan feat: Fix-First Review — auto-fix obvious issues, ask about hard ones (v0.4.5) (#116) a month 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
#!/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>}"
    grep -E "^${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>}"
    mkdir -p "$STATE_DIR"
    if grep -qE "^${KEY}:" "$CONFIG_FILE" 2>/dev/null; then
      sed -i '' "s/^${KEY}:.*/${KEY}: ${VALUE}/" "$CONFIG_FILE"
    else
      echo "${KEY}: ${VALUE}" >> "$CONFIG_FILE"
    fi
    ;;
  list)
    cat "$CONFIG_FILE" 2>/dev/null || true
    ;;
  *)
    echo "Usage: gstack-config {get|set|list} [key] [value]"
    exit 1
    ;;
esac