From 743d4745d7a85d73a7824d40393aeec5698e16f7 Mon Sep 17 00:00:00 2001 From: ilguappo Date: Fri, 18 Jul 2025 02:46:58 -0700 Subject: [PATCH] update --- mc | 89 ++++++++++++++++++++++++++++++++++-------------------- mc-test.sh | 6 ---- 2 files changed, 56 insertions(+), 39 deletions(-) diff --git a/mc b/mc index 124868a..f651bc9 100755 --- a/mc +++ b/mc @@ -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 diff --git a/mc-test.sh b/mc-test.sh index 7e78b36..5f911ba 100644 --- a/mc-test.sh +++ b/mc-test.sh @@ -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"