fix(cli): implement true YAML output support

The `--output yaml` flag was accepted but aliased to JSON since
printYAML() was never defined. Add printYAML() using gopkg.in/yaml.v3
(already an indirect dep via viper) and split all output format
branches into explicit json/yaml/table paths.

Affected commands: workspace list/create/inspect/audit,
agent list/inspect/peers, platform audit/health.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
Molecule AI · sdk-dev 2026-04-24 03:47:00 +00:00
parent 1ebe5fa999
commit ab91b652ef
4 changed files with 67 additions and 29 deletions

View File

@ -49,9 +49,12 @@ func runAgentList(cmd *cobra.Command, args []string) error {
if err != nil {
return fmt.Errorf("agent list: %w", err)
}
if outputFormat == "json" || outputFormat == "yaml" {
if outputFormat == "json" {
return printJSON(agents)
}
if outputFormat == "yaml" {
return printYAML(agents)
}
if len(agents) == 0 {
fmt.Println("No agents found.")
return nil
@ -81,9 +84,12 @@ func runAgentInspect(cmd *cobra.Command, args []string) error {
if err != nil {
return fmt.Errorf("agent inspect: %w", err)
}
if outputFormat == "json" || outputFormat == "yaml" {
if outputFormat == "json" {
return printJSON(a)
}
if outputFormat == "yaml" {
return printYAML(a)
}
w := tabwriter.NewWriter(os.Stdout, 0, 4, 2, ' ', 0)
kv(w, "ID", a.ID)
kv(w, "Name", a.Name)
@ -158,9 +164,12 @@ func runAgentPeers(cmd *cobra.Command, args []string) error {
if err != nil {
return fmt.Errorf("agent peers: %w", err)
}
if outputFormat == "json" || outputFormat == "yaml" {
if outputFormat == "json" {
return printJSON(peers)
}
if outputFormat == "yaml" {
return printYAML(peers)
}
if len(peers) == 0 {
fmt.Println("No peers found.")
return nil

View File

@ -54,6 +54,10 @@ func runPlatformAudit(cmd *cobra.Command, _ []string) error {
ID, Name, Status, Role string
AgentCount, DelegationCount int
}
byStatus := map[string]int{}
for _, ws := range workspaces {
byStatus[ws.Status]++
}
rows := make([]wsRow, 0, len(workspaces))
for _, ws := range workspaces {
ac := 0
@ -68,27 +72,28 @@ func runPlatformAudit(cmd *cobra.Command, _ []string) error {
})
}
if outputFormat == "json" || outputFormat == "yaml" {
type audit struct {
WorkspaceCount int `json:"workspace_count"`
AgentCount int `json:"agent_count"`
ByStatus map[string]int `json:"by_status"`
DelegationMap map[string]int `json:"delegations_by_workspace"`
Rows []wsRow `json:"workspaces"`
Agents []client.Agent `json:"agents"`
}
byStatus := map[string]int{}
for _, ws := range workspaces {
byStatus[ws.Status]++
}
return printJSON(audit{
WorkspaceCount: len(workspaces),
AgentCount: len(agents),
ByStatus: byStatus,
DelegationMap: delegationsByWS,
Rows: rows,
Agents: agents,
})
type audit struct {
WorkspaceCount int `json:"workspace_count"`
AgentCount int `json:"agent_count"`
ByStatus map[string]int `json:"by_status"`
DelegationMap map[string]int `json:"delegations_by_workspace"`
Rows []wsRow `json:"workspaces"`
Agents []client.Agent `json:"agents"`
}
auditReport := audit{
WorkspaceCount: len(workspaces),
AgentCount: len(agents),
ByStatus: byStatus,
DelegationMap: delegationsByWS,
Rows: rows,
Agents: agents,
}
if outputFormat == "json" {
return printJSON(auditReport)
}
if outputFormat == "yaml" {
return printYAML(auditReport)
}
w := tabwriter.NewWriter(os.Stdout, 0, 4, 2, ' ', 0)
@ -123,9 +128,13 @@ func runPlatformHealth(cmd *cobra.Command, _ []string) error {
fmt.Printf("Platform reachable at %s — raw status: %s\n", cl.BaseURL, string(body))
return nil
}
if outputFormat == "json" || outputFormat == "yaml" {
if outputFormat == "json" {
return printJSON(h)
}
if outputFormat == "yaml" {
return printYAML(h)
}
}
w := tabwriter.NewWriter(os.Stdout, 0, 4, 2, ' ', 0)
kv(w, "Status", h.Status)
kv(w, "Version", h.Version)

View File

@ -10,6 +10,7 @@ import (
"github.com/spf13/cobra"
"github.com/spf13/viper"
"gopkg.in/yaml.v3"
)
// Version is set at build time via -ldflags.
@ -114,6 +115,13 @@ func printJSON(v interface{}) error {
return json.NewEncoder(os.Stdout).Encode(v)
}
// printYAML writes v as YAML to stdout.
func printYAML(v interface{}) error {
enc := yaml.NewEncoder(os.Stdout)
enc.SetIndent(2)
return enc.Encode(v)
}
// kv writes a key-value pair to the tabwriter (only if v is non-empty).
func kv(w *tabwriter.Writer, k, v string) {
if v == "" {

View File

@ -43,9 +43,12 @@ func runWorkspaceList(cmd *cobra.Command, _ []string) error {
if err != nil {
return fmt.Errorf("workspace list: %w", err)
}
if outputFormat == "json" || outputFormat == "yaml" {
if outputFormat == "json" {
return printJSON(ws)
}
if outputFormat == "yaml" {
return printYAML(ws)
}
if len(ws) == 0 {
fmt.Println("No workspaces found.")
return nil
@ -115,9 +118,12 @@ func runWorkspaceCreate(cmd *cobra.Command, _ []string) error {
if err != nil {
return fmt.Errorf("workspace create: %w", err)
}
if outputFormat == "json" || outputFormat == "yaml" {
if outputFormat == "json" {
return printJSON(ws)
}
if outputFormat == "yaml" {
return printYAML(ws)
}
fmt.Printf("Workspace created: %s (%s)\n", ws.Name, ws.ID)
return nil
}
@ -138,9 +144,12 @@ func runWorkspaceInspect(cmd *cobra.Command, args []string) error {
if err != nil {
return fmt.Errorf("workspace inspect: %w", err)
}
if outputFormat == "json" || outputFormat == "yaml" {
if outputFormat == "json" {
return printJSON(ws)
}
if outputFormat == "yaml" {
return printYAML(ws)
}
w := tabwriter.NewWriter(os.Stdout, 0, 4, 2, ' ', 0)
kv(w, "ID", ws.ID)
kv(w, "Name", ws.Name)
@ -228,9 +237,12 @@ func runWorkspaceAudit(cmd *cobra.Command, _ []string) error {
Items: workspaces,
AgentList: agents,
}
if outputFormat == "json" || outputFormat == "yaml" {
if outputFormat == "json" {
return printJSON(report)
}
if outputFormat == "yaml" {
return printYAML(report)
}
w := tabwriter.NewWriter(os.Stdout, 0, 4, 2, ' ', 0)
fmt.Fprintln(w, "WORKSPACES\t")
fmt.Fprintln(w, "ID\tNAME\tSTATUS\tROLE\tRUNTIME")