Skip to content

[Feature]: Add JSON output option for CLI commands #3322

@nikita-toffee-ai

Description

@nikita-toffee-ai

Problem

Currently, dstack CLI commands output data in a human-readable text format that is difficult to parse programmatically. When trying to extract specific information or integrate dstack commands into scripts and automation workflows, parsing the text output with tools like awk, grep, or sed is unreliable and cumbersome. Different commands may have varying output formats, making it challenging to build robust automation around dstack CLI operations.

Solution

Add a global --json flag (or --output json) to dstack CLI commands that would output structured data in JSON format. This would:

  1. Provide a stable, machine-readable output format for all commands
  2. Make it easy to parse command output in scripts using standard JSON parsers
  3. Enable reliable automation and integration with other tools
  4. Follow common CLI best practices (similar to tools like docker, kubectl, gh, etc.)
dstack ps --json
dstack offer --json
dstack gateway list --json

Workaround

Currently, the only option is to parse text output using shell utilities, for example:

gateway_list=$(dstack gateway list --verbose 2>&1 || echo "")

result=$(echo "${gateway_list}" | awk -v prefix="${GATEWAY_PREFIX}" '
          $1 ~ "^" prefix && $1 != "NAME" {
            gateway_name = $1
            domain = ""

            # Search through all fields for something that looks like a domain name
            # Domain should contain dots but NOT match IP address pattern
            for (i = 2; i <= NF; i++) {
              field = $i
              # Check if field contains dot and is not an IP address
              if (field ~ /\./ && field !~ /^[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+$/) {
                # Skip truncated values and header-like values
                if (field !~ /…$/ && field !~ /\.\.\.$/ && field !~ /^[A-Z]+$/) {
                  domain = field
                  break
                }
              }
            }

            if (domain != "") {
              print gateway_name "," domain
              exit
            }
          }
        ')

This approach is fragile, breaks when output formatting changes, and requires different parsing logic for each command.

Would you like to help us implement this feature by sending a PR?

No. I would gladly help out the dstack team with this one, but realistically I won't have time for that.

Metadata

Metadata

Labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions