Skip to content

Instantly share code, notes, and snippets.

@overdrivemachines
Created October 14, 2025 06:13
Show Gist options
  • Save overdrivemachines/ee84107bdbd881b692a811b4d5c2093c to your computer and use it in GitHub Desktop.
Save overdrivemachines/ee84107bdbd881b692a811b4d5c2093c to your computer and use it in GitHub Desktop.
edit_credentials.sh
#!/usr/bin/env bash
# edit_credentials.sh
# Usage:
# ./edit_credentials.sh -> opens default (no --environment)
# ./edit_credentials.sh -p -> opens production credentials
# ./edit_credentials.sh --prod
# ./edit_credentials.sh --production
set -euo pipefail
# Always run from the project root (where this script lives)
cd "$(dirname "${BASH_SOURCE[0]}")"
# Ensure EDITOR is exported so Rails can use it; default to VS Code if available
if [[ -z "${EDITOR:-}" ]]; then
if command -v code >/dev/null 2>&1; then
export EDITOR="code --wait"
else
export EDITOR="vi"
fi
fi
ENVIRONMENT=""
# Parse args
for arg in "$@"; do
case "$arg" in
-p|--prod|--production)
ENVIRONMENT="production"
;;
-h|--help)
echo "Usage: $0 [--production|--prod|-p]"
exit 0
;;
*)
;;
esac
done
# Choose rails launcher
if [[ -x "bin/rails" ]]; then
RAILS_CMD="bin/rails"
else
RAILS_CMD="bundle exec rails"
fi
# Helpful warning for prod key
if [[ "$ENVIRONMENT" == "production" ]]; then
if [[ -z "${RAILS_MASTER_KEY:-}" && ! -f "config/credentials/production.key" ]]; then
echo "Warning: No RAILS_MASTER_KEY env var and no config/credentials/production.key found." >&2
fi
fi
if [[ "$ENVIRONMENT" == "production" ]]; then
echo "Opening Rails PRODUCTION credentials with EDITOR='${EDITOR}'..."
exec $RAILS_CMD credentials:edit --environment production
else
echo "Opening Rails (default) credentials with EDITOR='${EDITOR}'..."
exec $RAILS_CMD credentials:edit
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment