fix(agent-setup): source existing .env and update GH_CONFIG_DIR in place

Preserves other variables already in $AGENT_HOME/.env when updating
the GH_CONFIG_DIR export rather than overwriting the file.
This commit is contained in:
2026-05-03 18:15:40 -04:00
parent e3fb22a098
commit ef8c736173
+18 -4
View File
@@ -9,10 +9,24 @@ die() { echo "ERROR: $*" >&2; exit 1; }
# so we mirror that structure under AGENT_HOME
export GH_CONFIG_DIR="$AGENT_HOME/.github"
# Write to a session dotfile so child processes inherit the variables
mkdir -p "$AGENT_HOME"
cat > "$AGENT_HOME/.env" <<EOF
export GH_CONFIG_DIR="$GH_CONFIG_DIR"
EOF
_ENV_FILE="$AGENT_HOME/.env"
# If .env exists, source it first so we preserve existing variables
if [[ -f "$_ENV_FILE" ]]; then
set -a
source "$_ENV_FILE"
set +a
fi
# Update or add GH_CONFIG_DIR export
if grep -q '^export GH_CONFIG_DIR=' "$_ENV_FILE" 2>/dev/null; then
# Replace existing value in place
sed -i.bak "s|^export GH_CONFIG_DIR=.*|export GH_CONFIG_DIR=\"$GH_CONFIG_DIR\"|" "$_ENV_FILE"
rm -f "$_ENV_FILE.bak"
else
# Append new export
printf 'export GH_CONFIG_DIR="%s"\n' "$GH_CONFIG_DIR" >> "$_ENV_FILE"
fi
echo "GH_CONFIG_DIR set to $GH_CONFIG_DIR"