Skip to content

Add JSON output to show command #355

@josecelano

Description

@josecelano

Parent Epic: #348 - Add JSON output format support
Related: Roadmap Section 12.3, #349 ✅ Completed, #352 ✅ Completed

Overview

Add machine-readable JSON output format (--output-format json) to the show command. This enables automation workflows and AI agents to programmatically extract environment state, instance IP address, and service URLs without parsing human-readable text.

Goals

Implementation Approach

Step 1: Add Serde Derives to DTOs

The EnvironmentInfo and related DTOs in src/application/command_handlers/show/info/ need #[derive(Serialize)]:

  • EnvironmentInfo
  • InfrastructureInfo
  • ServiceInfo (in info/tracker.rs)
  • PrometheusInfo (in info/prometheus.rs)
  • GrafanaInfo (in info/grafana.rs)
  • TlsDomainInfo (in info/tracker.rs)
  • LocalhostServiceInfo (in info/tracker.rs)

Step 2: Reorganize Views Module

Following the refactor from PR #354 (Separate View Data from Views):

src/presentation/views/commands/show/
├── views/
│   ├── mod.rs            (Re-exports TextView and JsonView)
│   ├── text_view.rs      (Renamed from mod.rs - TextView implementation)
│   ├── json_view.rs      (NEW - JsonView implementation)
│   └── ... (existing helper modules for TextView)
└── mod.rs

Step 3: Create JsonView

Simple JSON serialization using serde_json::to_string_pretty() on the EnvironmentInfo DTO.

Step 4: Update Controller

Wire view selection in src/presentation/controllers/show/handler.rs:

let output = match ctx.output_format() {
    OutputFormat::Text => TextView::render(&info),
    OutputFormat::Json => JsonView::render(&info),
};

CLI Interface

# Human-readable output (default, unchanged)
torrust-tracker-deployer show my-env

# JSON output (new)
torrust-tracker-deployer show my-env --output-format json

# Short form
torrust-tracker-deployer show my-env -o json

Success Criteria

  • JSON output correctly serializes EnvironmentInfo and nested DTOs
  • JSON output varies correctly by state (created → provisioned → running)
  • Text output remains unchanged (existing tests pass)
  • Unit tests cover all state variations
  • Manual testing validates real-world usage
  • All linters pass
  • User documentation updated

Documentation

Full specification: docs/issues/355-add-json-output-to-show-command.md


Roadmap Task: 12.3
Pattern: Strategy Pattern (established in #349 and #352)
Architecture: Presentation layer only - no changes to application or domain layers

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions