From 1010fb88b45080110620405c3a103e0a509b7295 Mon Sep 17 00:00:00 2001 From: ilguappo Date: Sun, 20 Jul 2025 02:49:58 -0700 Subject: [PATCH] refactor: make command argument checking into guard claus. --- mc | 58 ++++++++++++++++++++++++++++------------------------------ 1 file changed, 28 insertions(+), 30 deletions(-) diff --git a/mc b/mc index 09516da..d233823 100755 --- a/mc +++ b/mc @@ -99,37 +99,35 @@ function main() { if [[ ! $(which curl) ]]; then echo "curl is required"; exit 1; fi if [[ ! $(which jq) ]]; then echo "jq is required"; exit 1; fi - for command in "${!commands[@]}"; do - if [[ "$1" == "${command}" ]]; then + # Check if key does NOT exists in command array + if [[ ! -v commands[$1] ]]; then + printf "${0}: invalid option -- $1\n" + usage + exit 1 + fi - local endpoint method body - read -r endpoint method body <<<"${commands[$1]}" - - case "${method}" in - "POST") - curl -sS "${headers[@]}" -X "${method}" "${base_url}${endpoint}" \ - -d "$(printf "${body}\n" "${args[@]}")" - echo "" - ;; - "GET") - curl -sS "${headers[@]}" -X "${method}" "${base_url}${endpoint}" - echo "" - ;; - "FUNC") - eval "${endpoint}" "${args[@]}" - ;; - *) - echo "ERROR: Invalid state. Check for typo in command array." - ;; - esac - - exit 0 - fi - done - - printf "${0}: invalid option -- $1\n" - usage - exit 1 + local endpoint method body + read -r endpoint method body <<<"${commands[$1]}" + + case "${method}" in + "POST") + curl -sS "${headers[@]}" -X "${method}" "${base_url}${endpoint}" \ + -d "$(printf "${body}\n" "${args[@]}")" + echo "" + ;; + "GET") + curl -sS "${headers[@]}" -X "${method}" "${base_url}${endpoint}" + echo "" + ;; + "FUNC") + eval "${endpoint}" "${args[@]}" + ;; + *) + echo "ERROR: Invalid state. Check for typo in command array." + ;; + esac + + exit 0 }