From 832391a281cdb51a71f22babd530d9c5708aa73e Mon Sep 17 00:00:00 2001 From: ilguappo Date: Fri, 18 Jul 2025 11:49:17 -0700 Subject: [PATCH] refactor: eliminate parsing functions in preference of read -r command --- mc | 40 +++++++++------------------------------- 1 file changed, 9 insertions(+), 31 deletions(-) diff --git a/mc b/mc index ff17d83..b55eb3d 100755 --- a/mc +++ b/mc @@ -78,32 +78,6 @@ function backup() { 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() { printf "API key for my-mc must be exported using 'export MY_MC_API_KEY='\n\n" printf "Backup directory can be changed from default ~/mc-mc_backup/ by using 'export MY_MC_BACKUP_DIR='\n\n" @@ -124,18 +98,22 @@ function main() { for command in "${!commands[@]}"; do if [[ "$1" == "${command}" ]]; then - case "$(second ${commands[$1]})" in + + local endpoint method body + read -r endpoint method body <<<"${commands[$1]}" + + case "${method}" in "POST") - curl -sS "${headers[@]}" -X "$(second ${commands[$1]})" "${base_url}$(first ${commands[$1]})" \ - -d "$(printf "$(drop 2 ${commands[$1]})\n" "${args[@]}")" + curl -sS "${headers[@]}" -X "${method}" "${base_url}${endpoint}" \ + -d "$(printf "${body}\n" "${args[@]}")" echo "" ;; "GET") - curl -sS "${headers[@]}" -X "$(second ${commands[$1]})" "${base_url}$(first ${commands[$1]})" + curl -sS "${headers[@]}" -X "${method}" "${base_url}${endpoint}" echo "" ;; "FUNC") - eval "$(first ${commands[$1]})" "${args[@]}" + eval "${endpoint}" "${args[@]}" ;; *) echo "ERROR: Invalid state. Check for typo in command array."