From 190798e57247116c8f8ae2b623719bccaefb3177 Mon Sep 17 00:00:00 2001 From: RD WebDesign Date: Wed, 19 Mar 2025 19:26:17 -0300 Subject: [PATCH 1/2] 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 From dee44cb3da7aa27c2ee853830c944e3565d5e45e Mon Sep 17 00:00:00 2001 From: RD WebDesign Date: Sun, 11 May 2025 19:51:27 -0300 Subject: [PATCH 2/2] Define "silent" as default option and include "verbose" to the man page Signed-off-by: RD WebDesign --- advanced/Scripts/api.sh | 20 ++++++++++---------- manpages/pihole.8 | 15 ++++++++++++--- pihole | 1 + 3 files changed, 23 insertions(+), 13 deletions(-) diff --git a/advanced/Scripts/api.sh b/advanced/Scripts/api.sh index fda17458..2a3d361b 100755 --- a/advanced/Scripts/api.sh +++ b/advanced/Scripts/api.sh @@ -301,19 +301,19 @@ secretRead() { } apiFunc() { - local data response status status_col verbose + local data response status status_col verbosity - # Define if the output will be verbose (default) or silent - verbose="verbose" - if [ "$1" = "silent" ] || [ "$1" = "-s" ]; then - verbose="" + # Define if the output will be silent (default) or verbose + verbosity="silent" + if [ "$1" = "verbose" ]; then + verbosity="verbose" shift fi # Authenticate with the API - LoginAPI "${verbose}" + LoginAPI "${verbosity}" - if [ "${verbose}" = "verbose" ]; then + if [ "${verbosity}" = "verbose" ]; then echo "" echo "Requesting: ${COL_PURPLE}GET ${COL_CYAN}${API_URL}${COL_YELLOW}$1${COL_NC}" echo "" @@ -336,13 +336,13 @@ apiFunc() { fi # Only print the status in verbose mode or if the status is not 200 - if [ "${verbose}" = "verbose" ] || [ "${status}" != 200 ]; then + if [ "${verbosity}" = "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 - if [ "${verbose}" = "verbose" ]; then + if [ "${verbosity}" = "verbose" ]; then echo "Data:" fi @@ -353,5 +353,5 @@ apiFunc() { fi # Delete the session - LogoutAPI "${verbose}" + LogoutAPI "${verbosity}" } diff --git a/manpages/pihole.8 b/manpages/pihole.8 index 97a6ec68..e0c38828 100644 --- a/manpages/pihole.8 +++ b/manpages/pihole.8 @@ -23,7 +23,7 @@ pihole -r .br \fBpihole -g\fR .br -\fBpihole\fR -\fBq\fR [options] +\fBpihole\fR \fB-q\fR [options] .br \fBpihole\fR \fB-l\fR (\fBon|off|off noflush\fR) .br @@ -43,7 +43,7 @@ pihole -r .br \fBpihole\fR \fBcheckout\fR repo [branch] .br -\fBpihole\fR \api\fR endpoint +\fBpihole\fR \fBapi\fR [verbose] endpoint .br \fBpihole\fR \fBhelp\fR .br @@ -234,10 +234,14 @@ Available commands and options: branchname Update subsystems to the specified branchname .br -\fBapi\fR endpoint +\fBapi\fR [verbose] endpoint .br Query the Pi-hole API at .br + + verbose Show authentication and status messages +.br + .SH "EXAMPLE" Some usage examples @@ -323,6 +327,11 @@ Switching Pi-hole subsystem branches Queries FTL for the stats/summary endpoint .br +\fBpihole api verbose stats/summary\fR +.br + Same as above, but shows authentication and status messages +.br + .SH "COLOPHON" Get sucked into the latest news and community activity by entering Pi-hole's orbit. Information about Pi-hole, and the latest version of the software can be found at https://pi-hole.net. diff --git a/pihole b/pihole index 248e9a7d..1d5093c6 100755 --- a/pihole +++ b/pihole @@ -493,6 +493,7 @@ Debugging Options: Add an optional argument to filter the log (regular expressions are supported) api Query the Pi-hole API at + Precede with 'verbose' option to show authentication and status messages Options: