-
Notifications
You must be signed in to change notification settings - Fork 27
Expand file tree
/
Copy pathinstall.sh
More file actions
184 lines (164 loc) · 6.92 KB
/
install.sh
File metadata and controls
184 lines (164 loc) · 6.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
#!/usr/bin/env bash
set -euo pipefail
# ── 0. Argument parsing ───────────────────────────────────────────────────────
YES=false
for arg in "$@"; do
case "$arg" in
--yes|-y) YES=true ;;
*) echo "Unknown argument: $arg" >&2; exit 1 ;;
esac
done
# ── 0b. Interactive detection ─────────────────────────────────────────────────
# Non-interactive when /dev/tty is unavailable (Docker, CI pipelines, etc.)
INTERACTIVE=false
if { exec 3</dev/tty && exec 3<&-; } 2>/dev/null; then
INTERACTIVE=true
fi
# Allow CSHIP_TEST_ROOT to override HOME for all path resolution (testability)
ROOT="${CSHIP_TEST_ROOT:-$HOME}"
INSTALL_DIR="$ROOT/.local/bin"
# ── 1. OS / Arch Detection ────────────────────────────────────────────────────
OS=$(uname -s)
ARCH=$(uname -m)
case "$OS" in
Darwin)
case "$ARCH" in
arm64) TARGET="aarch64-apple-darwin" ;;
x86_64) TARGET="x86_64-apple-darwin" ;;
*) echo "Unsupported macOS arch: $ARCH" >&2; exit 1 ;;
esac
;;
Linux)
case "$ARCH" in
x86_64) TARGET="x86_64-unknown-linux-musl" ;;
aarch64) TARGET="aarch64-unknown-linux-musl" ;;
*) echo "Unsupported Linux arch: $ARCH" >&2; exit 1 ;;
esac
;;
*)
echo "Unsupported OS: $OS" >&2; exit 1 ;;
esac
echo "Detected: $OS/$ARCH → target: $TARGET"
# ── 2. Uninstall any existing cship ───────────────────────────────────────────
if command -v cship >/dev/null 2>&1; then
echo "Existing cship found — running uninstall to clean up before upgrade..."
cship uninstall
fi
# ── 3. Download Binary ────────────────────────────────────────────────────────
BINARY_URL="https://github.com/stephenleo/cship/releases/latest/download/cship-${TARGET}"
mkdir -p "$INSTALL_DIR"
echo "Downloading cship from $BINARY_URL ..."
curl -fsSL "$BINARY_URL" -o "${INSTALL_DIR}/cship"
chmod +x "${INSTALL_DIR}/cship"
if [ ! -s "${INSTALL_DIR}/cship" ]; then
echo "Error: downloaded binary is empty — check network or release URL" >&2
rm -f "${INSTALL_DIR}/cship"
exit 1
fi
echo "Installed cship to ${INSTALL_DIR}/cship"
# ── 4. Linux: libsecret-tools check (usage limits dependency) ─────────────────
if [ "$OS" = "Linux" ] && ! command -v secret-tool >/dev/null 2>&1; then
if [ "$YES" = "true" ]; then
sudo apt-get install -y libsecret-tools
elif [ "$INTERACTIVE" = "true" ]; then
printf "Install libsecret-tools? (required for usage limits on Linux) [Y/n] "
read -r answer </dev/tty
case "$answer" in
[Nn]*) echo "Skipping — usage limits module unavailable until installed manually." ;;
*) sudo apt-get install -y libsecret-tools ;;
esac
else
echo "Skipping libsecret-tools (non-interactive). Re-run with --yes or install manually: sudo apt-get install -y libsecret-tools"
fi
fi
# ── 5. Starship detection and optional install ────────────────────────────────
if ! command -v starship >/dev/null 2>&1; then
if [ "$YES" = "true" ]; then
curl -sS https://starship.rs/install.sh | sh -s -- --yes
elif [ "$INTERACTIVE" = "true" ]; then
printf "Starship not found. Install Starship? (required for passthrough modules) [Y/n] "
read -r answer </dev/tty
case "$answer" in
[Nn]*) echo "Skipping Starship install. Native cship modules will still work." ;;
*) curl -sS https://starship.rs/install.sh | sh ;;
esac
else
echo "Skipping Starship install (non-interactive). Re-run with --yes or install manually: curl -sS https://starship.rs/install.sh | sh"
fi
fi
# ── 6. cship.toml — create minimal config (idempotent) ───────────────────────
CSHIP_CONFIG="$ROOT/.config/cship.toml"
mkdir -p "$(dirname "$CSHIP_CONFIG")"
CSHIP_BLOCK='# cship — Claude Code statusline
# Full config reference: https://cship.dev
[cship]
lines = [
"$directory$git_branch$git_status$python$nodejs$rust",
"$cship.model $cship.cost $cship.context_bar $cship.usage_limits"
]
[cship.model]
symbol = "🤖 "
style = "bold cyan"
[cship.context_bar]
width = 10
style = "fg:#7dcfff"
warn_threshold = 40.0
warn_style = "fg:#e0af68"
critical_threshold = 70.0
critical_style = "bold fg:#f7768e"
[cship.cost]
symbol = "💰 "
style = "fg:#a9b1d6"
warn_threshold = 2.0
warn_style = "fg:#e0af68"
critical_threshold = 5.0
critical_style = "bold fg:#f7768e"
[cship.usage_limits]
five_hour_format = "⌛ 5h {pct}% ({reset})"
seven_day_format = "📅 7d {pct}% ({reset})"
separator = " "
warn_threshold = 60.0
warn_style = "fg:#e0af68"
critical_threshold = 80.0
critical_style = "bold fg:#f7768e"
'
if [ -f "$CSHIP_CONFIG" ]; then
echo "cship.toml already exists at $CSHIP_CONFIG, skipping."
else
printf '%s' "$CSHIP_BLOCK" > "$CSHIP_CONFIG"
echo "Created minimal cship config at $CSHIP_CONFIG"
fi
# ── 7. ~/.claude/settings.json — wire statusline (via python3) ───────────────
SETTINGS="$ROOT/.claude/settings.json"
if ! command -v python3 >/dev/null 2>&1; then
echo "Warning: python3 not found. Skipping settings.json update."
echo "To wire cship manually, add \"statusLine\": {\"type\": \"command\", \"command\": \"cship\"} to $SETTINGS"
elif [ -f "$SETTINGS" ]; then
python3 - "$SETTINGS" <<'PYEOF' || echo "Warning: failed to update settings.json — add statusLine manually."
import json, sys
path = sys.argv[1]
try:
with open(path) as f:
d = json.load(f)
except (json.JSONDecodeError, ValueError) as e:
print('Warning: ' + path + ' contains invalid JSON: ' + str(e))
sys.exit(1)
if 'statusLine' not in d:
d['statusLine'] = {'type': 'command', 'command': 'cship'}
with open(path, 'w') as f:
json.dump(d, f, indent=2)
f.write('\n')
print('Added statusLine config to ' + path)
else:
print('"statusLine" already set in ' + path + ', skipping.')
PYEOF
else
echo "settings.json not found at $SETTINGS — skipping (Claude Code may not be installed yet)."
fi
# ── 8. First-run preview ──────────────────────────────────────────────────────
echo ""
echo "Running cship explain..."
"$INSTALL_DIR/cship" explain || true
echo ""
echo "cship installation complete!"
echo "If ~/.local/bin is not in your PATH, add: export PATH=\"\$HOME/.local/bin:\$PATH\""