Repository navigation
feat(cli): add --json output to status and run - #200
Merged
Merged
Conversation
Scripts, editors, and CI steps can read the results of status and run
only as human text, whose wording, symbols, and stream (stdout or
stderr) vary. Whether an installed hook is outdated is known only from
status's text, and which files a fixing check rewrote is not reported.
status --json prints one JSON document on stdout: the version, whether
the system is enabled, the config mode (modular or legacy) and path,
the repository root, the hooks directory, and for each hook type its
state (ready, outdated, not_executable, other, missing, or error),
whether it is executable, and whether it is outdated.
run --json prints one JSON document on stdout when the run ends: each
check's result (passed, failed, or skipped), duration, files, the files
it changed, and its error, suggestion, and tool output when it failed;
the files checked; the totals; and any warnings. All human output goes
to stderr instead, including every tool installation message, also
when a golangci-lint release download falls back to go install, and
the exit status is unchanged. A tool that is not installed, because
automatic installs are off or the install failed, is a failed check
(skipped with --graceful for lint) whose output is the install error.
When either command cannot report, it prints an error document,
{"version", "error": {"code", "message"}}, and exits 1. --json cannot
be combined with run --show-checks.
To report changed files, eof, whitespace, and fumpt implement a new
checks.ChangeReporter interface (fumpt now runs gofumpt -l -w), and the
runner records the files in CheckResult.ChangedFiles. config.Load
records which configuration it read, and the tool installer writes its
messages to a settable output (tools.SetOutput). The text output
without --json is unchanged. The README and both commands' help
document the fields, and tests pin the exact set of keys in each
document.
TestRunJSONAllPassed failed on CI: fumpt ran and tried to install gofumpt, which CI does not have. The test's legacy configuration disables fumpt, but the legacy .github/.env.base never overrides a value already in the environment, and other tests in the package load configuration that enables fumpt without restoring the environment afterwards. It passed locally only because gofumpt was on PATH. Every --json test now starts with no go-pre-commit configuration in the environment (restored when it ends), so its own configuration file is the only one it sees. Each one also runs with nothing but git on PATH and with empty GOPATH and GOBIN, and its configuration turns automatic installs off, so no test can use or install a tool from the machine it runs on; tests that need gofumpt put the fake one on PATH. TestRunJSONAllPassed sets the leaked value itself and checks that only the configured checks run.
Contributor
Code Coverage Analysis🟢 Overall Coverage: 92.7% Coverage Metrics
Coverage Breakdown
ResourcesGenerated via go-coverage • 2026-11-10 05:02:38 UTC |
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.
What Changed
go-pre-commit status --jsonprints one JSON document on stdout and nothing else: the version, whether the system is enabled, the config mode (modular/legacy) and path, the repository root, the hooks directory, and for each hook type (pre-commit, pre-push, commit-msg, post-commit) itsstate(ready,outdated,not_executable,other,missing,error),path,executable, andoutdated.go-pre-commit run --jsonprints one JSON document on stdout when the run ends: each check'sresult(passed,failed,skipped),duration_ms,files,changed_files, and itserror,suggestion, and tooloutputwhen it failed; the files checked;totals; andwarnings. All human output goes to stderr instead, including every tool installation message, also when a golangci-lint release download falls back togo install, so stdout holds only the JSON document. The exit status is unchanged.outputand the install command as itssuggestion; lint isskippedinstead with--graceful.{"version", "error": {"code", "message"}}and exits 1. Codes:config_not_found,config_invalid,not_a_repository,git_failed,no_checks_to_run,run_failed.--jsoncannot be combined withrun --show-checks.checks.ChangeReporter: eof, whitespace, and fumpt report the files they rewrote (fumpt now runsgofumpt -l -w); the runner records them inCheckResult.ChangedFiles.changed_filesis[]when nothing changed andnullfor checks that cannot tell.config.Loadrecords which configuration it read (Config.Source); the tool installer and its progress tracker write to a settable output (tools.SetOutput);output.NewWithWritersbuilds a formatter with explicit writers.Why It Was Necessary
Scripts, editors, and CI steps can only read
statusandrunresults as human text, whose wording, symbols, and stream (stdout or stderr) vary. Whether an installed hook is outdated (a byte comparison with the script the binary would write) is only visible instatus's text, and which files a fixing check rewrote was not reported at all.Testing Performed
--jsonwith--show-checks;go installfallback also fails (the install messages reach stderr only), failed and skipped with--graceful;go install, and temporary GOBIN, GOPATH, PATH, and HOME) to the writer set withSetOutput, with nothing on stdout.gofumpton a PATH holding only git, so no test can install a tool or reach the network. Unit tests coverRunWithChangesfor eof and whitespace, the runner'sChangedFiles(including panics and checks without files), progress output routing, and config source recording.gofumptreports a reformatted file; the built binary keeps stdout to the JSON document when a golangci-lint install falls back and fails; text output ofstatusandrunis identical to the previous release apart from timings.magex format:check,magex lint(0 issues),magex test,magex test:race,magex test:fuzz time=5s,magex tidy,magex deps:audit, andgo-pre-commit run --all-filespass; the touched packages also pass with-count=2 -shuffle=on.Impact / Risk
--json.-lto gofumpt to learn which files it rewrote; formatting is unchanged, and error output is still gofumpt's stderr.--gracefulis reported asskippedin JSON, while the existing text summary counts it as passed.