Files
skillshare/.devcontainer/bin/credential-helper
T
Willie c5d32111f7 dev: overhaul devcontainer experience with persistence and enhanced tooling
Improve the sandbox development environment to support "new user" simulation
while maintaining a high-quality developer experience.

Changes:
- Persistence: Redirect HOME to /home/developer and add a persistent volume
  to preserve shell history, gh auth state, and isolated environments.
- Tooling: Pre-install GitHub CLI (gh), air (hot-reload), and delve (dlv)
  in the sandbox Dockerfile.
- Isolated Envs:
    - Update ssenv to store environments in persistent $HOME/.ss-envs.
    - Add --init flag to ssnew/ssenv to auto-run 'ss init -g' in new envs.
    - Add visual PS1 indicator when an isolated environment is active.
- DX Refinements:
    - Add a centralized 'help' command (aliased to override bash built-in).
    - Fix ANSI color rendering in help script.
    - Robust PROJECT_ROOT redirection in skillshare wrapper script.
    - Support /home/developer in setup.sh environment checks.
2026-02-25 07:02:54 +08:00

122 lines
3.5 KiB
Bash
Executable File

#!/usr/bin/env bash
set -euo pipefail
BACKUP_DIR="$HOME/.git-auth-backup"
mkdir -p "$BACKUP_DIR"
BACKUP_FILE="$BACKUP_DIR/helper"
TOKEN_BACKUP="$BACKUP_DIR/tokens"
TOKEN_VARS=(GITHUB_TOKEN GITLAB_TOKEN BITBUCKET_TOKEN BITBUCKET_USERNAME SKILLSHARE_GIT_TOKEN)
# When called with --eval, output shell commands for the caller to eval.
# Usage: eval "$(credential-helper off)" / eval "$(credential-helper on)"
EVAL_MODE=false
if [ "${1:-}" = "--eval" ]; then
EVAL_MODE=true
shift
fi
emit() {
if $EVAL_MODE; then
echo "$1"
fi
}
case "${1:-}" in
off)
# Save current credential helper
current="$(git config --global credential.helper 2>/dev/null || true)"
if [ -n "$current" ] && [ "$current" != "''" ]; then
echo "$current" > "$BACKUP_FILE"
echo "Saved credential.helper: $current" >&2
fi
git config --global credential.helper ''
echo "credential.helper disabled" >&2
# Save and unset token env vars
: > "$TOKEN_BACKUP"
for var in "${TOKEN_VARS[@]}"; do
val="${!var:-}"
if [ -n "$val" ]; then
echo "$var=$val" >> "$TOKEN_BACKUP"
emit "unset $var;"
echo " unset $var" >&2
fi
done
if ! $EVAL_MODE; then
# Check if any tokens were active but couldn't be unset
has_tokens=false
for var in "${TOKEN_VARS[@]}"; do
if [ -n "${!var:-}" ]; then has_tokens=true; break; fi
done
if $has_tokens; then
echo "" >&2
echo "Token env vars detected but cannot be unset from a script." >&2
echo "Run with eval to also clear tokens:" >&2
echo " eval \"\$(credential-helper --eval off)\"" >&2
fi
fi
;;
on)
# Restore credential helper
if [ -f "$BACKUP_FILE" ]; then
saved="$(cat "$BACKUP_FILE")"
git config --global credential.helper "$saved"
echo "credential.helper restored: $saved" >&2
else
git config --global --unset credential.helper 2>/dev/null || true
echo "credential.helper unset (system default)" >&2
fi
# Restore token env vars
if [ -f "$TOKEN_BACKUP" ]; then
while IFS='=' read -r key val; do
[ -z "$key" ] && continue
emit "export $key='$val';"
echo " restored $key" >&2
done < "$TOKEN_BACKUP"
rm -f "$TOKEN_BACKUP"
fi
if ! $EVAL_MODE; then
if [ -f "$TOKEN_BACKUP" ] 2>/dev/null; then
echo "" >&2
echo "Token backup found. Run with eval to restore tokens:" >&2
echo " eval \"\$(credential-helper --eval on)\"" >&2
fi
fi
;;
status)
current="$(git config --global credential.helper 2>/dev/null || true)"
if [ -z "$current" ]; then
echo "credential.helper: (not set, using system default)"
elif [ "$current" = "''" ]; then
echo "credential.helper: DISABLED"
else
echo "credential.helper: $current"
fi
echo ""
for var in "${TOKEN_VARS[@]}"; do
val="${!var:-}"
if [ -n "$val" ]; then
# Show first 4 chars + mask
masked="${val:0:4}****"
echo "$var: $masked"
else
echo "$var: (not set)"
fi
done
;;
*)
echo "Usage: eval \"\$(credential-helper --eval <on|off>)\"" >&2
echo " credential-helper <on|off|status>" >&2
echo "" >&2
echo " off Disable all git auth (credential helper + token env vars)" >&2
echo " on Restore credential helper and token env vars" >&2
echo " status Show current auth state" >&2
echo "" >&2
echo "Use --eval to also unset/restore token env vars in your shell." >&2
exit 1
;;
esac