|
10 | 10 |
|
11 | 11 | set -euo pipefail |
12 | 12 |
|
13 | | -REPO="fulll/github-code-search" |
14 | 13 | BINARY_NAME="github-code-search" |
15 | | -INSTALL_DIR="${INSTALL_DIR:-/usr/local/bin}" |
16 | | -GITHUB_API="https://api.github.com" |
17 | | - |
18 | | -# ─── Detect OS ──────────────────────────────────────────────────────────────── |
19 | | - |
20 | | -case "$(uname -s)" in |
21 | | - Linux*) OS="linux" ;; |
22 | | - Darwin*) OS="macos" ;; |
23 | | - MINGW*|MSYS*|CYGWIN*) OS="windows" ;; |
24 | | - *) |
25 | | - echo "error: unsupported OS: $(uname -s)" >&2 |
26 | | - exit 1 |
27 | | - ;; |
28 | | -esac |
29 | | - |
30 | | -# ─── Detect architecture ────────────────────────────────────────────────────── |
31 | | - |
32 | | -MACHINE="$(uname -m)" |
33 | | -case "$MACHINE" in |
34 | | - x86_64|amd64) ARCH="x64" ;; |
35 | | - arm64|aarch64) ARCH="arm64" ;; |
36 | | - *) |
37 | | - echo "error: unsupported architecture: $MACHINE" >&2 |
38 | | - exit 1 |
39 | | - ;; |
40 | | -esac |
41 | | - |
42 | | -ARTIFACT="${BINARY_NAME}-${OS}-${ARCH}" |
43 | | -[ "$OS" = "windows" ] && ARTIFACT="${ARTIFACT}.exe" |
44 | | - |
45 | | -# ─── Resolve version ────────────────────────────────────────────────────────── |
46 | | - |
47 | | -if [ -n "${VERSION:-}" ]; then |
48 | | - TAG="$VERSION" |
49 | | -else |
50 | | - echo "Detecting latest release…" |
51 | | - TAG=$(curl -fsSL "${GITHUB_API}/repos/${REPO}/releases/latest" \ |
52 | | - | grep '"tag_name"' \ |
53 | | - | sed -E 's/.*"tag_name": *"([^"]+)".*/\1/') |
54 | | -fi |
55 | 14 |
|
56 | | -echo "Installing ${BINARY_NAME} ${TAG} (${OS}/${ARCH})…" |
| 15 | +# ─── Shell completions ──────────────────────────────────────────────────────── |
| 16 | +# Defined early so it can be sourced and tested independently (INSTALL_SH_TEST=1). |
57 | 17 |
|
58 | | -# ─── Download ───────────────────────────────────────────────────────────────── |
| 18 | +install_completions() { |
| 19 | + local bin="$1" |
| 20 | + local shell_name |
| 21 | + shell_name="$(basename "${SHELL:-}")" |
59 | 22 |
|
60 | | -DOWNLOAD_URL="https://github.com/${REPO}/releases/download/${TAG}/${ARTIFACT}" |
61 | | -TMP="$(mktemp)" |
62 | | -curl -fsSL --progress-bar -o "$TMP" "$DOWNLOAD_URL" |
63 | | -chmod +x "$TMP" |
| 23 | + case "$shell_name" in |
| 24 | + fish) |
| 25 | + local comp_dir="${XDG_CONFIG_HOME:-$HOME/.config}/fish/completions" |
| 26 | + local comp_file="${comp_dir}/github-code-search.fish" |
| 27 | + mkdir -p "$comp_dir" |
| 28 | + "$bin" completions --shell fish > "$comp_file" |
| 29 | + echo "" |
| 30 | + echo "✓ Fish completions installed at ${comp_file}" |
| 31 | + ;; |
| 32 | + zsh) |
| 33 | + local comp_dir="${ZDOTDIR:-$HOME}/.zfunc" |
| 34 | + local comp_file="${comp_dir}/_github-code-search" |
| 35 | + mkdir -p "$comp_dir" |
| 36 | + "$bin" completions --shell zsh > "$comp_file" |
| 37 | + echo "" |
| 38 | + echo "✓ Zsh completions installed at ${comp_file}" |
| 39 | + echo " Make sure ${comp_dir} is on your fpath. Add to ~/.zshrc if needed:" |
| 40 | + echo " fpath=(${comp_dir} \$fpath)" |
| 41 | + echo " autoload -Uz compinit && compinit" |
| 42 | + ;; |
| 43 | + bash) |
| 44 | + local comp_dir="${XDG_DATA_HOME:-$HOME/.local/share}/bash-completion/completions" |
| 45 | + local comp_file="${comp_dir}/github-code-search" |
| 46 | + mkdir -p "$comp_dir" |
| 47 | + "$bin" completions --shell bash > "$comp_file" |
| 48 | + echo "" |
| 49 | + echo "✓ Bash completions installed at ${comp_file}" |
| 50 | + ;; |
| 51 | + *) |
| 52 | + echo "" |
| 53 | + echo " Shell completions are available for bash, zsh and fish." |
| 54 | + echo " Run the following to generate your completion script:" |
| 55 | + echo " ${BINARY_NAME} completions --shell <bash|zsh|fish>" |
| 56 | + ;; |
| 57 | + esac |
| 58 | +} |
64 | 59 |
|
65 | | -# ─── Install ────────────────────────────────────────────────────────────────── |
| 60 | +# ─── Main installation ──────────────────────────────────────────────────────── |
| 61 | +# Skipped when sourced for testing (export INSTALL_SH_TEST=1 before sourcing). |
66 | 62 |
|
67 | | -DEST="${INSTALL_DIR}/${BINARY_NAME}" |
68 | | -if [ -w "$INSTALL_DIR" ]; then |
69 | | - mv "$TMP" "$DEST" |
70 | | -else |
71 | | - echo " (sudo required for ${INSTALL_DIR})" |
72 | | - sudo mv "$TMP" "$DEST" |
73 | | -fi |
| 63 | +if [ -z "${INSTALL_SH_TEST:-}" ]; then |
| 64 | + |
| 65 | + REPO="fulll/github-code-search" |
| 66 | + INSTALL_DIR="${INSTALL_DIR:-/usr/local/bin}" |
| 67 | + GITHUB_API="https://api.github.com" |
| 68 | + |
| 69 | + # ─── Detect OS ────────────────────────────────────────────────────────────── |
| 70 | + |
| 71 | + case "$(uname -s)" in |
| 72 | + Linux*) OS="linux" ;; |
| 73 | + Darwin*) OS="macos" ;; |
| 74 | + MINGW*|MSYS*|CYGWIN*) OS="windows" ;; |
| 75 | + *) |
| 76 | + echo "error: unsupported OS: $(uname -s)" >&2 |
| 77 | + exit 1 |
| 78 | + ;; |
| 79 | + esac |
| 80 | + |
| 81 | + # ─── Detect architecture ──────────────────────────────────────────────────── |
| 82 | + |
| 83 | + MACHINE="$(uname -m)" |
| 84 | + case "$MACHINE" in |
| 85 | + x86_64|amd64) ARCH="x64" ;; |
| 86 | + arm64|aarch64) ARCH="arm64" ;; |
| 87 | + *) |
| 88 | + echo "error: unsupported architecture: $MACHINE" >&2 |
| 89 | + exit 1 |
| 90 | + ;; |
| 91 | + esac |
| 92 | + |
| 93 | + ARTIFACT="${BINARY_NAME}-${OS}-${ARCH}" |
| 94 | + [ "$OS" = "windows" ] && ARTIFACT="${ARTIFACT}.exe" |
| 95 | + |
| 96 | + # ─── Resolve version ──────────────────────────────────────────────────────── |
74 | 97 |
|
75 | | -echo "✓ ${BINARY_NAME} ${TAG} installed at ${DEST}" |
76 | | -echo "" |
77 | | -echo " Remember to export your GitHub token:" |
78 | | -echo " export GITHUB_TOKEN=ghp_xxxxxxxxxxxxxxxxxxxx" |
| 98 | + if [ -n "${VERSION:-}" ]; then |
| 99 | + TAG="$VERSION" |
| 100 | + else |
| 101 | + echo "Detecting latest release…" |
| 102 | + TAG=$(curl -fsSL "${GITHUB_API}/repos/${REPO}/releases/latest" \ |
| 103 | + | grep '"tag_name"' \ |
| 104 | + | sed -E 's/.*"tag_name": *"([^"]+)".*/\1/') |
| 105 | + fi |
| 106 | + |
| 107 | + echo "Installing ${BINARY_NAME} ${TAG} (${OS}/${ARCH})…" |
| 108 | + |
| 109 | + # ─── Download ─────────────────────────────────────────────────────────────── |
| 110 | + |
| 111 | + DOWNLOAD_URL="https://github.com/${REPO}/releases/download/${TAG}/${ARTIFACT}" |
| 112 | + TMP="$(mktemp)" |
| 113 | + curl -fsSL --progress-bar -o "$TMP" "$DOWNLOAD_URL" |
| 114 | + chmod +x "$TMP" |
| 115 | + |
| 116 | + # ─── Install ──────────────────────────────────────────────────────────────── |
| 117 | + |
| 118 | + DEST="${INSTALL_DIR}/${BINARY_NAME}" |
| 119 | + if [ -w "$INSTALL_DIR" ]; then |
| 120 | + mv "$TMP" "$DEST" |
| 121 | + else |
| 122 | + echo " (sudo required for ${INSTALL_DIR})" |
| 123 | + sudo mv "$TMP" "$DEST" |
| 124 | + fi |
| 125 | + |
| 126 | + echo "✓ ${BINARY_NAME} ${TAG} installed at ${DEST}" |
| 127 | + echo "" |
| 128 | + echo " Remember to export your GitHub token:" |
| 129 | + echo " export GITHUB_TOKEN=ghp_xxxxxxxxxxxxxxxxxxxx" |
| 130 | + |
| 131 | + install_completions "$DEST" |
| 132 | + |
| 133 | +fi |
0 commit comments