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 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
}