Skip to content

Commit d7a3b11

Browse files
author
Евгений Балякин
committed
fix: handle missing release in installer
1 parent 121a729 commit d7a3b11

1 file changed

Lines changed: 32 additions & 10 deletions

File tree

install.sh

Lines changed: 32 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ REPO="balyakin/sudocheck"
55
INSTALL_DIR="${INSTALL_DIR:-/usr/local/bin}"
66
TMP_DIR="${TMPDIR:-/tmp}/sudocheck-install"
77
API_URL="https://api.github.com/repos/$REPO/releases/latest"
8+
MODULE="github.com/$REPO"
89

910
os="$(uname -s | tr '[:upper:]' '[:lower:]')"
1011
uname_arch="$(uname -m)"
@@ -28,7 +29,33 @@ case "$arch" in
2829
;;
2930
esac
3031

31-
release_json="$(curl -fsSL "$API_URL")"
32+
install_binary() {
33+
source_path="$1"
34+
if [ -w "$INSTALL_DIR" ]; then
35+
mv "$source_path" "$INSTALL_DIR/sudocheck"
36+
else
37+
sudo mv "$source_path" "$INSTALL_DIR/sudocheck"
38+
fi
39+
}
40+
41+
install_from_source() {
42+
if ! command -v go >/dev/null 2>&1; then
43+
echo "no published release found and Go is not installed" >&2
44+
echo "create a GitHub Release with binaries or install Go and run:" >&2
45+
echo " go install $MODULE@latest" >&2
46+
exit 1
47+
fi
48+
49+
echo "no published release found; installing from source with go install" >&2
50+
GOBIN="$TMP_DIR/bin" go install "$MODULE@latest"
51+
install_binary "$TMP_DIR/bin/sudocheck"
52+
echo "sudocheck installed to $INSTALL_DIR/sudocheck"
53+
exit 0
54+
}
55+
56+
if ! release_json="$(curl -fsSL "$API_URL")"; then
57+
install_from_source
58+
fi
3259
latest="$(printf '%s\n' "$release_json" | grep '"tag_name"' | cut -d '"' -f 4 | head -n 1)"
3360
archive_url="$(
3461
printf '%s\n' "$release_json" |
@@ -60,15 +87,14 @@ checksum_url="$(
6087
)"
6188

6289
if [ -z "$latest" ]; then
63-
echo "could not determine latest release tag for $REPO" >&2
64-
exit 1
90+
install_from_source
6591
fi
6692

6793
if [ -z "$archive_url" ]; then
68-
echo "could not find release archive for ${os}/${arch}" >&2
94+
echo "could not find release archive for ${os}/${arch}; trying source install" >&2
6995
echo "available assets:" >&2
7096
printf '%s\n' "$release_json" | grep '"browser_download_url":' | cut -d '"' -f 4 >&2
71-
exit 1
97+
install_from_source
7298
fi
7399

74100
if [ -z "$checksum_url" ]; then
@@ -100,10 +126,6 @@ fi
100126
printf '%s %s\n' "$checksum" "$archive" | sha256sum -c -
101127
tar xzf "$archive"
102128

103-
if [ -w "$INSTALL_DIR" ]; then
104-
mv sudocheck "$INSTALL_DIR/sudocheck"
105-
else
106-
sudo mv sudocheck "$INSTALL_DIR/sudocheck"
107-
fi
129+
install_binary sudocheck
108130

109131
echo "sudocheck $latest installed to $INSTALL_DIR/sudocheck"

0 commit comments

Comments
 (0)