-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathinstall.sh
More file actions
136 lines (118 loc) · 3.98 KB
/
Copy pathinstall.sh
File metadata and controls
136 lines (118 loc) · 3.98 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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
#!/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"
;;
mingw*|msys*|cygwin*|windows)
OS="Windows"
;;
*)
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)
if [ "$OS" = "Windows" ]; then
TARBALL="${BINARY_NAME}_${LATEST_TAG#v}_${OS}_${ARCH}.zip"
else
TARBALL="${BINARY_NAME}_${LATEST_TAG#v}_${OS}_${ARCH}.tar.gz"
fi
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}')
elif command -v shasum >/dev/null 2>&1; then
ACTUAL_SUM=$(shasum -a 256 "$TMP_DIR/$TARBALL" | awk '{print $1}')
elif command -v powershell.exe >/dev/null 2>&1; then
ACTUAL_SUM=$(powershell.exe -Command "(Get-FileHash -Path '$TMP_DIR/$TARBALL' -Algorithm SHA256).Hash" | tr -d '\r' | tr '[:upper:]' '[:lower:]')
else
echo "Error: No sha256sum or shasum tool found to verify checksum."
exit 1
fi
if [ "$EXPECTED_SUM" != "$ACTUAL_SUM" ]; then
echo "Error: Checksum verification failed for $TARBALL"
exit 1
fi
echo "Extracting..."
if [ "$OS" = "Windows" ]; then
if command -v unzip >/dev/null 2>&1; then
unzip -q "$TMP_DIR/$TARBALL" -d "$TMP_DIR"
elif command -v tar >/dev/null 2>&1; then
tar -xf "$TMP_DIR/$TARBALL" -C "$TMP_DIR"
else
powershell.exe -Command "Expand-Archive -Path '$TMP_DIR/$TARBALL' -DestinationPath '$TMP_DIR' -Force"
fi
else
tar -xzf "$TMP_DIR/$TARBALL" -C "$TMP_DIR"
fi
# Determine installation path & binary extension
if [ "$OS" = "Windows" ]; then
BINARY_EXE="${BINARY_NAME}.exe"
# Git Bash automatically adds ~/bin to PATH, so $HOME/bin is a standard directory
INSTALL_DIR="$HOME/bin"
mkdir -p "$INSTALL_DIR"
else
BINARY_EXE="${BINARY_NAME}"
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
fi
echo "Installing to $INSTALL_DIR..."
mv "$TMP_DIR/$BINARY_EXE" "$INSTALL_DIR/$BINARY_EXE"
chmod +x "$INSTALL_DIR/$BINARY_EXE"
echo "🛸 $BINARY_NAME was successfully installed to $INSTALL_DIR/$BINARY_EXE"