Repository navigation
feat(cli): add --json output for every command - #214
Open
neurekadev wants to merge 3 commits into
Open
neurekadev wants to merge 3 commits into
neurekadev wants to merge 3 commits into
Conversation
A global --json flag replaces the human-readable result with one JSON document on stdout, so scripts and other programs can consume vykar without parsing tables. Human output is unchanged. - New output module: commands build a Report and hand it to Output, which prints it at once or files it under the current repository and writes one versioned document at the end, also on failure. - JSON is built from CLI-side view types with raw byte counts, RFC 3339 UTC times, arrays and null, never from repository or wire types. - With --json nothing prompts; daemon and mount reject the flag. - Document the format in a new JSON Output reference page. Closes borgbase#35 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Fixes from the local review of the --json change: - Point stdout at stderr while a --json command runs, so hook output and stray prints cannot land before the document. - Fail with a clear error instead of prompting for a missing passphrase. - Hold results pre-serialized and stream the document, cutting the memory --json adds to large listings. - Write an error document for command-line usage errors too. - Report a bare cycle skipped by a hook or a missing repository as skipped. - Classify a repository's outcome once for its status and exit code. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Author
|
| # | Severity | Type | Finding | Status |
|---|---|---|---|---|
| 1 | Hook output on stdout corrupts the --json documentcrates/vykar-cli/src/output.rs:195 |
5fd9580d |
||
| 2 | Bare cycle prints its own backup summary, missing cloud-only lines (pre-existing)crates/vykar-cli/src/dispatch.rs:225 |
|||
| 3 | --json still prompts for a missing passphrase and can hangcrates/vykar-cli/src/passphrase.rs:54 |
5fd9580d |
||
| 4 | --json held every result as a Value treecrates/vykar-cli/src/output.rs:108 |
5fd9580d |
||
| 5 | Bare cycle skipped by a hook reported status okcrates/vykar-cli/src/lib.rs:562 |
5fd9580d |
||
| 6 | Repository outcome classified twice for status and exit codecrates/vykar-cli/src/output.rs:137 |
5fd9580d |
||
| 7 | Command-line usage errors wrote no JSON documentcrates/vykar-cli/src/lib.rs:55 |
5fd9580d |
||
| 8 | Cycle step statuses repeated the Status vocabularycrates/vykar-cli/src/dispatch.rs:194 |
5fd9580d |
1 · Hook output on stdout corrupts the --json document
- Scenario: A config with
hooks: { before: "echo hi" }andvykar backup --json: hooks inherit the process stdout, so stdout washifollowed by the document and JSON parsers failed. - Evidence: crates/vykar-core/src/hooks.rs runs hooks through shell::run_command_status_with_timeout with inherited stdout; reproduced with the debug binary.
- Fix: With --json, stdout (fd 1 on Unix, STD_OUTPUT_HANDLE on Windows) points at stderr while the command runs; the document is written to the saved original stdout at the end. Human mode is untouched. (
5fd9580d) - Fix check:
backup --jsonwith an echo hook: stdout is one JSON line, the hook text is on stderr; without --json the hook text stays on stdout.
2 · Bare cycle prints its own backup summary, missing cloud-only lines
- Scenario: On macOS with cloud-only files,
vykar backupprints the cloud-only summary but the barevykarcycle never does, because print_step_details duplicates the backup summary instead of reusing it. - Evidence: The base already had both copies and the difference; this PR keeps human output byte-identical.
- Follow-up: Pre-existing on main, and fixing it changes human output, so it is tracked separately.
3 · --json still prompts for a missing passphrase and can hang
- Scenario: An encrypted repository without passphrase, passcommand or VYKAR_PASSPHRASE, run as
vykar list --jsonby a program that keeps stdin open: the hidden prompt read stdin and blocked forever. - Evidence: Reproduced:
(sleep 6) | timeout 4 vykar list --jsonwas killed by timeout (exit 124). - Fix: In --json mode the passphrase prompt fails with "a passphrase is required; set VYKAR_PASSPHRASE or encryption.passcommand". (
5fd9580d) - Fix check: The same command now exits 1 at once with the error in the document; human mode still prompts as before.
4 · --json held every result as a Value tree
- Scenario:
vykar snapshot list latest --jsonon a 200,000-file snapshot peaked at 304 MB against 117 MB for the human listing. - Evidence: Measured peak RSS; each item became a serde_json::Value map, and the document was then built as one String.
- Fix: Results are stored pre-serialized (RawValue) in emit order and the document is streamed to stdout; output is always compact. (
5fd9580d) - Fix check: Same snapshot: 141 MB human vs 159 MB with --json; the output is still one valid JSON line.
- Fixed: The two reviewers rated this Medium and Low; fixed at the author's decision.
5 · Bare cycle skipped by a hook reported status ok
- Scenario:
vykar --jsonwith abeforehook exiting 75, or a missing local repository: the whole cycle was skipped but repositories[].status was "ok" and skip_reason null. - Evidence: The cycle records the skip as a step outcome, so the Err(Skipped) path that sets skip_reason never ran.
- Fix: When the cycle reports itself skipped, the repository is marked skipped with the reason. (
5fd9580d) - Fix check: The hook-skip and missing-repository cases now give status "skipped" with skip_reason; stderr is unchanged.
6 · Repository outcome classified twice for status and exit code
- Scenario: A new outcome or exit code had to be added both in Output::end_repo and in lib.rs finish/run_all_repos, or the JSON status and the exit code would disagree.
- Evidence: Both mapped CliResult<bool> plus the shutdown flag with the same precedence, one to a Status and one to an ExitCode.
- Fix: One Status::of classification and one Status::exit_code mapping, used by end_repo, run_targeted and run_all_repos. (
5fd9580d) - Fix check: Exit codes and stderr lines match the base build for single- and multi-repository runs, including continue-on-error.
Low and informational (2)
- 7 · Command-line usage errors wrote no JSON document (
crates/vykar-cli/src/lib.rs:55)- Scenario:
vykar list --json --last abcexited 2 with empty stdout. - Evidence: Cli::parse() exits before any Output exists.
- Fix: Cli::try_parse(); with --json a usage error also writes an error document, keeping clap's message on stderr and its exit code. (
5fd9580d) - Fix check: The error document is written with exit 2; --help, --version and non-JSON errors behave as before.
- Scenario:
- 8 · Cycle step statuses repeated the Status vocabulary (
crates/vykar-cli/src/dispatch.rs:194)- Scenario: Renaming a status word in output::Status would not reach result.cycle.steps[].status.
- Evidence: StepJson.status hand-wrote the same strings Status serializes to.
- Fix: StepJson.status is an output::Status. (
5fd9580d) - Fix check: The JSON is identical.
Dismissed (1)
crates/vykar-cli/src/cmd/backup.rs:70: Backup progress gating duplicated in backup.rs and dispatch.rs. Dismissed: Refuted: a single boolean expression, which the duplicate-logic definition excludes.
Claude Opus 5.5 (High) · T3 Code (Claude Code) · Round 1 of 3
neurekadev
marked this pull request as ready for review
November 25, 2026 01:29
Under --json nothing prompts, so `break-lock --sessions` could never remove session markers: the command had no --yes flag. The new flag skips the confirmation, as it does for upgrade and check --repair, and the refusal now names it. Also derive the multi-repository exit code from the shared Status mapping, and correct the JSON docs: a single path is a string. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Author
|
This branch has not been deployed
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.
Adds a global
--jsonflag that makes every command print one structured JSON document on stdout, for scripts and third-party integrations.Closes #35
Design
This follows the direction from the review of #128:
vykar --json listandvykar list --jsonboth work, and so does the barevykarcycle.nullfor missing values. Every record stands on its own; there is no host/label blanking as in the table.outputmodule holds anOutputvalue that is threaded through dispatch, and a smallReporttrait withjson()andprint_human(). Each command builds a report and callsout.emit(&report). Human mode prints it at once, as before. JSON mode files it under the current repository and writes one document at the end.SnapshotEntry/SnapshotStatsor other repository and wire types. A change to the on-disk format therefore cannot silently change the script interface.Document
{ "version": 1, "command": "list", "status": "ok", "error": null, "result": {}, "repositories": [ { "label": "main", "url": "/backup/repo", "status": "ok", "skip_reason": null, "error": null, "result": { "list": { "snapshots": [ ... ], "hidden": [] } } } ] }statusmirrors the exit code (ok/partial/error/interrupted; 0/3/1/130). Exit codes are unchanged.error. That covers a missing config, an unknown-R, and an ambiguous or missing snapshot during multi-repo probing.checkreportsresult.checkand setserror.resultis keyed by what ran.prune --compactgivesprune+compact, andcheck --repairgivescheck+repair. The bare cycle reportscyclewithsteps, plusbackup,prune,compactandcheckin the same shapes as the standalone commands.Behaviour with
--json--yes/--yes-delete-this-repo.check --repairwith a data-loss plan requires--yes, andconfigrequires--dest.break-lockgains a--yesflag for this, so scripts can clear stale session markers withbreak-lock --sessions --yes; without it, it still prompts or refuses as before.daemonandmountrefuse--json. Both run until stopped; the daemon already serves/api/status.json.-vis given. stderr otherwise stays as it was: progress, warnings and errors for people and logs.Human output is unchanged
I compared stdout, stderr and exit codes against an unmodified
mainbuild, and they are byte-identical:list,info,snapshot info/list --long/diff/find,prune -n [--list],compact -n,compact --report-unreferenced,check,check --repair -n,break-lock,key export, plus the error paths.init,backup,prune --list,compact,restore,snapshot delete, the bare cycle,prune --compact,check --repair --yes,break-lock --sessions,delete --yes-delete-this-repo,key import. These ran on two fresh twin repositories, with snapshot IDs, paths and durations normalized.Docs
docs/src/content/docs/json-output.md, covering the guarantees, the document format and the result for every command.--jsonrow in the common flags ofcommands.md.Tests
New integration tests in
cli_integration.rsparse stdout as JSON:list --json;listreports both.Locally,
make pre-commitpasses formatting, clippy (-D warnings) and the doc check. In the full test suite, 3,290 tests pass. Two tests fail in this sandbox (vykar-coreforce_remove_handles_restrictive_dirsandunreadable_pin_fails_closed). They also fail on unmodifiedmainthere, because it runs as root, which bypasses the file permissions those tests depend on. Neither touches this change.🤖 Generated with Claude Code