refactor: make command argument checking into guard claus.

This commit is contained in:
2025-07-20 02:49:58 -07:00
parent 58c41a708b
commit 1010fb88b4

58
mc
View File

@@ -99,37 +99,35 @@ function main() {
if [[ ! $(which curl) ]]; then echo "curl is required"; exit 1; fi if [[ ! $(which curl) ]]; then echo "curl is required"; exit 1; fi
if [[ ! $(which jq) ]]; then echo "jq is required"; exit 1; fi if [[ ! $(which jq) ]]; then echo "jq is required"; exit 1; fi
for command in "${!commands[@]}"; do # Check if key does NOT exists in command array
if [[ "$1" == "${command}" ]]; then if [[ ! -v commands[$1] ]]; then
printf "${0}: invalid option -- $1\n"
usage
exit 1
fi
local endpoint method body local endpoint method body
read -r endpoint method body <<<"${commands[$1]}" read -r endpoint method body <<<"${commands[$1]}"
case "${method}" in case "${method}" in
"POST") "POST")
curl -sS "${headers[@]}" -X "${method}" "${base_url}${endpoint}" \ curl -sS "${headers[@]}" -X "${method}" "${base_url}${endpoint}" \
-d "$(printf "${body}\n" "${args[@]}")" -d "$(printf "${body}\n" "${args[@]}")"
echo "" echo ""
;; ;;
"GET") "GET")
curl -sS "${headers[@]}" -X "${method}" "${base_url}${endpoint}" curl -sS "${headers[@]}" -X "${method}" "${base_url}${endpoint}"
echo "" echo ""
;; ;;
"FUNC") "FUNC")
eval "${endpoint}" "${args[@]}" eval "${endpoint}" "${args[@]}"
;; ;;
*) *)
echo "ERROR: Invalid state. Check for typo in command array." echo "ERROR: Invalid state. Check for typo in command array."
;; ;;
esac esac
exit 0 exit 0
fi
done
printf "${0}: invalid option -- $1\n"
usage
exit 1
} }