update agent install script arg parsing

This commit is contained in:
Henry Dollman
2024-11-19 14:31:50 -05:00
parent d51ffa17ed
commit 0529837ac8

View File

@@ -7,41 +7,11 @@ UNINSTALL=false
CHINA_MAINLAND=false
GITHUB_URL="https://github.com"
GITHUB_API_URL="https://api.github.com"
KEY=""
# Read command line options
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
# Check for help flag first
case "$1" in
'-k')
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')
-h | --help)
printf "Beszel Agent installation script\n\n"
printf "Usage: ./install-agent.sh [options]\n\n"
printf "Options: \n"
@@ -52,21 +22,26 @@ while true; do
printf " -h, --help : Display this help message\n"
exit 0
;;
'--')
shift
break
;;
*)
echo "Invalid option: $1" >&2
exit 1
;;
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 command -v sudo >/dev/null 2>&1; then
exec sudo "$0" "$@"
SUDO_ARGS=$(build_sudo_args "$@")
eval "exec sudo $0 $SUDO_ARGS"
else
echo "This script must be run as root. Please either:"
echo "1. Run this script as root (su root)"
@@ -75,6 +50,31 @@ if [ "$(id -u)" != "0" ]; then
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
if [ "$UNINSTALL" = true ]; then
echo "Stopping and disabling the agent service..."