Add JSON output support to list command and refactor tests#43
Merged
Add JSON output support to list command and refactor tests#43
Conversation
…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
3f68262 to
a04214c
Compare
Closed
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.
Summary
This PR adds JSON output capability to the
cronitor listcommand and refactors related code for better testability and maintainability.Key Changes
List Command Enhancements
--json/-jflag to output cron jobs as JSON instead of table formatgatherCrontabs()function for reusabilityjobLines()functionprintListAsTable()andprintListAsJSON()Test Improvements
createDefaultName()tests to uselib.Lineandlib.Crontabstructs instead of individual parameters, improving test clarity and reducing parameter countTestJobLines(): Validates filtering of lines without commandsTestPrintListAsJSON*(): Tests JSON output validity, structure, empty cases, and contentTestPrintListAsTable*(): Tests table output formatting and content inclusionImplementation Details
json.MarshalIndent()for readable formatting with 2-space indentationio.Writerparameter for better testabilitymakeCrontab(),makeLine()) provide consistent test fixture creationhttps://claude.ai/code/session_01K2duHV2B2CrFFVC5SYU6pH