refactor: replace nested if with case for clarity
This commit is contained in:
28
mc
28
mc
@@ -1,5 +1,6 @@
|
|||||||
#!/usr/bin/env bash
|
#!/usr/bin/env bash
|
||||||
|
|
||||||
|
args=("$@")
|
||||||
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}" )
|
||||||
|
|
||||||
@@ -100,7 +101,6 @@ function usage() {
|
|||||||
printf "\n"
|
printf "\n"
|
||||||
}
|
}
|
||||||
|
|
||||||
args=("$@")
|
|
||||||
|
|
||||||
function main() {
|
function main() {
|
||||||
# remove command from arg list while preserving quoted strings.
|
# remove command from arg list while preserving quoted strings.
|
||||||
@@ -110,15 +110,23 @@ function main() {
|
|||||||
|
|
||||||
for command in "${!commands[@]}"; do
|
for command in "${!commands[@]}"; do
|
||||||
if [[ "$1" == "${command}" ]]; then
|
if [[ "$1" == "${command}" ]]; then
|
||||||
if [[ "$(second ${commands[$1]})" == "POST" ]]; then
|
case "$(second ${commands[$1]})" in
|
||||||
curl -sS "${headers[@]}" -X "$(second ${commands[$1]})" "${base_url}$(first ${commands[$1]})" -d "$(printf "$(rest $(rest ${commands[$1]}))\n" "${args[@]}")"
|
"POST")
|
||||||
echo ""
|
curl -sS "${headers[@]}" -X "$(second ${commands[$1]})" "${base_url}$(first ${commands[$1]})" \
|
||||||
elif [[ "$(second ${commands[$1]})" == "FUNC" ]]; then
|
-d "$(printf "$(drop 2 ${commands[$1]})\n" "${args[@]}")"
|
||||||
eval "$(first ${commands[$1]})"
|
echo ""
|
||||||
else
|
;;
|
||||||
curl -sS "${headers[@]}" -X "$(second ${commands[$1]})" "${base_url}$(first ${commands[$1]})"
|
"GET")
|
||||||
echo ""
|
curl -sS "${headers[@]}" -X "$(second ${commands[$1]})" "${base_url}$(first ${commands[$1]})"
|
||||||
fi
|
echo ""
|
||||||
|
;;
|
||||||
|
"FUNC")
|
||||||
|
eval "$(first ${commands[$1]})"
|
||||||
|
;;
|
||||||
|
*)
|
||||||
|
echo "ERROR: Invalid state. Check for typo in command array."
|
||||||
|
;;
|
||||||
|
esac
|
||||||
|
|
||||||
exit 0
|
exit 0
|
||||||
fi
|
fi
|
||||||
|
Reference in New Issue
Block a user