From ab91b652ef3bb6113d6f03010ec87b5e86b867d6 Mon Sep 17 00:00:00 2001 From: Molecule AI SDK-Dev Date: Fri, 24 Apr 2026 03:47:00 +0000 Subject: [PATCH] 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 --- internal/cmd/agent.go | 15 ++++++++--- internal/cmd/platform.go | 53 +++++++++++++++++++++++---------------- internal/cmd/root.go | 8 ++++++ internal/cmd/workspace.go | 20 ++++++++++++--- 4 files changed, 67 insertions(+), 29 deletions(-) diff --git a/internal/cmd/agent.go b/internal/cmd/agent.go index 9569d75..2787e0d 100644 --- a/internal/cmd/agent.go +++ b/internal/cmd/agent.go @@ -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 diff --git a/internal/cmd/platform.go b/internal/cmd/platform.go index 93173e3..e77b96c 100644 --- a/internal/cmd/platform.go +++ b/internal/cmd/platform.go @@ -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) diff --git a/internal/cmd/root.go b/internal/cmd/root.go index 35f04e4..3aebe9a 100644 --- a/internal/cmd/root.go +++ b/internal/cmd/root.go @@ -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 == "" { diff --git a/internal/cmd/workspace.go b/internal/cmd/workspace.go index 26cdf33..65e831e 100644 --- a/internal/cmd/workspace.go +++ b/internal/cmd/workspace.go @@ -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")