This commit is contained in:
2025-07-18 02:46:58 -07:00
parent 30394f9563
commit 743d4745d7
2 changed files with 56 additions and 39 deletions

89
mc
View File

@@ -26,17 +26,17 @@ declare -rA commands=(
[status-minecraft]='status/Minecraft GET'
[status-bedrock]='status/Bedrock GET'
[status-sftp]='status/SFTP GET'
[ban]='ban POST {"username": "%s"}'
[unban]='unban POST {"username": "%s"}'
[say]='say POST {"message": "%s"}'
[tell]='tell POST {"username": "%s", "message": "%s"}'
[console]='console POST {"command": "%s"}'
[give]='give POST {"username": "%s", "item": "%s", "amount": "%s"}'
[install]='install POST {"mod": "%s"}'
[uninstall]='uninstall POST {"mod": "%s"}'
[search]='search POST {"mod": "%s"}'
[ban]='ban POST {"username":"%s"}'
[unban]='unban POST {"username":"%s"}'
[say]='say POST {"message":"%s"}'
[tell]='tell POST {"username":"%s","message":"%s"}'
[console]='console POST {"command":"%s"}'
[give]='give POST {"username":"%s","item":"%s","amount":"%s"}'
[install]='install POST {"mod":"%s"}'
[uninstall]='uninstall POST {"mod":"%s"}'
[search]='search POST {"mod":"%s"}'
[mod-list]='mod-list GET'
[backup]='backup FUNC'
[backup]='backup FUNC'
)
function backup() {
@@ -65,13 +65,28 @@ function backup() {
function first() {
echo "$@" | cut --delimiter=" " --fields="-1"
echo "$1"
}
function second() {
first $(rest "${@}")
}
function rest() {
if (( "${#@}" > 1 )); then
echo "$@" | cut --delimiter=" " --fields="2-"
drop 1 "$@"
}
function drop() {
if (( 1 == "$#" )); then
return 0
fi
if (( "$#" < 1 )); then
return 1
fi
shift $(( 1 + $1 ))
echo "$@"
}
function usage() {
@@ -86,27 +101,35 @@ function usage() {
}
args=("$@")
# remove command from arg list while preserving quoted strings.
args=("${args[@]:1}")
if [[ ! $(which jq) ]]; then echo "jq is required"; exit 1; fi
for command in "${!commands[@]}"; do
if [[ "$1" == "${command}" ]]; then
if [[ "$(first $(rest ${commands[$1]}))" == "POST" ]]; then
curl -sS "${headers[@]}" -X "$(first $(rest ${commands[$1]}))" "${base_url}$(first "${commands[$1]}")" -d "$(printf "$(rest $(rest ${commands[$1]}))\n" "${args[@]}")"
echo ""
elif [[ "$(first $(rest ${commands[$1]}))" == "FUNC" ]]; then
eval "$(first ${commands[$1]})"
else
curl -sS "${headers[@]}" -X "$(first $(rest ${commands[$1]}))" "${base_url}$(first "${commands[$1]}")"
echo ""
function main() {
# remove command from arg list while preserving quoted strings.
args=("${args[@]:1}")
if [[ ! $(which jq) ]]; then echo "jq is required"; exit 1; fi
for command in "${!commands[@]}"; do
if [[ "$1" == "${command}" ]]; then
if [[ "$(second ${commands[$1]})" == "POST" ]]; then
curl -sS "${headers[@]}" -X "$(second ${commands[$1]})" "${base_url}$(first "${commands[$1]}")" -d "$(printf "$(rest $(rest ${commands[$1]}))\n" "${args[@]}")"
echo ""
elif [[ "$(second ${commands[$1]})" == "FUNC" ]]; then
eval "$(first ${commands[$1]})"
else
curl -sS "${headers[@]}" -X "$(second ${commands[$1]})" "${base_url}$(first "${commands[$1]}")"
echo ""
fi
exit 0
fi
done
printf "${0}: invalid option -- $1\n"
usage
exit 1
}
exit 0
fi
done
printf "${0}: invalid option -- $1\n"
usage
exit 1
if [[ "$(basename "$0")" == "mc" ]]; then
main "${args[@]}"
fi

View File

@@ -13,12 +13,6 @@ function assert() {
return 1
}
function test_assert() {
local string="one two three four"
assert "$(rest ${string})" "$(rest ${string})"
}
test_assert
function first_func_returns_first_item() {
local string="one two three four"
assert "$(first ${string})" "one"