Allow simple pihole api output, containing only the JSON payload

Signed-off-by: RD WebDesign <github@rdwebdesign.com.br>
This commit is contained in:
RD WebDesign 2025-03-19 19:26:17 -03:00
parent 3f5c00919b
commit d0615de6ef
No known key found for this signature in database
GPG Key ID: AE3C7FC910687F33
2 changed files with 25 additions and 9 deletions

View File

@ -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}"
}

2
pihole
View File

@ -592,6 +592,6 @@ case "${1}" in
"updatechecker" ) shift; updateCheckFunc "$@";;
"arpflush" ) arpFunc "$@";;
"-t" | "tail" ) tailFunc "$2";;
"api" ) apiFunc "$2";;
"api" ) shift; apiFunc "$@";;
* ) helpFunc;;
esac