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
exec_user() {
exec_as() {
local user="$1"
shift
if [ "$USER" != "$user" ]; then
if [ "$(whoami)" != "$user" ]; then
exec sudo -u "$user" "$0" "$@"
else
return 0
cd
fi
}
exec_user minecraft "$@"
cd ~/bin || exit
exec_as minecraft "$@" || exit
bin=bin
PATH="${bin}:${PATH}"
EDITOR="${EDITOR:-nvim}"
alias ls='ls --color=auto'
shopt -s expand_aliases
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() {
@ -23,8 +26,8 @@ public_usage() {
name="$(basename "$0")"
cat << EOF
${name}: utilisation
${name} start|status|stop|watch|restart NOM_SERVEUR
${name} list|active|usage
${name} start|status|stop|watch|restart|edit <nom serveur>
${name} list|active|usage|shell
EOF
}
@ -36,8 +39,8 @@ error_usage() {
public_start() {
if [ -n "$1" ]; then
screen -d -m -S "$1" \
"./${1}"
exec screen -S "$1" \
"$1"
else
error_usage
fi
@ -58,23 +61,23 @@ public_status() {
public_stop() {
if [ -n "$1" ]; then
screen -S "$1" -X stuff '^E^Ustop^M'
exec screen -S "$1" -X stuff '^E^Ustop^M'
else
error_usage
fi
}
public_watch() {
screen -dr "$1"
exec screen -dr "$1"
}
public_active() {
screen -ls
exec screen -ls
}
public_list() {
echo "Serveurs disponibles :"
ls --color=auto
exec ls "$bin"
}
public_restart() {
@ -84,17 +87,21 @@ public_restart() {
set +e
}
public_edit() {
exec "$EDITOR" "${bin}/${1}"
}
public_shell() {
exec bash -i
}
verb="$1"
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