-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathinstall.sh
More file actions
106 lines (88 loc) · 2.84 KB
/
Copy pathinstall.sh
File metadata and controls
106 lines (88 loc) · 2.84 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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
#!/bin/sh
set -e
# Configuration
OWNER="KDM-cli"
REPO="ghx"
BINARY_NAME="ghx"
# Find OS and Architecture
OS=$(uname -s | tr '[:upper:]' '[:lower:]')
ARCH=$(uname -m)
case "$ARCH" in
x86_64|amd64)
ARCH="x86_64"
;;
arm64|aarch64)
ARCH="arm64"
;;
*)
echo "Unsupported architecture: $ARCH"
exit 1
;;
esac
case "$OS" in
darwin)
OS="Darwin"
;;
linux)
OS="Linux"
;;
*)
echo "Unsupported operating system: $OS"
exit 1
;;
esac
# Fetch latest release tag name from GitHub API
echo "Finding the latest release..."
LATEST_TAG=$(curl -s "https://api.github.com/repos/$OWNER/$REPO/releases/latest" | grep '"tag_name":' | sed -E 's/.*"([^"]+)".*/\1/')
if [ -z "$LATEST_TAG" ]; then
echo "Error: Could not retrieve latest release tag. Make sure a release tag has been pushed."
exit 1
fi
echo "Latest release is $LATEST_TAG"
# Construct download URL (matching GoReleaser naming format)
TARBALL="${BINARY_NAME}_${LATEST_TAG#v}_${OS}_${ARCH}.tar.gz"
DOWNLOAD_URL="https://github.com/$OWNER/$REPO/releases/download/$LATEST_TAG/$TARBALL"
# Create a temporary directory for downloading and extracting
TMP_DIR=$(mktemp -d)
clean_up() {
rm -rf "$TMP_DIR"
}
trap clean_up EXIT
echo "Downloading $DOWNLOAD_URL..."
if ! curl -sSfL "$DOWNLOAD_URL" -o "$TMP_DIR/$TARBALL"; then
echo "Error: Failed to download release asset."
echo "Check if the release asset exists at: https://github.com/$OWNER/$REPO/releases/tag/$LATEST_TAG"
exit 1
fi
CHECKSUMS_URL="https://github.com/$OWNER/$REPO/releases/download/$LATEST_TAG/checksums.txt"
echo "Downloading checksums..."
curl -sSfL "$CHECKSUMS_URL" -o "$TMP_DIR/checksums.txt"
EXPECTED_SUM=$(awk -v file="$TARBALL" '$2 == file {print $1}' "$TMP_DIR/checksums.txt")
if [ -z "$EXPECTED_SUM" ]; then
echo "Error: No checksum found for $TARBALL"
exit 1
fi
if command -v sha256sum >/dev/null 2>&1; then
ACTUAL_SUM=$(sha256sum "$TMP_DIR/$TARBALL" | awk '{print $1}')
else
ACTUAL_SUM=$(shasum -a 256 "$TMP_DIR/$TARBALL" | awk '{print $1}')
fi
if [ "$EXPECTED_SUM" != "$ACTUAL_SUM" ]; then
echo "Error: Checksum verification failed for $TARBALL"
exit 1
fi
echo "Extracting..."
tar -xzf "$TMP_DIR/$TARBALL" -C "$TMP_DIR"
# Determine installation path
INSTALL_DIR="/usr/local/bin"
if [ ! -w "$INSTALL_DIR" ]; then
# Fallback to local user bin if /usr/local/bin is not writable without sudo
INSTALL_DIR="$HOME/.local/bin"
mkdir -p "$INSTALL_DIR"
echo "Warning: /usr/local/bin is not writable. Installing to $INSTALL_DIR instead."
echo "Make sure $INSTALL_DIR is in your PATH."
fi
echo "Installing to $INSTALL_DIR..."
mv "$TMP_DIR/$BINARY_NAME" "$INSTALL_DIR/$BINARY_NAME"
chmod +x "$INSTALL_DIR/$BINARY_NAME"
echo "🛸 $BINARY_NAME was successfully installed to $INSTALL_DIR/$BINARY_NAME"