| 1 | package cli |
| 2 | |
| 3 | import ( |
| 4 | "encoding/json" |
| 5 | "flag" |
| 6 | "fmt" |
| 7 | "os" |
| 8 | "path/filepath" |
| 9 | "time" |
| 10 | |
| 11 | "reasonix/internal/capdiag" |
| 12 | ) |
| 13 | |
| 14 | func doctorCapabilitiesCommand(args []string) int { |
| 15 | fs := flag.NewFlagSet("doctor capabilities", flag.ContinueOnError) |
| 16 | fs.SetOutput(os.Stderr) |
| 17 | root := fs.String("root", "", "workspace root (default: current directory)") |
| 18 | jsonOut := fs.Bool("json", false, "print a single JSON object to stdout") |
| 19 | live := fs.Bool("live", false, "start automatic MCP servers in an isolated Host (may network)") |
| 20 | timeoutStr := fs.String("timeout", "", "per-server live probe timeout (1s-60s; requires --live; default 5s)") |
| 21 | if code, ok := parseCommandFlags(fs, args); !ok { |
| 22 | return code |
| 23 | } |
| 24 | if fs.NArg() != 0 { |
| 25 | fmt.Fprintln(os.Stderr, "usage: reasonix doctor capabilities [--root PATH] [--json] [--live] [--timeout 5s]") |
| 26 | return 2 |
| 27 | } |
| 28 | |
| 29 | var timeout time.Duration |
| 30 | if *timeoutStr != "" { |
| 31 | if !*live { |
| 32 | fmt.Fprintln(os.Stderr, "error: --timeout requires --live") |
| 33 | return 2 |
| 34 | } |
| 35 | d, err := time.ParseDuration(*timeoutStr) |
| 36 | if err != nil { |
| 37 | fmt.Fprintf(os.Stderr, "error: invalid --timeout: %v\n", err) |
| 38 | return 2 |
| 39 | } |
| 40 | if d < capdiag.MinLiveTimeout || d > capdiag.MaxLiveTimeout { |
| 41 | fmt.Fprintf(os.Stderr, "error: --timeout must be between %s and %s\n", |
| 42 | capdiag.MinLiveTimeout, capdiag.MaxLiveTimeout) |
| 43 | return 2 |
| 44 | } |
| 45 | timeout = d |
| 46 | } else if *live { |
| 47 | timeout = capdiag.DefaultLiveTimeout |
| 48 | } |
| 49 | |
| 50 | ws := *root |
| 51 | if ws == "" { |
| 52 | if wd, err := os.Getwd(); err == nil { |
| 53 | ws = wd |
| 54 | } else { |
| 55 | ws = "." |
| 56 | } |
| 57 | } |
| 58 | if abs, err := filepath.Abs(ws); err == nil { |
| 59 | ws = abs |
| 60 | } |
| 61 | |
| 62 | if *live { |
| 63 | fmt.Fprintln(os.Stderr, capdiag.LiveWarningMessage()) |
| 64 | } |
| 65 | |
| 66 | report := capdiag.Collect(capdiag.Options{ |
| 67 | Root: ws, |
| 68 | Live: *live, |
| 69 | LiveTimeout: timeout, |
| 70 | }) |
| 71 | |
| 72 | if *jsonOut { |
| 73 | enc := json.NewEncoder(os.Stdout) |
| 74 | enc.SetIndent("", " ") |
| 75 | if err := enc.Encode(report); err != nil { |
| 76 | fmt.Fprintln(os.Stderr, err) |
| 77 | return 1 |
| 78 | } |
| 79 | } else { |
| 80 | fmt.Fprint(os.Stdout, capdiag.RenderText(report)) |
| 81 | } |
| 82 | |
| 83 | if capdiag.HasErrorSeverity(report) { |
| 84 | return 1 |
| 85 | } |
| 86 | return 0 |
| 87 | } |
| 88 |