Skip to content

Commit 04aa42a

Browse files
committed
feat: add install script for automated binary installation
1 parent 6e4f6b3 commit 04aa42a

File tree

2 files changed

+123
-0
lines changed

2 files changed

+123
-0
lines changed

README.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,18 @@ yay -S snitch-bin
3838
paru -S snitch-bin
3939
```
4040

41+
### shell script
42+
43+
```bash
44+
curl -sSL https://raw.githubusercontent.com/karol-broda/snitch/master/install.sh | sh
45+
```
46+
47+
installs to `~/.local/bin` if available, otherwise `/usr/local/bin`. override with:
48+
49+
```bash
50+
curl -sSL https://raw.githubusercontent.com/karol-broda/snitch/master/install.sh | INSTALL_DIR=~/bin sh
51+
```
52+
4153
### binary
4254

4355
download from [releases](https://github.com/karol-broda/snitch/releases):
@@ -51,6 +63,7 @@ sudo mv snitch /usr/local/bin/
5163
```
5264

5365
> **macos:** if blocked with "cannot be opened because the developer cannot be verified", run:
66+
>
5467
> ```bash
5568
> xattr -d com.apple.quarantine /usr/local/bin/snitch
5669
> ```

install.sh

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
#!/bin/sh
2+
set -e
3+
4+
REPO="karol-broda/snitch"
5+
BINARY_NAME="snitch"
6+
7+
# allow override via environment
8+
INSTALL_DIR="${INSTALL_DIR:-}"
9+
10+
detect_install_dir() {
11+
if [ -n "$INSTALL_DIR" ]; then
12+
echo "$INSTALL_DIR"
13+
return
14+
fi
15+
16+
# prefer user-local directory if it exists and is in PATH
17+
if [ -d "$HOME/.local/bin" ] && echo "$PATH" | grep -q "$HOME/.local/bin"; then
18+
echo "$HOME/.local/bin"
19+
return
20+
fi
21+
22+
# fallback to /usr/local/bin
23+
echo "/usr/local/bin"
24+
}
25+
26+
detect_os() {
27+
os=$(uname -s | tr '[:upper:]' '[:lower:]')
28+
case "$os" in
29+
darwin) echo "darwin" ;;
30+
linux) echo "linux" ;;
31+
*)
32+
echo "error: unsupported operating system: $os" >&2
33+
exit 1
34+
;;
35+
esac
36+
}
37+
38+
detect_arch() {
39+
arch=$(uname -m)
40+
case "$arch" in
41+
x86_64|amd64) echo "amd64" ;;
42+
aarch64|arm64) echo "arm64" ;;
43+
armv7l) echo "armv7" ;;
44+
*)
45+
echo "error: unsupported architecture: $arch" >&2
46+
exit 1
47+
;;
48+
esac
49+
}
50+
51+
fetch_latest_version() {
52+
version=$(curl -sL "https://api.github.com/repos/${REPO}/releases/latest" | grep '"tag_name"' | head -1 | cut -d'"' -f4)
53+
if [ -z "$version" ]; then
54+
echo "error: failed to fetch latest version" >&2
55+
exit 1
56+
fi
57+
echo "$version"
58+
}
59+
60+
main() {
61+
os=$(detect_os)
62+
arch=$(detect_arch)
63+
install_dir=$(detect_install_dir)
64+
version=$(fetch_latest_version)
65+
version_no_v="${version#v}"
66+
67+
archive_name="${BINARY_NAME}_${version_no_v}_${os}_${arch}.tar.gz"
68+
download_url="https://github.com/${REPO}/releases/download/${version}/${archive_name}"
69+
70+
echo "installing ${BINARY_NAME} ${version} for ${os}/${arch}..."
71+
72+
tmp_dir=$(mktemp -d)
73+
trap 'rm -rf "$tmp_dir"' EXIT
74+
75+
echo "downloading ${download_url}..."
76+
if ! curl -sL --fail "$download_url" -o "${tmp_dir}/${archive_name}"; then
77+
echo "error: failed to download ${download_url}" >&2
78+
exit 1
79+
fi
80+
81+
echo "extracting..."
82+
tar -xzf "${tmp_dir}/${archive_name}" -C "$tmp_dir"
83+
84+
if [ ! -f "${tmp_dir}/${BINARY_NAME}" ]; then
85+
echo "error: binary not found in archive" >&2
86+
exit 1
87+
fi
88+
89+
# remove macos quarantine attribute
90+
if [ "$os" = "darwin" ]; then
91+
xattr -d com.apple.quarantine "${tmp_dir}/${BINARY_NAME}" 2>/dev/null || true
92+
fi
93+
94+
# install binary
95+
if [ -w "$install_dir" ]; then
96+
mv "${tmp_dir}/${BINARY_NAME}" "${install_dir}/${BINARY_NAME}"
97+
else
98+
echo "elevated permissions required to install to ${install_dir}"
99+
sudo mv "${tmp_dir}/${BINARY_NAME}" "${install_dir}/${BINARY_NAME}"
100+
fi
101+
102+
chmod +x "${install_dir}/${BINARY_NAME}"
103+
104+
echo "installed ${BINARY_NAME} to ${install_dir}/${BINARY_NAME}"
105+
echo ""
106+
echo "run '${BINARY_NAME} --help' to get started"
107+
}
108+
109+
main
110+

0 commit comments

Comments
 (0)