Add JSON output format support to create command#351
Merged
josecelano merged 5 commits intomainfrom Feb 16, 2026
Merged
Conversation
Extract output formatting logic from controller to view layer, matching the MVC pattern used by provision, list, and show commands. Changes: - Create src/presentation/views/commands/create/ module - Add EnvironmentDetailsData struct (presentation DTO) - Add EnvironmentDetailsView with render_human_readable() method - Implement From<&Environment<Created>> for data conversion - Update controller to delegate formatting to view - Add 3 unit tests for view rendering logic Testing: - All 426 tests pass - Golden test output unchanged (backward compatible) - View layer independently testable This refactoring is a prerequisite for adding JSON output support. The view layer separation makes format switching straightforward. Related: #349 (Phase 0 of 5)
- Add OutputFormat enum with Text (default) and Json variants - Add output_format field to GlobalArgs (global CLI argument) - Update specification to document OutputFormat approach - Follow existing LogFormat pattern for consistency This is Phase 1: CLI argument infrastructure. Phases 2-3 will implement JSON rendering in views and controllers.
Split view into separate format-specific views following SOLID principles: - environment_details.rs: DTO only (data structure) - text_view.rs: Human-readable text rendering - json_view.rs: Machine-readable JSON rendering Benefits: - Open/Closed Principle: Add formats without modifying existing code - Single Responsibility: Each view has one job - Strategy Pattern: Different rendering strategies for same data - Scalability: Easy to add XML, YAML, CSV, etc. Updated: - Controller now uses TextView::render() instead of EnvironmentDetailsView - Module exports TextView and JsonView separately - All tests pass (6 view tests, 41 controller tests) This refactor makes Phase 3 (format switching) cleaner and more maintainable.
- Add global --output-format argument (text|json) to CLI - Implement Strategy Pattern with TextView and JsonView - Wire output_format through ExecutionContext to controllers - Add format switching logic in create environment handler - Create test helper default_global_args() for test contexts - Add OutputFormatting error variant with detailed help - Update all doctests to use new ExecutionContext signature - Fix clippy issues (doc backticks, inline format args) The implementation follows SOLID principles with clear separation between data (EnvironmentDetailsData), rendering strategies (TextView/JsonView), and controller orchestration.
- Add comprehensive Output Formats section to create command guide - Document JSON schema with field descriptions and table - Add automation examples (Shell, CI/CD, Python, multi-env scripts) - Explain output channel separation (stdout vs stderr) - Add usage guidelines for text vs JSON format selection - Include validation and debugging examples with jq - Update common options to include --output-format flag - Mark Phase 4 documentation complete in issue spec
Member
Author
|
ACK ad2ac21 |
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
Implements issue #349 - Adds machine-readable JSON output format to the
create environmentcommand, enabling automation workflows and CI/CD integration.Changes
Architecture (5 Phases)
Phase 0: View Layer Extraction (67c10ff)
EnvironmentDetailsDataDTO andEnvironmentDetailsViewPhase 1: Global OutputFormat Argument (7ce64b0)
OutputFormatenum (Text | Json) with clap integration--output-formatglobal CLI argumentGlobalArgsandExecutionContextwith format supportPhase 2: Strategy Pattern Implementation (2ecb391)
Serializederive to EnvironmentDetailsDataPhase 3: Format Switching (afa7ef9)
Phase 4: Documentation (ad2ac21)
Features
✅ Two Output Formats:
✅ JSON Output Schema:
{ "environment_name": "my-env", "instance_name": "torrust-tracker-vm-my-env", "data_dir": "./data/my-env", "build_dir": "./build/my-env", "created_at": "2026-02-16T13:38:02.446056727Z" }✅ CLI Usage:
Quality Assurance
Documentation
Testing
Manual Verification
Automation Example
Related
Breaking Changes
None. This is a backward-compatible addition. Default behavior (text output) is preserved.
Future Work
This implementation provides the foundation for adding JSON output to other commands: