Skip to content

Instantly share code, notes, and snippets.

@jksprattler
Last active September 19, 2026 23:05
Show Gist options
  • Select an option

  • Save jksprattler/f54e48e096be5c87fcbc72b8c8949fce to your computer and use it in GitHub Desktop.

Select an option

Save jksprattler/f54e48e096be5c87fcbc72b8c8949fce to your computer and use it in GitHub Desktop.
Dev container token helper for project-scoped fine-grained PAT
# Dev container token helper functions
# Launch devcontainer in cli mode:
dev-agent() {
local project_name=$(basename "$PWD")
local token_file="$HOME/.gh-tokens/${project_name}.token"
if [ -f "$token_file" ]; then
export GH_TOKEN=$(cat "$token_file")
export GIT_AUTHOR_NAME="machine-username"
export GIT_AUTHOR_EMAIL="machine-username@users.noreply.github.com"
echo "Loaded project-scoped credentials for ${project_name}"
else
echo "Warning: No token file at ${token_file}. Launching with local host inherited credentials."
unset GH_TOKEN; unset GIT_AUTHOR_NAME; unset GIT_AUTHOR_EMAIL
fi
# Check if devcontainers cli installed, if not install it:
command -v devcontainer >/dev/null 2>&1 || npm install -g @devcontainers/cli
# Pass --build (or -b) to trigger a fresh image build and update packages
local build_flag=""
if [[ "$1" == "--build" || "$1" == "-b" ]]; then
build_flag="--remove-existing-container"
fi
devcontainer up --workspace-folder . $build_flag || return 1
devcontainer exec --workspace-folder . zsh
# Container cleanup after exit
docker rm -f $(docker ps -aq --filter "label=devcontainer.local_folder=$(pwd)")
}
# Launch devcontainer in IDE mode:
dev-agent-ide() {
local project_name=$(basename "$PWD")
local token_file="$HOME/.gh-tokens/${project_name}.token"
if [ -f "$token_file" ]; then
export GH_TOKEN=$(cat "$token_file")
export GIT_AUTHOR_NAME="machine-username"
export GIT_AUTHOR_EMAIL="machine-username@users.noreply.github.com"
echo "Loaded project-scoped credentials for ${project_name}"
else
echo "Warning: No token file at ${token_file}. Launching with local host inherited credentials."
unset GH_TOKEN; unset GIT_AUTHOR_NAME; unset GIT_AUTHOR_EMAIL
fi
code .
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment