Merge pull request #2718 from pi-hole/gravity-colour-tweak

Make colour output optional in streaming gravity API call
This commit is contained in:
Dominik
2025-11-22 09:48:52 +01:00
committed by GitHub
2 changed files with 23 additions and 2 deletions

View File

@@ -121,7 +121,15 @@ static int run_and_stream_command(struct ftl_conn *api, const char *path, const
int api_action_gravity(struct ftl_conn *api)
{
return run_and_stream_command(api, "/usr/local/bin/pihole", (const char *const []){ "pihole", "-g", NULL }, "FORCE_COLOR");
// Only set FORCE_COLOR if the client explicitly requests it via "color=true" query parameter
// This prevents ANSI escape codes from being included in the output for API consumers that don't need them
bool color = false;
const char *query = api->request != NULL ? api->request->query_string : "";
if(query != NULL)
get_bool_var(query, "color", &color);
const char *extra_env = color ? "FORCE_COLOR" : NULL;
return run_and_stream_command(api, "/usr/local/bin/pihole", (const char *const []){ "pihole", "-g", NULL }, extra_env);
}
int api_action_restartDNS(struct ftl_conn *api)

View File

@@ -4,11 +4,13 @@ components:
gravity:
post:
summary: Run gravity
parameters:
- $ref: 'action.yaml#/components/parameters/color'
tags:
- Actions
operationId: "action_gravity"
description: |
Update Pi-hole's adlists by running `pihole -g`. The output of the process is streamed with chunked encoding.
Update Pi-hole's adlists by running `pihole -g`. The output of the process is streamed with chunked encoding. Use the optional `color` query parameter to include ANSI color escape codes in the output.
responses:
'200':
description: OK
@@ -183,6 +185,17 @@ components:
- $ref: 'action.yaml#/components/errors/forbidden'
- $ref: 'common.yaml#/components/schemas/took'
parameters:
color:
name: color
in: query
description: Include ANSI color escape codes in the streamed output. Defaults to false to prevent colored output for API consumers that don't need formatting.
schema:
type: boolean
default: false
example: true
required: false
errors:
forbidden:
description: |