Skip to content

feat(cli): add unified --format=json output to commands - #314

Merged
Morgy93 merged 3 commits into
mainfrom
feature/json-output-format
Dec 1, 2026
Merged

Morgy93 merged 3 commits into
mainfrom
feature/json-output-format

Conversation

@Morgy93

@Morgy93 Morgy93 commented Dec 1, 2026

Copy link
Copy Markdown
Collaborator

Summary

Adds a unified machine-readable output mode so scripts, CI and AI agents can parse MageForge commands reliably.

  • New shared --format=table|json option (and --json shortcut) in AbstractCommand (addFormatOption(), writeJson(), isJsonOutput()).
  • Wired into theme:list, theme:info, theme:build, template:obsolete, dependencies:update, hyva:compatibility:check, system:version and system:check.
  • theme:info --json and template:obsolete --format=json keep their existing output; they now use the shared helper.
  • In JSON mode stdout contains only the JSON document, progress and messages go to stderr, prompts and spinners are disabled, and failures write {"error": "..."} with a non-zero exit code.
  • Invalid --format values fail with an error.
  • JSON structures are documented in docs/commands_reference.md.

Testing

  • Unit tests for the shared behaviour (stdout/stderr separation, error documents, invalid format) and for each command's JSON output.
  • ddev phpunit, ddev phpcs, ddev mago lint|fmt --check|analyze are clean. PHPStan (level 9) shows no new findings in the changed code.

Closes #307
Refs #306

Add a shared --format=table|json option (plus --json shortcut) to AbstractCommand and
use it in theme:list, theme:info, theme:build, template:obsolete, dependencies:update,
hyva:compatibility:check, system:version and system:check.

In JSON mode stdout only carries the JSON document; progress and messages go to stderr,
prompts are disabled and failures write {"error": "..."} with a non-zero exit code.

Refs #307
Copilot AI balanced review requested due to automatic review settings December 1, 2026 14:49
@Morgy93 Morgy93 added Feature Command ai AI / agent integration labels Dec 1, 2026
@github-actions github-actions Bot added the documentation Improvements or additions to documentation label Dec 1, 2026

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

Unmatched wildcard builds omit the requested JSON output, and the JSON shortcut bypasses format validation, allowing invalid invocations to execute commands.

2 open issues
What changed in this PR

Adds shared JSON output support to eight MageForge CLI commands, giving scripts, CI and AI agents a consistent way to consume command results.

Changes:

  • Introduces shared format options, JSON writers, diagnostic-output routing and non-interactive handling.
  • Adds structured reports while migrating existing JSON modes to the shared helpers.
  • Documents output structures and adds unit coverage for command results and shared behaviour.
File Description
tests/​Unit/​Console/​Command/​Theme/​ListCommandTest.php Tests populated and empty JSON theme lists.
tests/​Unit/​Console/​Command/​Theme/​BuildCommandTest.php Tests build reports and missing arguments.
tests/​Unit/​Console/​Command/​System/​VersionCommandTest.php Tests version reports and unknown values.
tests/​Unit/​Console/​Command/​System/​CheckCommandTest.php Tests structured system reports.
tests/​Unit/​Console/​Command/​Hyva/​CompatibilityCheckCommandTest.php Tests compatibility and Tailwind readiness JSON.
tests/​Unit/​Console/​Command/​Dependencies/​UpdateCommandTest.php Tests update results and missing arguments.
tests/​Unit/​Console/​Command/​ConcreteTestCommand.php Adds JSON payload support to the test fixture.
tests/​Unit/​Console/​Command/​AbstractCommandTest.php Tests shared format handling and output separation.
src/​Console/​Command/​Theme/​ListCommand.php Adds JSON theme lists.
src/​Console/​Command/​Theme/​InfoCommand.php Migrates existing JSON output to shared helpers.
src/​Console/​Command/​Theme/​BuildCommand.php Adds structured build results and failures.
src/​Console/​Command/​Template/​ObsoleteCommand.php Uses shared JSON options and serialisation.
src/​Console/​Command/​System/​VersionCommand.php Adds version and update-availability JSON.
src/​Console/​Command/​System/​CheckCommand.php Adds structured system information.
src/​Console/​Command/​Hyva/​CompatibilityCheckCommand.php Adds compatibility and readiness reports.
src/​Console/​Command/​Dependencies/​UpdateCommand.php Adds structured dependency-update results.
src/​Console/​Command/​AbstractCommand.php Centralises JSON options, output and error handling.
docs/​commands_reference.md Documents machine-readable usage and result structures.

Review effort: Balanced

Comment thread src/Console/Command/AbstractCommand.php Resolved
- Validate the --format value before applying the --json shortcut so that
  --format=xml --json is rejected instead of executing the command.
- theme:build and dependencies:update write an empty JSON result when a
  wildcard matches no theme, keeping the no-op success exit code.
- Return a list from the obsolete override serialiser to satisfy mago analyze.

Refs #307
Copilot AI balanced review requested due to automatic review settings December 1, 2026 14:56

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔵 Needs a closer look

The command reference incorrectly implies that all non-zero exits include an error key, which could cause CI consumers to mishandle unsuccessful commands that retain normal result documents.

No open issues 🎉

1 previously missed issue

In code that hasn't changed since last review

Low severity Distinguish execution errors from unsuccessful result documents

docs/​commands_reference.md:31

Several commands return a non-zero exit code while retaining their normal result document: build/update failures, critical compatibility findings, and template:obsolete --fail-on-found. Since writeJsonError() deliberately preserves an already-written document, these responses have no error key. Please distinguish execution-error documents from unsuccessful result documents so CI consumers do not rely on an incorrect failure schema.

2 resolved since last review

Review effort: Balanced

- Assert exact JSON documents, error messages, stderr separation, unicode
  handling and success/failure exit codes in the JSON output tests.
- Move duration rounding into AbstractCommand::roundSeconds() so it can be
  tested deterministically.
- Remove equivalent mutants: drop the redundant jsonOutput reset, the
  nullable stdout, the redundant failedCount check and the 0 initialiser for
  the Tailwind CSS v4 error count.

Refs #307
Copilot AI balanced review requested due to automatic review settings December 1, 2026 15:16

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔵 Needs a closer look

Human assessment is needed because the documented non-zero-exit contract can mislead agents when failed commands retain result reports instead of returning an error object.

No open issues 🎉

Review effort: Balanced

@Morgy93
Morgy93 merged commit ad2eab6 into main Dec 1, 2026
22 checks passed
@Morgy93
Morgy93 deleted the feature/json-output-format branch December 1, 2026 15:38
@github-actions github-actions Bot mentioned this pull request Dec 1, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ai AI / agent integration Command documentation Improvements or additions to documentation Feature

Projects

None yet

Development

Successfully merging this pull request may close these issues.

feat(cli): unified --format=json output for all commands

3 participants