fix: extract main api call to function. Script no longer needs to be in $PATH to work. And check-update command now pulls directly from mod-list endpoint.
This commit is contained in:
31
mc
31
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
|
||||
|
Reference in New Issue
Block a user