make commands regular

This commit is contained in:
2025-07-03 00:49:52 -07:00
parent 7b6b6d5708
commit d576e3d878

76
mc
View File

@ -3,36 +3,39 @@
readonly base_url="https://api.my-mc.link/" readonly base_url="https://api.my-mc.link/"
readonly headers=( -H "Accept: application/json" -H "Content-Type: application/json" -H "x-my-mc-auth: ${MY_MC_API_KEY}" ) readonly headers=( -H "Accept: application/json" -H "Content-Type: application/json" -H "x-my-mc-auth: ${MY_MC_API_KEY}" )
# command format: '<endpoint> <method> [options]'
declare -rA commands=( declare -rA commands=(
[hello]='GET' [hello]='hello GET'
[time]='GET' [time]='time GET'
[stats]='GET' [stats]='stats GET'
[log]='GET' [log]='log GET'
[start]='GET' [start]='start GET'
[stop]='GET' [stop]='stop GET'
[restart]='GET' [restart]='restart GET'
[my-link]='GET' [my-link]='my-link GET'
[my-geyser-link]='GET' [my-geyser-link]='my-geyser-link GET'
[my-sftp]='GET' [my-sftp]='my-sftp GET'
[my-hash]='GET' [my-hash]='my-hash GET'
[my-geyser-hash]='GET' [my-geyser-hash]='my-geyser-hash GET'
[my-hash-sftp]='GET' [my-hash-sftp]='my-hash-sftp GET'
[list-players]='GET' [list-players]='list-players GET'
[website]='GET' [website]='website GET'
[map]='GET' [map]='map GET'
[status-minecraft]='GET' [status-minecraft]='status/Minecraft GET'
[status-bedrock]='GET' [status-bedrock]='status/Bedrock GET'
[status-sftp]='GET' [status-sftp]='status/SFTP GET'
[ban]='POST {"username": "%s"}' [ban]='ban POST {"username": "%s"}'
[unban]='POST {"username": "%s"}' [unban]='unban POST {"username": "%s"}'
[say]='POST {"message": "%s"}' [say]='say POST {"message": "%s"}'
[tell]='POST {"username": "%s", "message": "%s"}' [tell]='tell POST {"username": "%s", "message": "%s"}'
[console]='POST {"command": "%s"}' [console]='console POST {"command": "%s"}'
[give]='POST {"username": "%s", "item": "%s", "amount": "%s"}' [give]='give POST {"username": "%s", "item": "%s", "amount": "%s"}'
[install]='POST {"mod": "%s"}' [install]='install POST {"mod": "%s"}'
[uninstall]='POST {"mod": "%s"}' [uninstall]='uninstall POST {"mod": "%s"}'
[search]='POST {"mod": "%s"}' [search]='search POST {"mod": "%s"}'
[mod-list]='GET' [mod-list]='mod-list GET'
) )
function first() { function first() {
@ -60,27 +63,26 @@ function usage() {
printf "Positional arguments will fill the JSON objects with values from left to right.\n\n" printf "Positional arguments will fill the JSON objects with values from left to right.\n\n"
printf "COMMANDS:\n" printf "COMMANDS:\n"
for command in "${!commands[@]}"; do for command in "${!commands[@]}"; do
printf "\t ${command} $(rest ${commands[${command}]})\n" printf "\t ${command} $(rest $(rest ${commands[${command}]}))\n"
done done
printf "\n" printf "\n"
} }
# remove command from arg list while preserving quoted strings.
args=("$@") args=("$@")
# remove command from arg list while preserving quoted strings.
args=("${args[@]:1}") args=("${args[@]:1}")
for command in "${!commands[@]}"; do for command in "${!commands[@]}"; do
if [[ "$1" == "${command}" ]]; then if [[ "$1" == "${command}" ]]; then
if [[ "$(first "${commands[$1]}")" == "POST" ]]; then if [[ "$(first $(rest ${commands[$1]}))" == "POST" ]]; then
curl -sS "${headers[@]}" -X "$(first "${commands[$1]}")" "${base_url}${command}" -d "$(printf "$(rest ${commands[$1]})\n" "${args[@]}")" curl -sS "${headers[@]}" -X "$(first $(rest ${commands[$1]}))" "${base_url}$(first "${commands[$1]}")" -d "$(printf "$(rest $(rest ${commands[$1]}))\n" "${args[@]}")"
echo ""
elif [[ "${command}" =~ ^"status" ]]; then
curl -sS "${headers[@]}" -X "$(first "${commands[$1]}")" "${base_url}status/$(status_format ${command})"
echo "" echo ""
else else
curl -sS "${headers[@]}" -X "$(first "${commands[$1]}")" "${base_url}${command}" curl -sS "${headers[@]}" -X "$(first $(rest ${commands[$1]}))" "${base_url}$(first "${commands[$1]}")"
echo "" echo ""
fi fi
exit 0 exit 0
fi fi
done done