Skip to content

feat: Add JSON output format to configure command#373

Merged
josecelano merged 8 commits into
mainfrom
371-add-json-output-to-configure-command
Feb 23, 2026
Merged

feat: Add JSON output format to configure command#373
josecelano merged 8 commits into
mainfrom
371-add-json-output-to-configure-command

Conversation

@josecelano

Copy link
Copy Markdown
Member

Overview

Adds JSON output format support to the configure command, implementing roadmap task 12.6 as part of Phase 2 of the JSON output epic (#348).

Changes

New View Module

  • Created src/presentation/views/commands/configure/ module with DDD structure
  • Implemented ConfigureDetailsData DTO for presentation layer
  • Follows the provision/ module pattern with view_data/ and views/ subdirectories

Views Implementation

  • JsonView: Renders machine-readable JSON output
    • Returns String with inline error handling (list command pattern)
    • Pretty-printed JSON with all required fields
    • Fallback error JSON on serialization failure
  • TextView: Renders human-readable text output
    • Clear field labels and formatting
    • Handles optional instance_ip gracefully

Controller Updates

  • Added output_format: OutputFormat parameter to ConfigureCommandController::execute()
  • Implemented display_configure_results() method using Strategy Pattern
  • Updated router to extract and pass output_format from context

Testing

  • ✅ All 8 view tests pass (JsonView, TextView, DTO conversion)
  • ✅ All 7 controller tests updated and passing
  • ✅ All 2326 library tests pass
  • ✅ E2E tested locally with real LXD environment

JSON Output Example

{
  "environment_name": "my-env",
  "instance_name": "torrust-tracker-vm-my-env",
  "provider": "lxd",
  "state": "Configured",
  "instance_ip": "10.140.190.101",
  "created_at": "2026-02-23T08:26:06.789141249Z"
}

Usage

# Default text format (human-readable)
torrust-tracker-deployer configure my-env

# JSON format (machine-readable)
torrust-tracker-deployer configure my-env --output-format json

# Parse with jq
torrust-tracker-deployer configure my-env --output-format json | jq '.instance_ip'

Architecture

  • Pattern: Strategy Pattern for rendering (same as provision, list, create commands)
  • DDD Layer: Presentation layer only (no business logic)
  • Error Handling: Inline fallback for JSON serialization (list pattern)
  • Output: Through UserOutput methods (no direct println!)

Related

Closes #371

- Add ConfigureDetailsData DTO for presentation layer
- Implement JsonView and TextView for configure command
- Update ConfigureCommandController to accept output_format parameter
- Add display_configure_results() method using Strategy Pattern
- Update router to pass output_format from context
- Add comprehensive unit tests for views and controller
- Follow list command pattern (JsonView returns String with inline error handling)

Closes #371
@josecelano josecelano self-assigned this Feb 23, 2026
- Added assert_contains_all() helper to text_view.rs tests
  - Replaced 13 individual assert! calls with single helper call
  - Improved readability and maintainability

- Added assert_json_fields_eq() and assert_json_has_fields() helpers to json_view.rs tests
  - Replaced 6 assert_eq! calls with assert_json_fields_eq()
  - Replaced 6 assert! calls with assert_json_has_fields()
  - Better error messages showing expected vs actual values
- Added Phase 3: Avoiding Duplicate Code
  - Explains DRY principle for test fixtures and setup
  - Shows how to decouple helpers by adding parameters

- Added Step 6: Create assertion helpers
  - Demonstrates extracting repetitive assertions into reusable helpers
  - Includes before/after examples from configure view tests
  - Highlights benefits: readability, maintainability, better error messages
…ciples

- Added Beck's Four Rules in priority order:
  1. Passes the tests - code must work as intended
  2. Reveals intention - code should express purpose clearly
  3. No duplication - apply DRY/Once and Only Once
  4. Fewest elements - remove anything that doesn't serve prior rules

- Included reference to Martin Fowler's article
- Noted that rules feed off each other and empathy wins over metrics
- Fixed clippy::uninlined_format_args warnings in test helpers
- Updated assert_json_fields_eq() in json_view.rs
- Updated assert_json_has_fields() in json_view.rs
- Updated assert_contains_all() in text_view.rs
- All linters now pass successfully
@josecelano

Copy link
Copy Markdown
Member Author

ACK 831f177

@josecelano
josecelano merged commit 15b38de into main Feb 23, 2026
39 checks passed
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.

Add JSON output format to configure command

1 participant