Skip to content

Commit df39619

Browse files
authored
chore: move CLI install script to scripts/install.sh (#10)
1 parent dafbc84 commit df39619

File tree

1 file changed

+143
-0
lines changed

1 file changed

+143
-0
lines changed

scripts/install.sh

Lines changed: 143 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
1+
#!/bin/sh
2+
set -e
3+
4+
# OneCLI CLI - manage agents, secrets, and configuration from the terminal
5+
# Source: https://github.com/onecli/onecli-cli
6+
# License: Apache 2.0
7+
#
8+
# Usage: curl -fsSL https://onecli.sh/cli/install | sh
9+
#
10+
# This script downloads the latest onecli binary from GitHub Releases,
11+
# installs it to /usr/local/bin (or ~/.local/bin), and verifies the install.
12+
13+
REPO="onecli/onecli-cli"
14+
BINARY="onecli"
15+
16+
main() {
17+
# Detect OS
18+
OS=$(uname -s | tr '[:upper:]' '[:lower:]')
19+
case "$OS" in
20+
darwin) OS="darwin" ;;
21+
linux) OS="linux" ;;
22+
*)
23+
echo "Error: unsupported operating system: $OS" >&2
24+
exit 1
25+
;;
26+
esac
27+
28+
# Detect architecture
29+
ARCH=$(uname -m)
30+
case "$ARCH" in
31+
x86_64|amd64) ARCH="amd64" ;;
32+
arm64|aarch64) ARCH="arm64" ;;
33+
*)
34+
echo "Error: unsupported architecture: $ARCH" >&2
35+
exit 1
36+
;;
37+
esac
38+
39+
echo "Detected: $OS/$ARCH"
40+
41+
# Fetch latest release tag from GitHub API (public repo, no auth needed)
42+
echo "Fetching latest release..."
43+
LATEST=$(curl -fsSL "https://api.github.com/repos/$REPO/releases/latest" | grep '"tag_name"' | sed -E 's/.*"tag_name": *"([^"]+)".*/\1/')
44+
45+
if [ -z "$LATEST" ]; then
46+
echo "Error: could not determine latest release" >&2
47+
exit 1
48+
fi
49+
50+
VERSION="${LATEST#v}"
51+
echo "Latest version: $VERSION"
52+
53+
# Download archive from GitHub Releases
54+
ARCHIVE="${BINARY}_${VERSION}_${OS}_${ARCH}.tar.gz"
55+
DOWNLOAD_URL="https://github.com/$REPO/releases/download/$LATEST/$ARCHIVE"
56+
TMPDIR=$(mktemp -d)
57+
trap 'rm -rf "$TMPDIR"' EXIT
58+
59+
echo "Downloading $ARCHIVE..."
60+
HTTP_CODE=$(curl -fsSL -w "%{http_code}" -o "$TMPDIR/$ARCHIVE" "$DOWNLOAD_URL" 2>/dev/null) || true
61+
62+
if [ ! -f "$TMPDIR/$ARCHIVE" ] || [ "$HTTP_CODE" = "404" ]; then
63+
echo "Error: failed to download $ARCHIVE" >&2
64+
echo "Check available releases at https://github.com/$REPO/releases" >&2
65+
exit 1
66+
fi
67+
68+
# Extract binary
69+
tar -xzf "$TMPDIR/$ARCHIVE" -C "$TMPDIR"
70+
71+
# Install
72+
if [ -w /usr/local/bin ]; then
73+
INSTALL_DIR="/usr/local/bin"
74+
elif [ -d "$HOME/.local/bin" ] || mkdir -p "$HOME/.local/bin" 2>/dev/null; then
75+
INSTALL_DIR="$HOME/.local/bin"
76+
else
77+
echo "Error: cannot find a writable install directory" >&2
78+
echo "Try: sudo sh -c 'curl -fsSL https://onecli.sh/cli/install | sh'" >&2
79+
exit 1
80+
fi
81+
82+
cp "$TMPDIR/$BINARY" "$INSTALL_DIR/$BINARY"
83+
chmod +x "$INSTALL_DIR/$BINARY"
84+
85+
echo ""
86+
echo "$BINARY $VERSION installed to $INSTALL_DIR/$BINARY"
87+
88+
# Ensure install dir is in PATH
89+
PATH_ADDED=""
90+
SHELL_RC=""
91+
case ":$PATH:" in
92+
*":$INSTALL_DIR:"*) ;;
93+
*)
94+
PATH_ADDED="1"
95+
PATH_LINE="export PATH=\"$INSTALL_DIR:\$PATH\""
96+
case "${SHELL##*/}" in
97+
zsh) SHELL_RC="$HOME/.zshrc" ;;
98+
bash)
99+
if [ -f "$HOME/.bash_profile" ]; then
100+
SHELL_RC="$HOME/.bash_profile"
101+
else
102+
SHELL_RC="$HOME/.bashrc"
103+
fi
104+
;;
105+
esac
106+
if [ -n "$SHELL_RC" ]; then
107+
if ! grep -q "$INSTALL_DIR" "$SHELL_RC" 2>/dev/null; then
108+
echo "" >> "$SHELL_RC"
109+
echo "# OneCLI" >> "$SHELL_RC"
110+
echo "$PATH_LINE" >> "$SHELL_RC"
111+
fi
112+
echo "PATH configured in $SHELL_RC"
113+
else
114+
echo ""
115+
echo "Add $INSTALL_DIR to your PATH:"
116+
echo " export PATH=\"$INSTALL_DIR:\$PATH\""
117+
fi
118+
export PATH="$INSTALL_DIR:$PATH"
119+
;;
120+
esac
121+
122+
# Verify installation
123+
echo ""
124+
"$INSTALL_DIR/$BINARY" version 2>/dev/null || echo "$BINARY installed successfully"
125+
126+
echo ""
127+
echo "Get started:"
128+
echo " onecli auth login --api-key oc_..."
129+
echo " onecli agents list"
130+
echo " onecli secrets list"
131+
132+
# Remind user to reload shell if PATH was just added
133+
if [ -n "$PATH_ADDED" ] && [ -n "$SHELL_RC" ]; then
134+
echo ""
135+
echo "To start using $BINARY, run:"
136+
echo ""
137+
echo " source $SHELL_RC"
138+
echo ""
139+
echo "Or open a new terminal."
140+
fi
141+
}
142+
143+
main

0 commit comments

Comments
 (0)