mcserv : ré-écriture

This commit is contained in:
Hippolyte Chauvin 2023-12-01 13:29:06 +01:00
parent f17dd5aad2
commit 56b23f6c98

View file

@ -1,21 +1,24 @@
#!/bin/bash #!/bin/bash
exec_user() { exec_as() {
local user="$1" local user="$1"
shift shift
if [ "$USER" != "$user" ]; then if [ "$(whoami)" != "$user" ]; then
exec sudo -u "$user" "$0" "$@" exec sudo -u "$user" "$0" "$@"
else else
return 0 cd
fi fi
} }
exec_user minecraft "$@" exec_as minecraft "$@" || exit
bin=bin
cd ~/bin || exit PATH="${bin}:${PATH}"
EDITOR="${EDITOR:-nvim}"
alias ls='ls --color=auto'
shopt -s expand_aliases
print_error() { print_error() {
>&2 printf '\033[31;1m%s\033[0m \033[1m%s\033[0m\n' ERROR: "$*" >&2 printf '\033[31;1mERROR:\033[0m \033[1m%s\033[0m\n' "$*"
} }
public_usage() { public_usage() {
@ -23,8 +26,8 @@ public_usage() {
name="$(basename "$0")" name="$(basename "$0")"
cat << EOF cat << EOF
${name}: utilisation ${name}: utilisation
${name} start|status|stop|watch|restart NOM_SERVEUR ${name} start|status|stop|watch|restart|edit <nom serveur>
${name} list|active|usage ${name} list|active|usage|shell
EOF EOF
} }
@ -36,8 +39,8 @@ error_usage() {
public_start() { public_start() {
if [ -n "$1" ]; then if [ -n "$1" ]; then
screen -d -m -S "$1" \ exec screen -S "$1" \
"./${1}" "$1"
else else
error_usage error_usage
fi fi
@ -58,23 +61,23 @@ public_status() {
public_stop() { public_stop() {
if [ -n "$1" ]; then if [ -n "$1" ]; then
screen -S "$1" -X stuff '^E^Ustop^M' exec screen -S "$1" -X stuff '^E^Ustop^M'
else else
error_usage error_usage
fi fi
} }
public_watch() { public_watch() {
screen -dr "$1" exec screen -dr "$1"
} }
public_active() { public_active() {
screen -ls exec screen -ls
} }
public_list() { public_list() {
echo "Serveurs disponibles :" echo "Serveurs disponibles :"
ls --color=auto exec ls "$bin"
} }
public_restart() { public_restart() {
@ -84,17 +87,21 @@ public_restart() {
set +e set +e
} }
public_edit() {
exec "$EDITOR" "${bin}/${1}"
}
public_shell() {
exec bash -i
}
verb="$1" verb="$1"
shift shift
case "$verb" in
start|status|stop|watch|active|list|usage|restart)
"public_${verb}" "$@"
;;
"")
public_active
;;
*)
error_usage
;;
esac
if [ -z "$verb" ]; then
public_active
elif declare -F | cut -d \ -f 3 | grep '^public_' | sed 's/^public_//' | grep -q "^${verb}$"; then
"public_${verb}" "$@"
else
error_usage
fi