-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinstall.sh
More file actions
executable file
·119 lines (104 loc) · 4.04 KB
/
Copy pathinstall.sh
File metadata and controls
executable file
·119 lines (104 loc) · 4.04 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
#!/usr/bin/env bash
#
# codex-usage installer.
#
# One-liner:
# curl -fsSL https://raw.githubusercontent.com/mbogdan0/codex-usage/main/install.sh | bash
#
# Installs the `codex-usage` command into ~/.local/bin and adds a shell alias.
# Sensible defaults; pass -y (or set CODEX_USAGE_YES=1) to skip confirmation.
#
# Overridable via env:
# INSTALL_DIR where the command is placed (default: ~/.local/bin)
# BRANCH branch to download from (default: main)
set -euo pipefail
REPO="mbogdan0/codex-usage"
BRANCH="${BRANCH:-main}"
INSTALL_DIR="${INSTALL_DIR:-$HOME/.local/bin}"
CMD_NAME="codex-usage"
RAW_URL="https://raw.githubusercontent.com/${REPO}/${BRANCH}/codex-usage.sh"
ASSUME_YES="${CODEX_USAGE_YES:-0}"
[ "${1:-}" = "-y" ] && ASSUME_YES=1
# ── Pretty output ────────────────────────────────────────
if [ -t 1 ]; then
BOLD="$(printf '\033[1m')"; DIM="$(printf '\033[2m')"
GREEN="$(printf '\033[32m')"; YELLOW="$(printf '\033[33m')"; RESET="$(printf '\033[0m')"
else
BOLD=""; DIM=""; GREEN=""; YELLOW=""; RESET=""
fi
info() { printf '%s\n' "$*"; }
ok() { printf '%s✓%s %s\n' "$GREEN" "$RESET" "$*"; }
warn() { printf '%s!%s %s\n' "$YELLOW" "$RESET" "$*"; }
die() { printf 'Error: %s\n' "$*" >&2; exit 1; }
# ── Locate the source script (local clone or download) ───
resolve_source() {
local self_dir
self_dir="$(cd "$(dirname "${BASH_SOURCE[0]:-$0}")" 2>/dev/null && pwd || true)"
if [ -n "$self_dir" ] && [ -f "$self_dir/codex-usage.sh" ]; then
SRC="$self_dir/codex-usage.sh" # running from a clone
SRC_KIND="local"
else
SRC="$(mktemp -t codex-usage.XXXXXX)" # piped via curl → download it
SRC_KIND="download"
if command -v curl >/dev/null 2>&1; then
curl -fsSL "$RAW_URL" -o "$SRC" || die "download failed: $RAW_URL"
elif command -v wget >/dev/null 2>&1; then
wget -qO "$SRC" "$RAW_URL" || die "download failed: $RAW_URL"
else
die "need curl or wget to download the script"
fi
trap 'rm -f "$SRC"' EXIT
fi
}
# ── Detect shell rc for the alias ────────────────────────
detect_rc() {
case "$(basename "${SHELL:-}")" in
zsh) RC="$HOME/.zshrc" ;;
bash) [ "$(uname)" = "Darwin" ] && RC="$HOME/.bash_profile" || RC="$HOME/.bashrc" ;;
*) RC="" ;;
esac
}
resolve_source
detect_rc
TARGET="$INSTALL_DIR/$CMD_NAME"
ALIAS_LINE="alias ${CMD_NAME}=\"${TARGET}\""
info ""
info "${BOLD}codex-usage installer${RESET}"
info " ${DIM}source :${RESET} $SRC_KIND"
info " ${DIM}command:${RESET} $TARGET"
info " ${DIM}alias :${RESET} ${RC:-<manual — shell not detected>}"
info ""
if [ "$ASSUME_YES" != "1" ] && [ -e /dev/tty ]; then
printf 'Proceed with these defaults? [Y/n] '
read -r reply < /dev/tty || reply=""
case "$reply" in [nN]*) info "Aborted."; exit 0 ;; esac
fi
# ── Install the command ──────────────────────────────────
mkdir -p "$INSTALL_DIR"
install -m 0755 "$SRC" "$TARGET" 2>/dev/null || { cp "$SRC" "$TARGET"; chmod 0755 "$TARGET"; }
ok "Installed $CMD_NAME → $TARGET"
# ── Wire up the alias (idempotent) ───────────────────────
if [ -n "$RC" ]; then
if [ -f "$RC" ] && grep -q "alias ${CMD_NAME}=" "$RC"; then
ok "Alias already present in $RC"
else
printf '\n# codex-usage\n%s\n' "$ALIAS_LINE" >> "$RC"
ok "Added alias to $RC"
fi
ACTIVATE="source \"$RC\""
else
warn "Could not detect your shell rc. Add this line manually:"
info " $ALIAS_LINE"
ACTIVATE="add the alias above, then reload your shell"
fi
# ── Dependency check (script needs these at runtime) ─────
missing=""
for c in codex jq python3; do
command -v "$c" >/dev/null 2>&1 || missing="$missing $c"
done
[ -n "$missing" ] && warn "Missing runtime dependencies:$missing"
info ""
ok "Done. To start using it now:"
info " ${BOLD}${ACTIVATE}${RESET}"
info " ${BOLD}${CMD_NAME}${RESET}"
info ""