Skip to content

Add JSON output support to list command and refactor tests#43

Merged
aflanagan merged 3 commits intomasterfrom
claude/review-go-cli-pr-SF6Xa
Feb 5, 2026
Merged

Add JSON output support to list command and refactor tests#43
aflanagan merged 3 commits intomasterfrom
claude/review-go-cli-pr-SF6Xa

Conversation

@aflanagan
Copy link
Contributor

Summary

This PR adds JSON output capability to the cronitor list command and refactors related code for better testability and maintainability.

Key Changes

List Command Enhancements

  • Added --json / -j flag to output cron jobs as JSON instead of table format
  • Extracted crontab gathering logic into gatherCrontabs() function for reusability
  • Extracted job line filtering into jobLines() function
  • Split output rendering into two functions: printListAsTable() and printListAsJSON()
  • Updated command help text to document the new JSON output option

Test Improvements

  • discover_test.go: Refactored createDefaultName() tests to use lib.Line and lib.Crontab structs instead of individual parameters, improving test clarity and reducing parameter count
  • list_test.go (new): Added comprehensive test coverage for list command functionality:
    • TestJobLines(): Validates filtering of lines without commands
    • TestPrintListAsJSON*(): Tests JSON output validity, structure, empty cases, and content
    • TestPrintListAsTable*(): Tests table output formatting and content inclusion

Implementation Details

  • JSON output uses json.MarshalIndent() for readable formatting with 2-space indentation
  • Empty crontabs (those with no job lines) are filtered from JSON output
  • Both output formats accept an io.Writer parameter for better testability
  • Test data helpers (makeCrontab(), makeLine()) provide consistent test fixture creation

https://claude.ai/code/session_01K2duHV2B2CrFFVC5SYU6pH

…ture

The createDefaultName function signature changed to accept *lib.Line and
*lib.Crontab instead of individual parameters. Update tests to construct
proper structs and adjust expected values for the current truncation logic
(maxNameLen=75).

https://claude.ai/code/session_01K2duHV2B2CrFFVC5SYU6pH
Add -j/--json flag to the list command that outputs discovered cron jobs
as structured JSON instead of a table. Leverages the existing MarshalJSON
implementations on Crontab and Line structs.

Resolves #20.

https://claude.ai/code/session_01K2duHV2B2CrFFVC5SYU6pH
- Filter non-job lines (comments, env vars) from JSON output so it
  matches the table output behavior. Creates a shallow copy of each
  crontab with only job lines before marshaling.
- Return error from printListAsJSON instead of calling os.Exit directly,
  making the error path testable.
- Check w.Write return value instead of discarding it.
- Add TestPrintListAsJSONExcludesNonJobLines test.

https://claude.ai/code/session_01K2duHV2B2CrFFVC5SYU6pH
@aflanagan aflanagan force-pushed the claude/review-go-cli-pr-SF6Xa branch from 3f68262 to a04214c Compare February 5, 2026 03:46
@aflanagan aflanagan merged commit 5209659 into master Feb 5, 2026
2 checks passed
@aflanagan aflanagan mentioned this pull request Feb 5, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants