refactor: replace nested if with case for clarity

This commit is contained in:
2025-07-18 08:00:45 -07:00
parent f578460900
commit f290de739b

28
mc
View File

@@ -1,5 +1,6 @@
#!/usr/bin/env bash
args=("$@")
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}" )
@@ -100,7 +101,6 @@ function usage() {
printf "\n"
}
args=("$@")
function main() {
# remove command from arg list while preserving quoted strings.
@@ -110,15 +110,23 @@ function main() {
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
case "$(second ${commands[$1]})" in
"POST")
curl -sS "${headers[@]}" -X "$(second ${commands[$1]})" "${base_url}$(first ${commands[$1]})" \
-d "$(printf "$(drop 2 ${commands[$1]})\n" "${args[@]}")"
echo ""
;;
"GET")
curl -sS "${headers[@]}" -X "$(second ${commands[$1]})" "${base_url}$(first ${commands[$1]})"
echo ""
;;
"FUNC")
eval "$(first ${commands[$1]})"
;;
*)
echo "ERROR: Invalid state. Check for typo in command array."
;;
esac
exit 0
fi