Skip to content

Instantly share code, notes, and snippets.

@kenpower
Last active October 24, 2025 10:57
Show Gist options
  • Save kenpower/2b910a03a578b94dfdb15315e6308c7d to your computer and use it in GitHub Desktop.
Save kenpower/2b910a03a578b94dfdb15315e6308c7d to your computer and use it in GitHub Desktop.
setup-dotfiles.sh
#!/bin/bash
# ================================================================
# setup-dotfiles.sh — public bootstrap script for dotfiles
# Parameterised: GITHUB_TOKEN, GITHUB_USER, REPO_NAME
# ================================================================
set -euo pipefail
# ─── READ PARAMETERS OR ENV VARS ───────────────────────────────────────────
for arg in "$@"; do
eval "$arg"
done
: "${GITHUB_TOKEN:?You must pass GITHUB_TOKEN=<token>}"
: "${GITHUB_USER:?You must pass GITHUB_USER=<username>}"
: "${REPO_NAME:?You must pass REPO_NAME=<repository name>}"
BRANCH="${BRANCH:-main}"
# ─── PREPARE SYSTEM ───────────────────────────────────────────────────────
sudo apt update -y && sudo apt install -y git curl
git config --global user.name "$GITHUB_USER"
git config --global user.email "[email protected]"
git config --global init.defaultBranch "$BRANCH"
git config --global credential.helper store
# ─── CONFIGURE GIT HTTPS CREDENTIAL ───────────────────────────────────────
GIT_CRED_FILE="$HOME/.git-credentials"
CRED_URL="https://$GITHUB_USER:[email protected]"
echo "$CRED_URL" > "$GIT_CRED_FILE"
chmod 600 "$GIT_CRED_FILE"
# ─── CLONE OR UPDATE DOTFILES REPO ────────────────────────────────────────
DOTFILES_DIR="$HOME/$REPO_NAME"
if [ -d "$DOTFILES_DIR/.git" ]; then
echo "⚙️ Repo exists — pulling latest..."
cd "$DOTFILES_DIR"
git pull origin "$BRANCH"
else
echo "⬇️ Cloning repo..."
git clone "https://github.com/$GITHUB_USER/$REPO_NAME.git" "$DOTFILES_DIR"
cd "$DOTFILES_DIR"
fi
# ─── EXECUTABLE PERMISSIONS ───────────────────────────────────────────────
chmod +x "$DOTFILES_DIR"/*.sh 2>/dev/null || true
# ─── RUN SYMLINK SETUP & SYNC ────────────────────────────────────────────
if [ -x "$DOTFILES_DIR/scripts/link-dotfiles.sh" ]; then
echo "🔗 Running link-dotfiles.sh..."
"$DOTFILES_DIR/scripts/link-dotfiles.sh"
fi
if [ -x "$DOTFILES_DIR/scripts/sync.sh" ]; then
echo "🔁 Running sync.sh..."
"$DOTFILES_DIR/scripts/sync.sh"
fi
# ─── RELOAD SHELL ─────────────────────────────────────────────────────────
if [ -f "$HOME/.bashrc" ]; then
echo "🔁 Reloading Bash environment..."
source "$HOME/.bashrc"
fi
echo "🎉 Dotfiles setup complete for $GITHUB_USER/$REPO_NAME on $(hostname)"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment