mirror of
https://github.com/fankes/beszel.git
synced 2025-10-20 10:19:27 +08:00
update agent install script arg parsing
This commit is contained in:
@@ -7,41 +7,11 @@ UNINSTALL=false
|
|||||||
CHINA_MAINLAND=false
|
CHINA_MAINLAND=false
|
||||||
GITHUB_URL="https://github.com"
|
GITHUB_URL="https://github.com"
|
||||||
GITHUB_API_URL="https://api.github.com"
|
GITHUB_API_URL="https://api.github.com"
|
||||||
|
KEY=""
|
||||||
|
|
||||||
# Read command line options
|
# Check for help flag first
|
||||||
TEMP=$(getopt -o 'k:p:uh' --long 'china-mirrors,help' -n 'install-agent.sh' -- "$@")
|
|
||||||
|
|
||||||
if [ $? -ne 0 ]; then
|
|
||||||
echo 'Failed to parse command line arguments' >&2
|
|
||||||
exit 1
|
|
||||||
fi
|
|
||||||
|
|
||||||
eval set -- "$TEMP"
|
|
||||||
unset TEMP
|
|
||||||
|
|
||||||
while true; do
|
|
||||||
case "$1" in
|
case "$1" in
|
||||||
'-k')
|
-h | --help)
|
||||||
KEY="$2"
|
|
||||||
shift 2
|
|
||||||
continue
|
|
||||||
;;
|
|
||||||
'-p')
|
|
||||||
PORT="$2"
|
|
||||||
shift 2
|
|
||||||
continue
|
|
||||||
;;
|
|
||||||
'-u')
|
|
||||||
UNINSTALL=true
|
|
||||||
shift
|
|
||||||
continue
|
|
||||||
;;
|
|
||||||
'--china-mirrors')
|
|
||||||
CHINA_MAINLAND=true
|
|
||||||
shift
|
|
||||||
continue
|
|
||||||
;;
|
|
||||||
'-h'|'--help')
|
|
||||||
printf "Beszel Agent installation script\n\n"
|
printf "Beszel Agent installation script\n\n"
|
||||||
printf "Usage: ./install-agent.sh [options]\n\n"
|
printf "Usage: ./install-agent.sh [options]\n\n"
|
||||||
printf "Options: \n"
|
printf "Options: \n"
|
||||||
@@ -52,21 +22,26 @@ while true; do
|
|||||||
printf " -h, --help : Display this help message\n"
|
printf " -h, --help : Display this help message\n"
|
||||||
exit 0
|
exit 0
|
||||||
;;
|
;;
|
||||||
'--')
|
|
||||||
shift
|
|
||||||
break
|
|
||||||
;;
|
|
||||||
*)
|
|
||||||
echo "Invalid option: $1" >&2
|
|
||||||
exit 1
|
|
||||||
;;
|
|
||||||
esac
|
esac
|
||||||
done
|
|
||||||
|
|
||||||
# Check if running as root
|
# Build sudo args by properly quoting everything
|
||||||
|
build_sudo_args() {
|
||||||
|
QUOTED_ARGS=""
|
||||||
|
while [ $# -gt 0 ]; do
|
||||||
|
if [ -n "$QUOTED_ARGS" ]; then
|
||||||
|
QUOTED_ARGS="$QUOTED_ARGS "
|
||||||
|
fi
|
||||||
|
QUOTED_ARGS="$QUOTED_ARGS'$(echo "$1" | sed "s/'/'\\\\''/g")'"
|
||||||
|
shift
|
||||||
|
done
|
||||||
|
echo "$QUOTED_ARGS"
|
||||||
|
}
|
||||||
|
|
||||||
|
# Check if running as root and re-execute with sudo if needed
|
||||||
if [ "$(id -u)" != "0" ]; then
|
if [ "$(id -u)" != "0" ]; then
|
||||||
if command -v sudo >/dev/null 2>&1; then
|
if command -v sudo >/dev/null 2>&1; then
|
||||||
exec sudo "$0" "$@"
|
SUDO_ARGS=$(build_sudo_args "$@")
|
||||||
|
eval "exec sudo $0 $SUDO_ARGS"
|
||||||
else
|
else
|
||||||
echo "This script must be run as root. Please either:"
|
echo "This script must be run as root. Please either:"
|
||||||
echo "1. Run this script as root (su root)"
|
echo "1. Run this script as root (su root)"
|
||||||
@@ -75,6 +50,31 @@ if [ "$(id -u)" != "0" ]; then
|
|||||||
fi
|
fi
|
||||||
fi
|
fi
|
||||||
|
|
||||||
|
# Parse arguments
|
||||||
|
while [ $# -gt 0 ]; do
|
||||||
|
case "$1" in
|
||||||
|
-k)
|
||||||
|
shift
|
||||||
|
KEY="$1"
|
||||||
|
;;
|
||||||
|
-p)
|
||||||
|
shift
|
||||||
|
PORT="$1"
|
||||||
|
;;
|
||||||
|
-u)
|
||||||
|
UNINSTALL=true
|
||||||
|
;;
|
||||||
|
--china-mirrors)
|
||||||
|
CHINA_MAINLAND=true
|
||||||
|
;;
|
||||||
|
*)
|
||||||
|
echo "Invalid option: $1" >&2
|
||||||
|
exit 1
|
||||||
|
;;
|
||||||
|
esac
|
||||||
|
shift
|
||||||
|
done
|
||||||
|
|
||||||
# Uninstall process
|
# Uninstall process
|
||||||
if [ "$UNINSTALL" = true ]; then
|
if [ "$UNINSTALL" = true ]; then
|
||||||
echo "Stopping and disabling the agent service..."
|
echo "Stopping and disabling the agent service..."
|
||||||
|
Reference in New Issue
Block a user