-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathinstall_gh.sh
More file actions
63 lines (53 loc) · 2.08 KB
/
install_gh.sh
File metadata and controls
63 lines (53 loc) · 2.08 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
#!/bin/bash
# Install GitHub CLI (gh) to ~/.local/bin with an auth-login wrapper.
#
# - Fetches the latest 2.x release from the GitHub API
# - Installs to ~/.local/bin/gh.real
# - Creates a wrapper at ~/.local/bin/gh that intercepts `gh auth login`
# to skip interactive prompts (arrow-key menus break in xterm.js PTY)
set -euo pipefail
INSTALL_DIR="$HOME/.local/bin"
mkdir -p "$INSTALL_DIR"
# Fetch latest release tag
GH_VERSION=$(curl -fsSL "https://api.github.com/repos/cli/cli/releases/latest" \
| python3 -c "import sys, json; print(json.load(sys.stdin)['tag_name'].lstrip('v'))")
echo "Installing GitHub CLI v${GH_VERSION}"
# Detect OS and architecture
_UNAME=$(uname -s)
_ARCH=$(uname -m)
case "$_ARCH" in
x86_64) _ARCH="amd64" ;;
aarch64|arm64) _ARCH="arm64" ;;
esac
if [ "$_UNAME" = "Darwin" ]; then
GH_ASSET="gh_${GH_VERSION}_macOS_${_ARCH}.zip"
curl -fsSL "https://github.com/cli/cli/releases/download/v${GH_VERSION}/${GH_ASSET}" \
-o /tmp/gh.zip
unzip -q /tmp/gh.zip -d /tmp/gh_extract
mv "/tmp/gh_extract/gh_${GH_VERSION}_macOS_${_ARCH}/bin/gh" "$INSTALL_DIR/gh"
rm -rf /tmp/gh.zip /tmp/gh_extract
else
GH_ASSET="gh_${GH_VERSION}_linux_${_ARCH}.tar.gz"
curl -fsSL "https://github.com/cli/cli/releases/download/v${GH_VERSION}/${GH_ASSET}" \
-o /tmp/gh.tar.gz
tar -xzf /tmp/gh.tar.gz -C /tmp
mv "/tmp/gh_${GH_VERSION}_linux_${_ARCH}/bin/gh" "$INSTALL_DIR/gh"
rm -rf /tmp/gh.tar.gz "/tmp/gh_${GH_VERSION}_linux_${_ARCH}"
fi
chmod +x "$INSTALL_DIR/gh"
# Set git protocol to HTTPS
"$INSTALL_DIR/gh" config set git_protocol https 2>/dev/null || true
# Create wrapper that intercepts `gh auth login` to avoid interactive prompts
cat > "$INSTALL_DIR/gh.wrapper" << 'WRAPPER'
#!/bin/bash
if [ "$1" = "auth" ] && [ "$2" = "login" ]; then
shift 2
printf "Y\\n" | ~/.local/bin/gh.real auth login -h github.com -p https -w --skip-ssh-key "$@"
exit 0
fi
exec ~/.local/bin/gh.real "$@"
WRAPPER
mv "$INSTALL_DIR/gh" "$INSTALL_DIR/gh.real"
mv "$INSTALL_DIR/gh.wrapper" "$INSTALL_DIR/gh"
chmod +x "$INSTALL_DIR/gh"
echo "GitHub CLI v${GH_VERSION} installed to $INSTALL_DIR"