Skip to content

Commit 304ec06

Browse files
committed
Improve platform detection and handling in install.sh
Refactor `select_archive_format` to use a case statement for better readability and extend functionality for more robust platform detection. Ensure `sudo` is correctly handled only when required in the main installation block. Changes: - Use case statement in `select_archive_format` for clarity - Add a root check before using `sudo` in the installation block - Maintain compatibility for both `tar` and `unzip` commands
1 parent fb43060 commit 304ec06

File tree

1 file changed

+23
-14
lines changed

1 file changed

+23
-14
lines changed

install.sh

Lines changed: 23 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -56,18 +56,21 @@ detect_platform() {
5656
select_archive_format() {
5757
PLATFORM_ARCH=$1
5858

59-
if [ "$PLATFORM_ARCH" = windows-* ]; then
60-
echo "zip"
61-
else
62-
if command -v tar >/dev/null 2>&1; then
63-
echo "tar.gz"
64-
elif command -v unzip >/dev/null 2>&1; then
59+
case "$PLATFORM_ARCH" in
60+
windows-*)
6561
echo "zip"
66-
else
67-
echo "Unsupported: neither tar nor unzip are available."
68-
exit 1
69-
fi
70-
fi
62+
;;
63+
*)
64+
if command -v tar >/dev/null 2>&1; then
65+
echo "tar.gz"
66+
elif command -v unzip >/dev/null 2>&1; then
67+
echo "zip"
68+
else
69+
echo "Unsupported: neither tar nor unzip are available."
70+
exit 1
71+
fi
72+
;;
73+
esac
7174
}
7275

7376
main() {
@@ -119,10 +122,16 @@ main() {
119122
mkdir -p "$INSTALL_DIR"
120123
mv "$BINARY_PATH" "$INSTALL_DIR/$BINARY_NAME.exe"
121124
else
122-
sudo su <<EOF
123-
[ $(uname -s) = "Linux" ] && setcap cap_net_admin=eip $BINARY_PATH
124-
mv "$BINARY_PATH" "$INSTALL_DIR/$BINARY_NAME"
125+
# Run using sudo if not root
126+
if [ "$(id -u)" -ne 0 ]; then
127+
sudo sh <<EOF
128+
[ "$(uname -s)" = "Linux" ] && setcap cap_net_admin=eip "$BINARY_PATH"
129+
mv "$BINARY_PATH" "$INSTALL_DIR/$BINARY_NAME"
125130
EOF
131+
else
132+
[ "$(uname -s)" = "Linux" ] && setcap cap_net_admin=eip "$BINARY_PATH"
133+
mv "$BINARY_PATH" "$INSTALL_DIR/$BINARY_NAME"
134+
fi
126135
fi
127136

128137
# Clean up

0 commit comments

Comments
 (0)