Skip to content

Add JSON Output to List Command #359

@josecelano

Description

@josecelano

Overview

Add machine-readable JSON output format (--output-format json) to the list command. This enables automation workflows and AI agents to programmatically extract environment information (full names, states, providers, timestamps) without parsing human-readable text tables that may truncate long names.

Parent Epic: #348 - Add JSON output format support
Roadmap: Section 12.5
Specification: docs/issues/359-add-json-output-to-list-command.md

Goals

Rationale

Why JSON Output for List?

The table format truncates long environment names to fit the terminal width, making it difficult to:

  • Parse environment names programmatically
  • Disambiguate environments with long names
  • Extract complete environment information for automation workflows

JSON output provides:

  • Full environment names without truncation
  • Structured data for easy parsing with jq or programming languages
  • Complete state and timestamp information for each environment
  • Failed environment details for error handling

Implementation Approach

Following the established Strategy Pattern from #349, #352, #355, and #357:

  1. Create JsonView in src/presentation/views/commands/list/views/json_view.rs
  2. Add #[derive(Serialize)] to EnvironmentList and EnvironmentSummary DTOs
  3. Update ListCommandController to accept output_format parameter
  4. Update router to pass output_format from ExecutionContext
  5. Add comprehensive unit tests
  6. Update user documentation with JSON examples and automation use cases

Example JSON Output

{
  "environments": [
    {
      "name": "production-high-availability-tracker",
      "state": "Running",
      "provider": "Hetzner",
      "created_at": "2026-02-14T16:45:00Z"
    },
    {
      "name": "staging-environment",
      "state": "Provisioned",
      "provider": "LXD",
      "created_at": "2026-02-10T09:15:00Z"
    }
  ],
  "total_count": 2,
  "failed_environments": [],
  "data_directory": "/path/to/project/data"
}

Automation Use Cases

# Extract all environment names
torrust-tracker-deployer list -o json | jq -r '.environments[].name'

# Filter by state
torrust-tracker-deployer list -o json | jq -r '.environments[] | select(.state == "Running") | .name'

# Count by provider
torrust-tracker-deployer list -o json | jq '.environments | group_by(.provider) | map({provider: .[0].provider, count: length})'

Success Criteria

  • ✅ JSON output implemented following Strategy Pattern
  • ✅ Full environment names provided without truncation
  • ✅ All tests passing (unit + integration)
  • ✅ All linters passing
  • ✅ Documentation updated with JSON examples and automation use cases
  • ✅ Backward compatible (text output unchanged)

Related Issues

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions