refactor: eliminate parsing functions in preference of read -r command

This commit is contained in:
2025-07-18 11:49:17 -07:00
parent 65610d15c8
commit 832391a281

40
mc
View File

@@ -78,32 +78,6 @@ function backup() {
popd popd
} }
function first() {
echo "$1"
}
function second() {
first $(rest "${@}")
}
function rest() {
drop 1 "$@"
}
function drop() {
if (( 1 == "$#" )); then
return 0
fi
if (( "$#" < 1 )); then
return 1
fi
shift $(( 1 + $1 ))
echo "$@"
}
function usage() { function usage() {
printf "API key for my-mc must be exported using 'export MY_MC_API_KEY=<my-mc api key>'\n\n" printf "API key for my-mc must be exported using 'export MY_MC_API_KEY=<my-mc api key>'\n\n"
printf "Backup directory can be changed from default ~/mc-mc_backup/ by using 'export MY_MC_BACKUP_DIR=</path/to/dir/>'\n\n" printf "Backup directory can be changed from default ~/mc-mc_backup/ by using 'export MY_MC_BACKUP_DIR=</path/to/dir/>'\n\n"
@@ -124,18 +98,22 @@ function main() {
for command in "${!commands[@]}"; do for command in "${!commands[@]}"; do
if [[ "$1" == "${command}" ]]; then if [[ "$1" == "${command}" ]]; then
case "$(second ${commands[$1]})" in
local endpoint method body
read -r endpoint method body <<<"${commands[$1]}"
case "${method}" in
"POST") "POST")
curl -sS "${headers[@]}" -X "$(second ${commands[$1]})" "${base_url}$(first ${commands[$1]})" \ curl -sS "${headers[@]}" -X "${method}" "${base_url}${endpoint}" \
-d "$(printf "$(drop 2 ${commands[$1]})\n" "${args[@]}")" -d "$(printf "${body}\n" "${args[@]}")"
echo "" echo ""
;; ;;
"GET") "GET")
curl -sS "${headers[@]}" -X "$(second ${commands[$1]})" "${base_url}$(first ${commands[$1]})" curl -sS "${headers[@]}" -X "${method}" "${base_url}${endpoint}"
echo "" echo ""
;; ;;
"FUNC") "FUNC")
eval "$(first ${commands[$1]})" "${args[@]}" eval "${endpoint}" "${args[@]}"
;; ;;
*) *)
echo "ERROR: Invalid state. Check for typo in command array." echo "ERROR: Invalid state. Check for typo in command array."