Last active
May 17, 2026 03:30
-
-
Save dkotama/29cc0cb005d280945d01fd70200a1613 to your computer and use it in GitHub Desktop.
install-ccswitcher.sh
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env bash | |
| set -euo pipefail | |
| # ── constants ────────────────────────────────────────────────────────────────── | |
| REPO_URL="https://github.com/dkotama/ccsettings-switcher.git" | |
| INSTALL_DIR="$HOME/.ccsettings-switcher" | |
| SCRIPT_NAME="cc-switch.sh" | |
| ALIAS_NAME="ccswitch" | |
| # ── helpers ──────────────────────────────────────────────────────────────────── | |
| info() { echo " $1"; } | |
| success() { echo "✓ $1"; } | |
| warn() { echo "⚠ $1"; } | |
| die() { echo "✗ Error: $1" >&2; exit 1; } | |
| require_cmd() { | |
| command -v "$1" &>/dev/null || die "'$1' is required but not installed. $2" | |
| } | |
| detect_shell_config() { | |
| local shell_name | |
| shell_name="$(basename "${SHELL:-}")" | |
| case "$shell_name" in | |
| zsh) echo "$HOME/.zshrc" ;; | |
| bash) echo "$HOME/.bashrc" ;; | |
| ksh) echo "$HOME/.kshrc" ;; | |
| fish) echo "$HOME/.config/fish/config.fish" ;; | |
| *) | |
| # Fallback: macOS defaults to zsh, Linux to bash | |
| if [[ "$(uname)" == "Darwin" ]]; then | |
| echo "$HOME/.zshrc" | |
| else | |
| echo "$HOME/.bashrc" | |
| fi | |
| ;; | |
| esac | |
| } | |
| inject_alias() { | |
| local config="$1" script_path="$2" | |
| local alias_line="${ALIAS_NAME}=\"${script_path}\"" | |
| if grep -qE "^alias ${ALIAS_NAME}=" "$config" 2>/dev/null; then | |
| local existing | |
| existing="$(grep -E "^alias ${ALIAS_NAME}=" "$config")" | |
| if [[ "$existing" == *"$script_path"* ]]; then | |
| success "Alias already correct in $config" | |
| return | |
| fi | |
| # Update stale alias in-place (works on both GNU and BSD sed) | |
| warn "Updating stale alias in $config" | |
| sed -i.bak "s|^alias ${ALIAS_NAME}=.*|alias ${alias_line}|" "$config" | |
| rm -f "${config}.bak" | |
| success "Alias updated" | |
| else | |
| printf '\n# ccsettings-switcher\nalias %s\n' "$alias_line" >> "$config" | |
| success "Alias added to $config" | |
| fi | |
| } | |
| # ── preflight checks ─────────────────────────────────────────────────────────── | |
| echo "" | |
| echo "ccsettings-switcher installer" | |
| echo "────────────────────────────────" | |
| require_cmd git "Install git: https://git-scm.com/downloads" | |
| require_cmd jq "Install jq: sudo apt install jq / brew install jq" | |
| # ── clone or update ──────────────────────────────────────────────────────────── | |
| if [[ -d "$INSTALL_DIR/.git" ]]; then | |
| info "Existing installation found — pulling latest..." | |
| git -C "$INSTALL_DIR" pull --ff-only | |
| success "Repository updated" | |
| elif [[ -d "$INSTALL_DIR" ]]; then | |
| die "$INSTALL_DIR exists but is not a git repo. Remove it and re-run." | |
| else | |
| info "Cloning repository to $INSTALL_DIR..." | |
| git clone --depth=1 "$REPO_URL" "$INSTALL_DIR" | |
| success "Repository cloned" | |
| fi | |
| # ── verify and make executable ───────────────────────────────────────────────── | |
| SCRIPT_PATH="$INSTALL_DIR/$SCRIPT_NAME" | |
| [[ -f "$SCRIPT_PATH" ]] || die "$SCRIPT_NAME not found in repository." | |
| chmod +x "$SCRIPT_PATH" | |
| success "$SCRIPT_NAME is executable" | |
| # ── shell config ─────────────────────────────────────────────────────────────── | |
| CONFIG_FILE="$(detect_shell_config)" | |
| # Ensure config file exists (e.g. first-time zsh user) | |
| [[ -f "$CONFIG_FILE" ]] || touch "$CONFIG_FILE" | |
| inject_alias "$CONFIG_FILE" "$SCRIPT_PATH" | |
| # ── fish special case: fish uses 'function', not alias ──────────────────────── | |
| if [[ "$CONFIG_FILE" == *.fish ]]; then | |
| warn "Fish shell detected. Adding function instead of alias." | |
| cat >> "$CONFIG_FILE" <<FISH | |
| # ccsettings-switcher | |
| function ${ALIAS_NAME} | |
| bash "$SCRIPT_PATH" \$argv | |
| end | |
| FISH | |
| fi | |
| # ── done ─────────────────────────────────────────────────────────────────────── | |
| echo "" | |
| echo "Installation complete." | |
| echo "Reload your shell: source $CONFIG_FILE" | |
| echo "Then run: $ALIAS_NAME <profile-name>" | |
| echo "" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment