113 lines
3.6 KiB
Bash
Executable File
113 lines
3.6 KiB
Bash
Executable File
#!/usr/bin/env bash
|
|
|
|
readonly base_url="https://api.my-mc.link/"
|
|
readonly headers=( -H "Accept: application/json" -H "Content-Type: application/json" -H "x-my-mc-auth: ${MY_MC_API_KEY}" )
|
|
|
|
|
|
# command format: '<endpoint> <method> [options]'
|
|
|
|
declare -rA commands=(
|
|
[hello]='hello GET'
|
|
[time]='time GET'
|
|
[stats]='stats GET'
|
|
[log]='log GET'
|
|
[start]='start GET'
|
|
[stop]='stop GET'
|
|
[restart]='restart GET'
|
|
[my-link]='my-link GET'
|
|
[my-geyser-link]='my-geyser-link GET'
|
|
[my-sftp]='my-sftp GET'
|
|
[my-hash]='my-hash GET'
|
|
[my-geyser-hash]='my-geyser-hash GET'
|
|
[my-hash-sftp]='my-hash-sftp GET'
|
|
[list-players]='list-players GET'
|
|
[website]='website GET'
|
|
[map]='map GET'
|
|
[status-minecraft]='status/Minecraft GET'
|
|
[status-bedrock]='status/Bedrock GET'
|
|
[status-sftp]='status/SFTP GET'
|
|
[ban]='ban POST {"username": "%s"}'
|
|
[unban]='unban POST {"username": "%s"}'
|
|
[say]='say POST {"message": "%s"}'
|
|
[tell]='tell POST {"username": "%s", "message": "%s"}'
|
|
[console]='console POST {"command": "%s"}'
|
|
[give]='give POST {"username": "%s", "item": "%s", "amount": "%s"}'
|
|
[install]='install POST {"mod": "%s"}'
|
|
[uninstall]='uninstall POST {"mod": "%s"}'
|
|
[search]='search POST {"mod": "%s"}'
|
|
[mod-list]='mod-list GET'
|
|
[backup]='backup FUNC'
|
|
)
|
|
|
|
function backup() {
|
|
if [[ ! $(which lftp) ]]; then echo "lftp is required"; exit 1; fi
|
|
|
|
local sftp_credentials="$(mc my-sftp)"
|
|
if [[ "$(jq -r '.success' <<<"${sftp_credentials}" )" != "true" ]]; then
|
|
echo "ERROR: failed to get sftp login info";
|
|
exit 1;
|
|
fi
|
|
|
|
local host="$(jq -r '.hostname' <<<"${sftp_credentials}")"
|
|
local username="$(jq -r '.user' <<<"${sftp_credentials}")"
|
|
local port="$(jq -r '.port' <<<"${sftp_credentials}")"
|
|
local password="$(jq -r '.password' <<<"${sftp_credentials}")"
|
|
|
|
local localdir="${MY_MC_BACKUP_DIR:-${HOME}/my-mc_backup/}"
|
|
if [[ ! -d ${localdir} ]]; then mkdir -p ${localdir}; fi
|
|
|
|
pushd "${localdir}"
|
|
|
|
lftp -u "${username}","${password}" "${host}":"${port}" -e "set sftp:connect-program 'ssh -o StrictHostKeyChecking=no'; mirror -c --use-pget minecraft/ minecraft/; quit"
|
|
|
|
popd
|
|
}
|
|
|
|
|
|
function first() {
|
|
echo "$@" | cut --delimiter=" " --fields="-1"
|
|
}
|
|
|
|
function rest() {
|
|
if (( "${#@}" > 1 )); then
|
|
echo "$@" | cut --delimiter=" " --fields="2-"
|
|
fi
|
|
}
|
|
|
|
function usage() {
|
|
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 "Positional arguments will fill the JSON objects with values from left to right.\n\n"
|
|
printf "COMMANDS:\n"
|
|
for command in "${!commands[@]}"; do
|
|
printf "\t ${command} $(rest $(rest ${commands[${command}]}))\n"
|
|
done
|
|
printf "\n"
|
|
}
|
|
|
|
args=("$@")
|
|
# remove command from arg list while preserving quoted strings.
|
|
args=("${args[@]:1}")
|
|
|
|
if [[ ! $(which jq) ]]; then echo "jq is required"; exit 1; fi
|
|
|
|
for command in "${!commands[@]}"; do
|
|
if [[ "$1" == "${command}" ]]; then
|
|
if [[ "$(first $(rest ${commands[$1]}))" == "POST" ]]; then
|
|
curl -sS "${headers[@]}" -X "$(first $(rest ${commands[$1]}))" "${base_url}$(first "${commands[$1]}")" -d "$(printf "$(rest $(rest ${commands[$1]}))\n" "${args[@]}")"
|
|
echo ""
|
|
elif [[ "$(first $(rest ${commands[$1]}))" == "FUNC" ]]; then
|
|
eval "$(first ${commands[$1]})"
|
|
else
|
|
curl -sS "${headers[@]}" -X "$(first $(rest ${commands[$1]}))" "${base_url}$(first "${commands[$1]}")"
|
|
echo ""
|
|
fi
|
|
|
|
exit 0
|
|
fi
|
|
done
|
|
|
|
printf "${0}: invalid option -- $1\n"
|
|
usage
|
|
exit 1
|