Commit 5e6483c
Add --format json support for verifycommit and audit commands (#318)
* add --format json support for verifycommit and audit commands
This change adds machine-readable JSON output to the verifycommit and
audit commands, making it easier to extract data programmatically
(e.g., with jq) instead of parsing text output with bash.
Changes:
- Add common OutputFormat framework (internal/cmd/output.go)
- Add --format flag to verifycommit command with JSON support
- Add --format flag to audit command with JSON support
- Include comprehensive test coverage for both commands
The JSON output includes:
- verifycommit: success status, commit info, verified SLSA levels
- audit: commit results with summary statistics
All tests pass and the implementation maintains backward compatibility
with existing text output (default format).
🤖 Generated with [Claude Code](https://claude.com/claude-code)
Co-Authored-By: Claude <[email protected]>
* Fix golangci-lint issues in output formatting code
- Add status constants (statusPassed, statusFailed) to avoid string repetition
- Rename writeText to writeTextf following Go printf naming conventions
- Fix errcheck by adding nolint comment for intentional error ignore
- Auto-fix gci import grouping issues
All linting issues now resolved. Tests pass.
🤖 Generated with [Claude Code](https://claude.com/claude-code)
Co-Authored-By: Claude <[email protected]>
* Refactor audit command to use single loop for both JSON and text output
This addresses the code duplication issue raised in PR review. Instead of
having separate loops for JSON and text output that could diverge over time,
we now use a single loop that handles both cases.
Changes:
- Initialize JSON result structure before the loop (if needed)
- Single loop processes results for both formats
- Within loop, conditionally call JSON conversion or text printing
- Early termination conditions work for both formats
- Summary statistics (passed/failed counts) now tracked for both formats
This makes the code more maintainable and ensures JSON and text outputs
stay in sync.
🤖 Generated with [Claude Code](https://claude.com/claude-code)
Co-Authored-By: Claude <[email protected]>
* Improve test robustness with semantic JSON comparison
Replace string-based JSON comparison with semantic comparison using
reflect.DeepEqual. This makes tests more robust against:
- Field ordering changes in JSON output
- Whitespace/formatting differences
- Future changes to JSON encoder settings
Changes:
- Add shared assertJSONEqual helper function
- Convert test expectations from JSON strings to Go structs
- Tests now compare semantic meaning rather than exact string format
This addresses the code review feedback about brittle string comparisons.
🤖 Generated with [Claude Code](https://claude.com/claude-code)
Co-Authored-By: Claude <[email protected]>
* Implement idiomatic Go output handling with String() and writeResult()
This addresses PR review feedback to use Go idioms for output formatting:
1. Added String() method to VerifyCommitResult
- Implements fmt.Stringer interface for text output
- Eliminates need for manual string formatting at call sites
2. Created writeResult() method in outputOptions
- Automatically selects JSON or text output based on format
- Uses String() method for text when available
- Simplifies call sites from 4-6 lines to 1 line
Benefits:
- More idiomatic Go code using standard interfaces
- Eliminates repetitive if/else checks for output format
- Cleaner, more maintainable code
- Easier to add new output types in future
Example simplification in verifycommit.go:
Before: if opts.isJSON() { ... } else { opts.writeTextf(...) }
After: return opts.writeResult(result)
🤖 Generated with [Claude Code](https://claude.com/claude-code)
Co-Authored-By: Claude <[email protected]>
* Simplify OutputFormat from int to string type
Change OutputFormat from an int-based custom type with String(), Set(),
and Type() methods to simple string constants. This eliminates unnecessary
type conversion logic between int and string.
- Remove OutputFormat custom type and its methods
- Use string constants: OutputFormatText = "text", OutputFormatJSON = "json"
- Update format field in outputOptions from OutputFormat to string
- Change flag registration from Var() to StringVar() in audit.go and verifycommit.go
- Update tests to use string type instead of OutputFormat type
- Remove obsolete tests for OutputFormat.String() and OutputFormat.Set()
This addresses code review feedback requesting a simpler string-based
approach instead of the int-based enum pattern.
🤖 Generated with [Claude Code](https://claude.com/claude-code)
Co-Authored-By: Claude <[email protected]>
Signed-off-by: Ralph Bean <[email protected]>
* Replace writer property with getWriter() method
Replace the stored io.Writer property with a getWriter() method that
returns os.Stdout. This follows the "open -> write -> close" usage
pattern and prepares for future expansion to support file output.
- Remove writer field from outputOptions struct
- Add getWriter() method that returns os.Stdout
- Remove init() method that was only used to initialize writer
- Update all methods to call getWriter() instead of using oo.writer
- Update test to work without direct writer injection
This addresses code review feedback requesting that the writer not be
stored as a property, with the option to expand getWriter() later to
support file output.
🤖 Generated with [Claude Code](https://claude.com/claude-code)
Co-Authored-By: Claude <[email protected]>
Signed-off-by: Ralph Bean <[email protected]>
* Rename isJSON() to outputFormatIsJSON() for clarity
Rename the isJSON() method to outputFormatIsJSON() to make it more
descriptive when called on embedded option sets. This improves code
readability when the method is accessed as `opts.outputFormatIsJSON()`
instead of the more ambiguous `opts.isJSON()`.
- Rename isJSON() to outputFormatIsJSON() in outputOptions
- Update all call sites in audit.go to use new method name
- Update test name and assertions to match new method name
This addresses code review feedback requesting a more descriptive
method name for clarity when outputOptions is embedded in other structs.
🤖 Generated with [Claude Code](https://claude.com/claude-code)
Co-Authored-By: Claude <[email protected]>
Signed-off-by: Ralph Bean <[email protected]>
* Add AddFlags() and Validate() methods to outputOptions
Add architecture methods to outputOptions to follow the established
pattern used by other option sets in the codebase (branchOptions,
verifierOptions, etc.).
- Add AddFlags() method to initialize format and register the --format flag
- Add Validate() method to verify format is either 'text' or 'json'
- Add test coverage for Validate() method with valid and invalid inputs
- Import cobra package for AddFlags() implementation
This prepares outputOptions to be properly integrated into embedding
option sets following the command architecture pattern.
🤖 Generated with [Claude Code](https://claude.com/claude-code)
Co-Authored-By: Claude <[email protected]>
Signed-off-by: Ralph Bean <[email protected]>
* Integrate outputOptions into embedding option sets
Update auditOpts and verifyCommitOptions to properly use the new
outputOptions architecture methods instead of directly managing
the format flag.
- Call outputOptions.AddFlags() in both auditOpts and verifyCommitOptions
- Call outputOptions.Validate() in both Validate() methods
- Remove direct format flag registration from both files
- Remove manual format initialization (now handled by AddFlags())
This completes the refactoring to follow the established command
architecture pattern where option sets manage their own flags and
validation through AddFlags() and Validate() methods.
🤖 Generated with [Claude Code](https://claude.com/claude-code)
Co-Authored-By: Claude <[email protected]>
Signed-off-by: Ralph Bean <[email protected]>
---------
Signed-off-by: Ralph Bean <[email protected]>
Co-authored-by: Claude <[email protected]>1 parent c6bcaa0 commit 5e6483c
File tree
5 files changed
+770
-16
lines changed- internal/cmd
5 files changed
+770
-16
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
22 | 22 | | |
23 | 23 | | |
24 | 24 | | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
25 | 30 | | |
26 | 31 | | |
27 | 32 | | |
| |||
56 | 61 | | |
57 | 62 | | |
58 | 63 | | |
| 64 | + | |
59 | 65 | | |
60 | 66 | | |
61 | 67 | | |
62 | 68 | | |
63 | 69 | | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
64 | 101 | | |
65 | 102 | | |
66 | 103 | | |
67 | 104 | | |
| 105 | + | |
68 | 106 | | |
69 | 107 | | |
70 | 108 | | |
71 | 109 | | |
72 | 110 | | |
73 | 111 | | |
74 | 112 | | |
| 113 | + | |
75 | 114 | | |
76 | 115 | | |
77 | 116 | | |
| |||
124 | 163 | | |
125 | 164 | | |
126 | 165 | | |
127 | | - | |
| 166 | + | |
128 | 167 | | |
129 | | - | |
| 168 | + | |
130 | 169 | | |
131 | 170 | | |
132 | 171 | | |
| |||
157 | 196 | | |
158 | 197 | | |
159 | 198 | | |
| 199 | + | |
| 200 | + | |
| 201 | + | |
| 202 | + | |
| 203 | + | |
| 204 | + | |
| 205 | + | |
| 206 | + | |
| 207 | + | |
| 208 | + | |
| 209 | + | |
| 210 | + | |
| 211 | + | |
| 212 | + | |
| 213 | + | |
| 214 | + | |
| 215 | + | |
| 216 | + | |
| 217 | + | |
| 218 | + | |
| 219 | + | |
| 220 | + | |
| 221 | + | |
| 222 | + | |
| 223 | + | |
| 224 | + | |
| 225 | + | |
| 226 | + | |
| 227 | + | |
| 228 | + | |
| 229 | + | |
| 230 | + | |
| 231 | + | |
| 232 | + | |
| 233 | + | |
160 | 234 | | |
161 | 235 | | |
162 | 236 | | |
| |||
170 | 244 | | |
171 | 245 | | |
172 | 246 | | |
173 | | - | |
| 247 | + | |
| 248 | + | |
| 249 | + | |
| 250 | + | |
| 251 | + | |
| 252 | + | |
| 253 | + | |
| 254 | + | |
| 255 | + | |
| 256 | + | |
| 257 | + | |
| 258 | + | |
| 259 | + | |
| 260 | + | |
174 | 261 | | |
| 262 | + | |
175 | 263 | | |
| 264 | + | |
| 265 | + | |
| 266 | + | |
176 | 267 | | |
177 | 268 | | |
178 | 269 | | |
179 | 270 | | |
180 | | - | |
181 | | - | |
| 271 | + | |
| 272 | + | |
| 273 | + | |
| 274 | + | |
| 275 | + | |
| 276 | + | |
| 277 | + | |
| 278 | + | |
| 279 | + | |
| 280 | + | |
| 281 | + | |
| 282 | + | |
| 283 | + | |
| 284 | + | |
| 285 | + | |
| 286 | + | |
| 287 | + | |
| 288 | + | |
| 289 | + | |
182 | 290 | | |
183 | | - | |
| 291 | + | |
| 292 | + | |
184 | 293 | | |
185 | | - | |
186 | | - | |
| 294 | + | |
| 295 | + | |
| 296 | + | |
| 297 | + | |
187 | 298 | | |
188 | 299 | | |
189 | | - | |
190 | | - | |
| 300 | + | |
| 301 | + | |
| 302 | + | |
| 303 | + | |
191 | 304 | | |
192 | 305 | | |
193 | 306 | | |
194 | 307 | | |
| 308 | + | |
| 309 | + | |
| 310 | + | |
| 311 | + | |
| 312 | + | |
| 313 | + | |
| 314 | + | |
| 315 | + | |
| 316 | + | |
| 317 | + | |
195 | 318 | | |
196 | 319 | | |
0 commit comments