From 190798e57247116c8f8ae2b623719bccaefb3177 Mon Sep 17 00:00:00 2001 From: RD WebDesign Date: Wed, 19 Mar 2025 19:26:17 -0300 Subject: [PATCH] Allow simple `pihole api` output, containing only the JSON payload Signed-off-by: RD WebDesign --- advanced/Scripts/api.sh | 32 ++++++++++++++++++++++++-------- pihole | 2 +- 2 files changed, 25 insertions(+), 9 deletions(-) diff --git a/advanced/Scripts/api.sh b/advanced/Scripts/api.sh index 97192108..fda17458 100755 --- a/advanced/Scripts/api.sh +++ b/advanced/Scripts/api.sh @@ -301,14 +301,23 @@ secretRead() { } apiFunc() { - local data response status status_col + local data response status status_col verbose + + # Define if the output will be verbose (default) or silent + verbose="verbose" + if [ "$1" = "silent" ] || [ "$1" = "-s" ]; then + verbose="" + shift + fi # Authenticate with the API - LoginAPI verbose - echo "" + LoginAPI "${verbose}" - echo "Requesting: ${COL_PURPLE}GET ${COL_CYAN}${API_URL}${COL_YELLOW}$1${COL_NC}" - echo "" + if [ "${verbose}" = "verbose" ]; then + echo "" + echo "Requesting: ${COL_PURPLE}GET ${COL_CYAN}${API_URL}${COL_YELLOW}$1${COL_NC}" + echo "" + fi # Get the data from the API response=$(GetFTLData "$1" raw) @@ -325,11 +334,18 @@ apiFunc() { else status_col="${COL_RED}" fi - echo "Status: ${status_col}${status}${COL_NC}" + + # Only print the status in verbose mode or if the status is not 200 + if [ "${verbose}" = "verbose" ] || [ "${status}" != 200 ]; then + echo "Status: ${status_col}${status}${COL_NC}" + fi # Output the data. Format it with jq if available and data is actually JSON. # Otherwise just print it - echo "Data:" + if [ "${verbose}" = "verbose" ]; then + echo "Data:" + fi + if command -v jq >/dev/null && echo "${data}" | jq . >/dev/null 2>&1; then echo "${data}" | jq . else @@ -337,5 +353,5 @@ apiFunc() { fi # Delete the session - LogoutAPI verbose + LogoutAPI "${verbose}" } diff --git a/pihole b/pihole index 2c3a433b..248e9a7d 100755 --- a/pihole +++ b/pihole @@ -601,6 +601,6 @@ case "${1}" in "updatechecker" ) shift; updateCheckFunc "$@";; "arpflush" ) arpFunc "$@";; "-t" | "tail" ) tailFunc "$2";; - "api" ) apiFunc "$2";; + "api" ) shift; apiFunc "$@";; * ) helpFunc;; esac