Skip to content

[AX] Support AUTHSOME_JSON to default CLI output to JSON #317

@ankitranjan7

Description

@ankitranjan7

Summary

The CLI already supports machine-readable output via --json, but it only works when the flag is passed explicitly on each invocation. There is currently no environment-level way to make JSON the default for unattended callers.

For agents, wrappers, and automation, that is brittle: forgetting --json on a single command switches stdout back to human-oriented output such as tables, pretty-printed text, or colored messages.

Because authsome is intended to be used by agents to manage credentials, this is not just a convenience issue. Predictable machine-facing output is part of the CLI's core product contract.

Current behavior

  • ContextObj stores a json_output boolean used across commands.
  • common_options() only sets that flag from the explicit --json Click option and otherwise defaults it to False.
  • When json_output is enabled, commands emit the existing versioned JSON envelope via ContextObj.print_json(...), for example:
{
  "v": 1,
  "status": "ok"
}
  • Error handling also already respects json_output and returns structured JSON with error and message keys.

There is no support today for AUTHSOME_JSON or any equivalent environment variable.

Why this matters

Automation often configures environment once and then invokes many CLI commands. Requiring --json every time is repetitive and easy to miss, especially when a higher-level agent or tool shells out to authsome in multiple places.

For an agent-oriented credential CLI, an environment variable would make JSON output opt-in at the process/session level and reduce accidental fallbacks to human-readable stdout.

Proposed behavior

Support an environment variable such as AUTHSOME_JSON that enables JSON mode globally.

When AUTHSOME_JSON is set to a truthy value such as 1, true, yes, or on:

  • the CLI should behave as if --json was passed
  • ContextObj.json_output should default to True
  • existing JSON payload shapes, including the "v": 1 envelope, should remain unchanged
  • existing structured JSON error behavior should remain unchanged

If the variable is unset, current behavior should remain unchanged.

Scope

Primary implementation area:

  • src/authsome/cli/context.py

Likely touch points:

  • common_options() for resolving the default value
  • tests covering representative commands and error handling
  • docs for CLI flags and environment variables

Non-goals / clarifications

  • This issue is about making structured stdout the default when requested via environment.
  • It does not require suppressing stderr warnings or verbose logs. Commands that currently write warnings to stderr may continue to do so.
  • It does not require replacing the existing --json flag; the flag should continue to work as it does today.

Acceptance criteria

  • AUTHSOME_JSON is recognized as a global opt-in for JSON mode.
  • With the variable set, commands produce the same stdout they would produce under explicit --json.
  • The existing {"v": 1, ...} JSON envelope is preserved.
  • JSON error responses continue to use the current error / message structure.
  • Behavior is unchanged when the variable is not set.
  • Tests cover at least one success path and one error path with AUTHSOME_JSON enabled.
  • Relevant CLI/environment-variable docs are updated.

Metadata

Metadata

Assignees

Labels

enhancementNew feature or requestneeds-triageMaintainer needs to evaluate this issue

Type

No type
No fields configured for issues without a type.

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions