feat(cmd): add --json machine-readable output for all commands - #41
Merged
Conversation
Add a persistent --json flag that makes every subcommand (info, read, update, create, fix) emit a single structured JSON document to stdout instead of the human output, for scripting and CI. Closes #12. Output is a stable, versioned envelope: {schema_version, markfluence_version, command, results, summary}. results holds one object per target (a single element for the single-target info/read); each command's result keys are per-command stable, with compound values (version, page_width, author stamps) as nested objects rather than display strings. Per-file operational failures are carried in results as {ok:false, error, code} and exit 1; fatal/pre-flight failures (bad flags, credential resolution) emit a typed error object to stderr and exit 2. Warnings and broken image/link notices become data (warnings/broken arrays) instead of stderr log lines. A new internal/jsonout package holds the envelope, the typed error object, the error-code vocabulary, and the shared value types. Each command's per-file worker was refactored to build a typed result struct consumed by both a human renderer and the JSON collector.
Add schema/json-output/v1.json (JSON Schema draft 2020-12) describing the full --json contract: the envelope root, per-command results/summary shapes selected via if/then on `command`, and the stderr error object at #/$defs/errorObject. Every object uses additionalProperties:false. Add internal/schematest, a test-only helper (using santhosh-tekuri/jsonschema/v6) that compiles the schema once; each command's TestSchemaConformance validates the command's actual marshaled output against it, so the schema cannot drift from the Go structs. Link the schema from the README.
Member
Author
|
Added a published JSON Schema for the output contract, per discussion:
Note: this adds a test-only dependency ( |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #12.
Adds a persistent
--jsonflag that makes every subcommand (info,read,update,create,fix) emit a single structured JSON document to stdout instead of the human output, for scripting and CI. Design was worked out in_plans/015_json-output.md.Output shape
A stable, versioned envelope for every command:
{ "schema_version": 1, "markfluence_version": "1.4.0", "command": "update", "results": [ { "ok": true, "status": "published", "file": "docs/foo.md", "page_id": "123", "version": { "previous": 3, "new": 4 }, "page_width": { "value": "max", "default": false }, "attachments": [], "warnings": [], "broken": [], "error": null, "code": null } ], "summary": { "total": 1, "succeeded": 1, "failed": 0, "skipped": 0 } }null/[]); the key set differs per command.schema_versionbumps on breaking changes.published/skipped,created/not_created,changed/consistent, plusfailed.info/readcarry data only.version,page_width, author stamps), never display strings.create's two-phase abort lists every input file (failed ones with anerror, the restnot_created) and setssummary.aborted: true.Errors & exit codes
resultsentries{ok:false, error, code}, exit1.2.CONFIG,AUTH,NOT_FOUND,VALIDATION,CONVERT,IO,NETWORK,API.Implementation
internal/jsonoutpackage: envelope, typed error object, code vocabulary +HTTPErrormapping, shared value types.ui.SetJSONsilences the human helpers so stdout stays pure JSON;ui.ErrSilentnow carries an exit code so the root can exit0/1/2.Notable decisions made during implementation
info/readomit afilekey (they take an id/URLARG, not aFILE) —filelives only on the batch commands.2for config/usage/bad-flag failures applies in both modes (previously such failures exited1) — more correct, but a human-mode behavior change worth noting.Tests
internal/jsonoutunit tests + per-command golden JSON tests built from hand-constructed result structs (no network mock needed).make test && make lint && make vetall green; smoke-tested end-to-end against a mock Confluence server.