From 61448d18bc86e9091aae319ec9233f66ff7f831e Mon Sep 17 00:00:00 2001 From: ilguappo Date: Tue, 22 Jul 2025 19:18:59 -0700 Subject: [PATCH] fix: extract main api call to function. Script no longer needs to be in $PATH to work. --- mc | 31 +++++++++++++++++-------------- 1 file changed, 17 insertions(+), 14 deletions(-) diff --git a/mc b/mc index 5fa7d58..ff8b8b9 100755 --- a/mc +++ b/mc @@ -1,6 +1,6 @@ #!/usr/bin/env bash -args=("$@") +ARGS=("$@") CYAN="\033[1;96m" RED="\033[0;91m" GREEN="\033[0;92m" @@ -49,7 +49,7 @@ declare -rA commands=( ) function version() { - local message="$(mc console version | jq -r '.message')" + local message="$(call_api console version | jq -r '.message')" if [[ $message =~ ([0-9]+\.[0-9]+\.[0-9]+) ]]; then local version="${BASH_REMATCH[0]}" @@ -60,8 +60,7 @@ function version() { function check_update() { local version="${1:-$(version)}" - local url_base="$(mc website | jq -r '.message')" - local mods="$(curl -sS ${url_base}/mods.json | jq -c '.'))" + local mods="$(call_api mod-list | jq -c '.mods')" local -a result=("MOD\tID\tAVAILABLE\n") local item id name latest available has_version available_versions @@ -98,7 +97,7 @@ function connect() { function backup() { if [[ ! $(which lftp) ]]; then echo "lftp is required"; exit 1; fi - local sftp_credentials="$(mc my-sftp)" + local sftp_credentials="$(call_api my-sftp)" if [[ "$(jq -r '.success' <<<"${sftp_credentials}" )" != "true" ]]; then echo "ERROR: failed to get sftp login info"; exit 1; @@ -136,14 +135,10 @@ function usage() { echo -e "${message[*]}" | column -t -s $'\t' } - -function main() { +function call_api() { # remove command from arg list while preserving quoted strings. - args=("${args[@]:1}") - - if [[ ! $(which curl) ]]; then echo "curl is required"; exit 1; fi - if [[ ! $(which jq) ]]; then echo "jq is required"; exit 1; fi - + local args=("${ARGS[@]:1}") + # Check if key does NOT exists in command array if [[ ! -v commands[$1] ]]; then printf "${0}: invalid option -- $1\n\n" @@ -171,11 +166,19 @@ function main() { echo "ERROR: Invalid state. Check for typo in command array." ;; esac - +} + + +function main() { + if [[ ! $(which curl) ]]; then echo "curl is required"; exit 1; fi + if [[ ! $(which jq) ]]; then echo "jq is required"; exit 1; fi + + call_api "${ARGS[@]}" + exit 0 } if [[ "$(basename "$0")" == "mc" ]]; then - main "${args[@]}" + main fi