Skip to content

Add JSON Output to Run Command#358

Merged
josecelano merged 1 commit intomainfrom
357-add-json-output-to-run-command
Feb 17, 2026
Merged

Add JSON Output to Run Command#358
josecelano merged 1 commit intomainfrom
357-add-json-output-to-run-command

Conversation

@josecelano
Copy link
Copy Markdown
Member

Description

Implements JSON output format for the run command following the Strategy Pattern established in #349, #352, and #355.

Closes #357

Changes

Implementation

  • TextView (src/presentation/views/commands/run/text_view.rs): Refactors existing text output logic using shared components (CompactServiceUrlsView and DnsHintView)
  • JsonView (src/presentation/views/commands/run/json_view.rs): Serializes ServiceInfo and GrafanaInfo to JSON with environment_name and state wrapper
  • RunCommandController: Updated to accept output_format parameter and use Strategy Pattern to select appropriate view
  • Router: Updated to pass output_format from ExecutionContext to controller

Testing

  • 4 unit tests for TextView (basic output, grafana, DNS hints, tip)
  • 4 unit tests for JsonView (basic JSON, grafana, valid parsing, all fields)
  • All existing tests updated to pass OutputFormat::Text
  • All tests passing ✅
  • All linters passing ✅

Documentation

  • Added comprehensive JSON output section to docs/user-guide/commands/run.md
  • Documented JSON structure with examples for HTTP-only and HTTPS/TLS environments
  • Included automation use cases with jq examples
  • Documented all JSON fields with descriptions

Architecture

Follows the established pattern from previous implementations:

  1. TextView: Renders human-readable output with service URLs and DNS hints
  2. JsonView: Renders machine-readable JSON for automation
  3. Controller: Uses Strategy Pattern to select view based on output_format
  4. Router: Extracts output_format from context and passes to controller

Example Usage

Text Output (default)

torrust-tracker-deployer run my-env

JSON Output

torrust-tracker-deployer run my-env --output-format json
# Or short form:
torrust-tracker-deployer run my-env -o json

JSON with jq

# Extract API endpoint
torrust-tracker-deployer run my-env -o json | jq -r '.services.api_endpoint'

# Check if HTTPS is enabled
torrust-tracker-deployer run my-env -o json | jq -r '.services.api_uses_https'

# Get all HTTP tracker URLs
torrust-tracker-deployer run my-env -o json | jq -r '.services | (.direct_http_trackers + .https_http_trackers)[]'

Verification

Related Issues

Implement JSON output format for the run command following the Strategy Pattern established in #349, #352, and #355.

Changes:
- Add TextView and JsonView in src/presentation/views/commands/run/
- Update RunCommandController to accept output_format parameter
- Update router to pass output_format from ExecutionContext
- Update all unit tests to pass OutputFormat::Text
- Add JSON output documentation to docs/user-guide/commands/run.md

Implementation:
- TextView: Refactors existing text output using shared CompactServiceUrlsView and DnsHintView
- JsonView: Serializes ServiceInfo and GrafanaInfo to JSON with environment_name and state
- Controller: Uses Strategy Pattern to select view based on output_format
- Router: Extracts output_format from context and passes to controller

Testing:
- 4 unit tests for TextView (basic output, grafana, DNS hints, tip)
- 4 unit tests for JsonView (basic JSON, grafana, valid parsing, all fields)
- All existing tests updated to pass OutputFormat::Text

Documentation:
- Add JSON output section with structure, examples, and automation use cases
- Document all JSON fields with descriptions
- Add jq examples for common automation tasks
@josecelano josecelano self-assigned this Feb 17, 2026
@josecelano
Copy link
Copy Markdown
Member Author

ACK 81f925a

@josecelano josecelano merged commit 7945eea into main Feb 17, 2026
39 checks passed
@josecelano josecelano mentioned this pull request Feb 17, 2026
41 tasks
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 to run command

1 participant