diff --git a/src/api/action.c b/src/api/action.c index 70436992..51ea9f29 100644 --- a/src/api/action.c +++ b/src/api/action.c @@ -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) diff --git a/src/api/docs/content/specs/action.yaml b/src/api/docs/content/specs/action.yaml index 893a42cf..b5d8edeb 100644 --- a/src/api/docs/content/specs/action.yaml +++ b/src/api/docs/content/specs/action.yaml @@ -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: |