-
Notifications
You must be signed in to change notification settings - Fork 207
Description
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:
- Provide a stable, machine-readable output format for all commands
- Make it easy to parse command output in scripts using standard JSON parsers
- Enable reliable automation and integration with other tools
- Follow common CLI best practices (similar to tools like
docker,kubectl,gh, etc.)
dstack ps --json
dstack offer --json
dstack gateway list --jsonWorkaround
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.