#!/bin/bash # Sergeant team setup for Claude Code. One line in the Terminal app: # # curl -fsSL https://setup.sergeant.agency | bash # # What it does: # 1. GitHub CLI: installs gh if it is missing, and logs you in if you are not logged in. # 2. Checks that you can read the private repo sergeant-agency/SGT-Team-Setup. # 3. Claude Code for the terminal: finds it in the usual places (Homebrew, Anthropic # installer, npm, nvm), installs it with Homebrew if it is missing, and updates it. # 4. Checks the version (at least MIN_VERSION). # 5. Lets git use your gh login if git cannot read the repo yet. # 6. Installs the plugin sergeant-setup, or updates it if it is already there. # 7. Asks for your chat language and sets the matching output style. # # Run it again at any time to update. It contains no secrets: the plugin itself is in the # private repo, and only people with access can install it. # # DRY_RUN=1 shows the commands and changes nothing. # NO_PROMPT=1 skips all questions (no login, no style choice). # SETUP_STYLE=1..5 picks the style without a question (1 English ... 5 Spanish). # SETUP_GIT=y|n answers the question about gh auth setup-git (default without a terminal: n). set -u ORIG_PATH="$PATH" MIN_VERSION="2.1.292" PLUGIN="sergeant-setup" MARKETPLACE_NAME="sergeant" MARKETPLACE_SOURCE="sergeant-agency/SGT-Team-Setup" SETTINGS="$HOME/.claude/settings.json" HELP="Ask for help in the Sergeant Slack (#general). Copy the error message, but leave out any login code." say() { printf '\n==> %s\n' "$*"; } warn() { printf '\nWARNING: %s\n' "$*" >&2; } fail() { printf '\nERROR: %s\n%s\n' "$*" "$HELP" >&2; exit 1; } run() { echo " + $*"; [ -n "${DRY_RUN:-}" ] || "$@" /dev/null; } ask() { local a=""; read -r -p "$1" a m ? n : m); i++) { if ((x[i] + 0) < (y[i] + 0)) exit 0 if ((x[i] + 0) > (y[i] + 0)) exit 1 } exit 1 }' } find_first() { local c; for c in "$@"; do if [ -n "$c" ] && [ -x "$c" ]; then echo "$c"; return 0; fi; done; return 1; } find_brew() { find_first "$(command -v brew 2>/dev/null)" /opt/homebrew/bin/brew /usr/local/bin/brew; } find_gh() { find_first "$(command -v gh 2>/dev/null)" /opt/homebrew/bin/gh /usr/local/bin/gh; } find_claude() { find_first "$(command -v claude 2>/dev/null)" \ "$HOME/.local/bin/claude" "$HOME/.claude/local/claude" \ /opt/homebrew/bin/claude /usr/local/bin/claude \ "$HOME/.npm-global/bin/claude" "$HOME"/.nvm/versions/node/*/bin/claude } claude_version() { "$1" --version 2>/dev/null "$js" <<'EOF' ObjC.import('Foundation'); function run(argv) { const [given, key, value] = argv; const fm = $.NSFileManager.defaultManager; // Write through a symlink to its target, so a linked settings file stays linked. const path = $(given).stringByResolvingSymlinksInPath.js; let obj = {}; if (fm.fileExistsAtPath(path)) { const s = $.NSString.stringWithContentsOfFileEncodingError(path, $.NSUTF8StringEncoding, null); if (s.isNil()) throw new Error('cannot read ' + path + ', nothing changed'); const txt = s.js; if (txt.trim()) obj = JSON.parse(txt); // invalid JSON throws: nothing changed if (typeof obj !== 'object' || obj === null || Array.isArray(obj)) throw new Error('not a JSON object: ' + path); } if (value === undefined) return obj[key] === undefined ? '' : String(obj[key]); if (fm.fileExistsAtPath(path)) { const bak = path + '.before-sergeant-setup'; if (!fm.fileExistsAtPath(bak) && !fm.copyItemAtPathToPathError(path, bak, null)) throw new Error('cannot save the backup ' + bak + ', nothing changed'); } obj[key] = value; const ok = $(JSON.stringify(obj, null, 2) + '\n').writeToFileAtomicallyEncodingError(path, true, $.NSUTF8StringEncoding, null); if (!ok) throw new Error('cannot write ' + path); return 'ok'; } EOF osascript -l JavaScript "$js" "$@" /dev/null 2>&1; then printf '\nERROR: The Apple command line tools are missing (git needs them). Run this line, wait\nuntil the install has finished, then run the setup line again:\n\n xcode-select --install\n\n%s\n' "$HELP" >&2 exit 1 fi if ! find_brew >/dev/null; then NEED="" find_gh >/dev/null || NEED="$NEED\n - GitHub CLI: download the macOS installer from https://cli.github.com and open it." find_claude >/dev/null || NEED="$NEED\n - Claude Code for the terminal: run curl -fsSL https://claude.ai/install.sh | bash" if [ -n "$NEED" ]; then printf '\nERROR: This Mac has no Homebrew, so install these first, then run the setup line again:%b\n\n%s\n' "$NEED" "$HELP" >&2 exit 1 fi fi # --- 1. GitHub CLI --------------------------------------------------------- say "Checking the GitHub CLI (gh)" if ! GH="$(find_gh)"; then echo " gh is not installed." if BREW="$(find_brew)"; then run "$BREW" install gh || fail "Homebrew could not install gh." GH="$(find_gh)" || { [ -n "${DRY_RUN:-}" ] && GH="gh"; } || fail "gh was installed, but it is not found." else printf '\nERROR: The GitHub CLI (gh) is missing, and Homebrew too. Install gh first:\n\n Download the macOS installer from https://cli.github.com and open it.\n\nThen run the setup line again.\n%s\n' "$HELP" >&2 exit 1 fi fi [ -x "$GH" ] && use_dir "$GH" if ! "$GH" auth status --hostname github.com >/dev/null 2>&1 /dev/null /dev/null &2 exit 1 fi run "$BREW" install --cask claude-code || fail "Homebrew could not install Claude Code." if ! CLAUDE="$(find_claude)"; then [ -n "${DRY_RUN:-}" ] && { echo " (dry run: stop here)"; exit 0; } fail "Claude Code was installed, but it is not found." fi fi use_dir "$CLAUDE" echo " Found: $CLAUDE ($(claude_version "$CLAUDE"))" say "Updating Claude Code" REAL="$(readlink -f "$CLAUDE" 2>/dev/null || echo "$CLAUDE")" case "$REAL" in */Caskroom/*) # Use the Homebrew that owns this cask (Apple Silicon or Intel). BREW="${REAL%%/Caskroom/*}/bin/brew" if [ -x "$BREW" ]; then run "$BREW" upgrade --cask claude-code || warn "The Homebrew update failed. The setup goes on with the installed version." else warn "Homebrew is not found, so Claude Code is not updated." fi ;; *) run "$CLAUDE" update || warn "The update failed. The setup goes on with the installed version." ;; esac # --- 4. Check the version -------------------------------------------------- CLAUDE="$(find_claude)" || fail "Claude Code is not found after the update." use_dir "$CLAUDE" VERSION="$(claude_version "$CLAUDE")" [ -n "$VERSION" ] || fail "Claude Code at $CLAUDE does not start." say "Claude Code version: $VERSION" if version_lt "$VERSION" "$MIN_VERSION"; then if [ -n "${DRY_RUN:-}" ]; then echo " (dry run: the update did not run, so the old version is shown)" else fail "Version $VERSION is too old. This setup needs $MIN_VERSION or newer, and the update did not get there. Install the newest version with: curl -fsSL https://claude.ai/install.sh | bash" fi fi # --- 5. Git access for Claude Code ----------------------------------------- say "Checking git access to the repo" if GIT_TERMINAL_PROMPT=0 git ls-remote "https://github.com/$MARKETPLACE_SOURCE.git" HEAD >/dev/null 2>&1; then echo " git can read the repo." else echo " git cannot read the repo yet. The fix is 'gh auth setup-git': git then uses your gh" echo " login for github.com. This changes ~/.gitconfig: other GitHub repos also use this login." ANSWER="${SETUP_GIT:-n}" if [ -z "${SETUP_GIT:-}" ] && can_ask; then ANSWER="$(ask " Do it now? (y/n) ")"; fi case "$ANSWER" in y|Y|yes|j|J|ja) run "$GH" auth setup-git --hostname github.com || warn "gh auth setup-git failed. The plugin install can fail with a login error." ;; *) warn "git is not changed. The plugin install will likely fail. To fix it later, run: gh auth setup-git --hostname github.com" ;; esac fi # --- 6. Install or update the plugin --------------------------------------- LIST="$("$CLAUDE" plugin list 2>&1 &1)"; then warn "Your settings file cannot be read, so the style is not changed: $CURRENT" CURRENT=""; STYLE_OK=0 fi [ -n "$CURRENT" ] && echo " Your current style: $CURRENT" CHOICE="${SETUP_STYLE:-}" [ "$STYLE_OK" = 1 ] || CHOICE="skip" if [ -z "$CHOICE" ] && can_ask; then echo " Which language do you want to chat in with Claude?" echo " 1 English 2 German 3 French 4 Italian 5 Spanish Enter = keep as it is" CHOICE="$(ask " Your choice: ")" fi STYLE="" case "$CHOICE" in 1) STYLE="Simplified Technical" ;; 2) STYLE="Einfache Sprache" ;; 3) STYLE="Langage clair" ;; 4) STYLE="Linguaggio chiaro" ;; 5) STYLE="Lenguaje claro" ;; *) STYLE="" ;; esac if [ -n "$STYLE" ]; then NEW="$PLUGIN:$STYLE" if [ -n "$CURRENT" ] && [ "$CURRENT" != "$NEW" ] && [ -z "${SETUP_STYLE:-}" ]; then case "$(ask " Replace '$CURRENT' with '$NEW'? (y/n) ")" in y|Y|yes|j|J|ja) ;; *) NEW="" ;; esac fi if [ -n "$NEW" ]; then echo " + set outputStyle to \"$NEW\" in $SETTINGS" if [ -z "${DRY_RUN:-}" ]; then mkdir -p "$(dirname "$SETTINGS")" if ! MSG="$(json_key "$SETTINGS" outputStyle "$NEW" 2>&1)"; then warn "The style could not be saved ($MSG). Pick it in Claude Code with /output-style." STYLE_OK=0 fi fi fi else echo " Style not changed. You can pick it any time in Claude Code with /output-style." fi # --- 8. Done --------------------------------------------------------------- if [ "$STYLE_OK" = 1 ]; then say "Done. Restart Claude Code (terminal and desktop app) to load the plugin." else say "Done, except the style (see the warning above). Restart Claude Code to load the plugin." fi DIR="" if [ -z "$(PATH="$ORIG_PATH" command -v claude 2>/dev/null)" ]; then DIR="$(dirname "$CLAUDE")" case "$DIR" in */.nvm/*) echo echo " Claude Code is installed in an nvm Node version ($DIR)." echo " Run 'nvm use' with that Node version before you start claude." DIR="" ;; esac fi if [ -n "$DIR" ]; then echo echo " Your terminal does not find the command 'claude' yet. It is in $DIR." echo " Run this line once, then open a new terminal window:" echo echo " echo 'export PATH=\"$DIR:\$PATH\"' >> ~/.zshrc" fi [ "$STYLE_OK" = 1 ] || exit 2 } main "$@"