molecule-cli/internal/cmd/platform.go
claude-ceo-assistant 76f37d928f
All checks were successful
Release Go binaries / test (pull_request) Successful in 1m37s
Release Go binaries / release (pull_request) Has been skipped
fix(post-suspension): vanity import path go.moleculesai.app/cli (closes molecule-ai/internal#71 phase 2)
Migrates go.mod + 22 Go imports + README + comments + generated config
templates off the dead github.com/Molecule-AI/ identity onto the vanity
host go.moleculesai.app, owned by us.

Surfaces touched:
- go.mod module declaration: github.com/Molecule-AI/molecule-cli ->
  go.moleculesai.app/cli
- Every Go import statement under cmd/ + internal/
- README install section: rewritten to lead with the vanity install
  command (the previous text was migration-in-progress hedging)
- Comment URLs in internal/backends/backend.go + internal/cmd/connect.go
  (https://github.com/Molecule-AI/molecule-cli/issues/10) -> point at
  git.moleculesai.app/molecule-ai/molecule-cli
- Generated config templates in internal/cmd/init.go +
  internal/cmd/config.go: header URL updated so new users land on the
  live SCM
- Adds internal/lint/import_path_lint_test.go — structural test that
  walks every *.go / *.mod / Dockerfile / *.md / *.sh / *.yml in the
  module and rejects future references to github.com/Molecule-AI/ or
  Molecule-AI/molecule-monorepo. Mutation-tested before commit.

Test plan
- go build ./... clean
- go test ./... green (cmd/molecule + 5 internal packages + new lint
  gate, all pass)
- TestNoLegacyGitHubImportPaths fails on injected canary, passes on
  clean tree (no tautology)

Open dependency
- go.moleculesai.app responder must be deployed before
  'go install go.moleculesai.app/cli/cmd/molecule@latest' works
  externally. Internal builds + 'go build ./cmd/molecule' from a fresh
  clone work today (self-referential module path).
- Responder code prepared (worker.js, vendor-portable for CF Workers /
  Vercel Edge); deploy tracked separately under internal#71 phase 1.

Pairs with parallel migrations of plugin-gh-identity (#3) +
molecule-controlplane + molecule-core under the same internal#71 sweep.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-07 22:26:45 +00:00

153 lines
4.1 KiB
Go

// Package cmd implements the CLI command tree.
package cmd
import (
"fmt"
"io"
"net/http"
"os"
"text/tabwriter"
"go.moleculesai.app/cli/internal/client"
"github.com/spf13/cobra"
)
// ---------------------------------------------------------------------------
// Platform command group
// ---------------------------------------------------------------------------
var platformCmd = &cobra.Command{
Use: "platform",
Short: "Platform-level operations",
Long: `Audit the platform, check health, and inspect raw API responses.`,
}
func init() {
platformCmd.AddCommand(platformAuditCmd, platformHealthCmd)
}
// ===========================================================================
// mol platform audit
// ===========================================================================
var platformAuditCmd = &cobra.Command{
Use: "audit",
Short: "Full platform audit: workspaces, agents, delegation summary",
RunE: runPlatformAudit,
}
func runPlatformAudit(cmd *cobra.Command, _ []string) error {
cl := client.New(apiURL)
workspaces, agents, err := cl.AuditWorkspaces()
if err != nil {
return fmt.Errorf("platform audit: %w", err)
}
delegationsByWS := map[string]int{}
for _, ws := range workspaces {
dels, err := cl.GetDelegations(ws.ID)
if err == nil {
delegationsByWS[ws.ID] = len(dels)
}
}
type wsRow struct {
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
for _, a := range agents {
if a.WorkspaceID == ws.ID {
ac++
}
}
rows = append(rows, wsRow{
ID: ws.ID, Name: ws.Name, Status: ws.Status, Role: ws.Role,
AgentCount: ac, DelegationCount: delegationsByWS[ws.ID],
})
}
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)
fmt.Fprintf(w, "=== Platform Audit (%d workspaces, %d agents) ===\n\n",
len(workspaces), len(agents))
fmt.Fprintln(w, "WORKSPACE\tSTATUS\tROLE\tAGENTS\tDELEGATIONS")
for _, r := range rows {
fmt.Fprintf(w, "%s\t%s\t%s\t%d\t%d\n",
r.Name, r.Status, r.Role, r.AgentCount, r.DelegationCount)
}
return w.Flush()
}
// ===========================================================================
// mol platform health
// ===========================================================================
var platformHealthCmd = &cobra.Command{
Use: "health",
Short: "Check platform health and version",
RunE: runPlatformHealth,
}
func runPlatformHealth(cmd *cobra.Command, _ []string) error {
cl := client.New(apiURL)
h, err := cl.Health()
if err != nil {
// Fall back to raw check if /health 404s on older platforms.
body, hErr := platformRawHealth(cl.BaseURL)
if hErr != nil {
return fmt.Errorf("platform health: %w (and /health fallback also failed: %v)", err, hErr)
}
fmt.Printf("Platform reachable at %s — raw status: %s\n", cl.BaseURL, string(body))
return nil
}
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)
kv(w, "Uptime", h.Uptime)
kv(w, "Database", h.Database)
return w.Flush()
}
func platformRawHealth(baseURL string) ([]byte, error) {
req, _ := http.NewRequest("GET", baseURL+"/health", nil)
resp, err := httpClient().Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
return io.ReadAll(resp.Body)
}