|
| 1 | +package cli |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "io" |
| 7 | + "os" |
| 8 | + "path/filepath" |
| 9 | + "strings" |
| 10 | + |
| 11 | + "github.com/LAA-Software-Engineering/agentic-control-plane/internal/plan" |
| 12 | + "github.com/LAA-Software-Engineering/agentic-control-plane/internal/render" |
| 13 | + "github.com/LAA-Software-Engineering/agentic-control-plane/internal/state/sqlite" |
| 14 | + "github.com/spf13/cobra" |
| 15 | +) |
| 16 | + |
| 17 | +func newPlanCmd() *cobra.Command { |
| 18 | + return &cobra.Command{ |
| 19 | + Use: "plan", |
| 20 | + Short: "Show desired vs applied deployment diff", |
| 21 | + SilenceUsage: true, |
| 22 | + Long: `Compare the validated project graph to rows in the SQLite deployment store and print |
| 23 | +a summary with create/update/delete lines plus a risk delta (design doc section 10.2). |
| 24 | +
|
| 25 | +The state database defaults to .agentic/state.db under --project, or project.spec.state.dsn, |
| 26 | +unless overridden by global --state. |
| 27 | +
|
| 28 | +Environment for stored rows is taken from -e / --env when set, otherwise "local". |
| 29 | +
|
| 30 | +Exit codes (section 11.2): |
| 31 | + 0 — success |
| 32 | + 1 — generic failure (e.g. cannot open SQLite) |
| 33 | + 2 — validation failure (invalid project) |
| 34 | + 3 — plan/apply conflict (reserved; not used in this MVP)`, |
| 35 | + RunE: runPlan, |
| 36 | + } |
| 37 | +} |
| 38 | + |
| 39 | +func planEnvironment(g *Global) string { |
| 40 | + if g == nil { |
| 41 | + return "local" |
| 42 | + } |
| 43 | + if s := strings.TrimSpace(g.Env); s != "" { |
| 44 | + return s |
| 45 | + } |
| 46 | + return "local" |
| 47 | +} |
| 48 | + |
| 49 | +func runPlan(cmd *cobra.Command, args []string) error { |
| 50 | + _ = args |
| 51 | + ctx := context.Background() |
| 52 | + g := Globals() |
| 53 | + |
| 54 | + graph, root, err := prepareProjectGraph(g.ProjectRoot, g) |
| 55 | + if err != nil { |
| 56 | + return NewExitError(ExitValidationError, err) |
| 57 | + } |
| 58 | + |
| 59 | + env := planEnvironment(g) |
| 60 | + dsn, err := resolveStateSQLitePath(root, graph, g.StatePath) |
| 61 | + if err != nil { |
| 62 | + return fmt.Errorf("plan: resolve state path: %w", err) |
| 63 | + } |
| 64 | + if err := os.MkdirAll(filepath.Dir(dsn), 0o755); err != nil { |
| 65 | + return fmt.Errorf("plan: create state directory: %w", err) |
| 66 | + } |
| 67 | + |
| 68 | + st, err := sqlite.Open(ctx, dsn) |
| 69 | + if err != nil { |
| 70 | + return fmt.Errorf("plan: open sqlite %q: %w", dsn, err) |
| 71 | + } |
| 72 | + defer func() { _ = st.Close() }() |
| 73 | + |
| 74 | + pl, err := plan.NewPlanner(st).ComputePlan(ctx, env, graph) |
| 75 | + if err != nil { |
| 76 | + return fmt.Errorf("plan: compute: %w", err) |
| 77 | + } |
| 78 | + |
| 79 | + return writePlanOutput(cmd, env, dsn, pl, g) |
| 80 | +} |
| 81 | + |
| 82 | +func writePlanOutput(cmd *cobra.Command, env, dsn string, p *plan.Plan, g *Global) error { |
| 83 | + out := cmd.OutOrStdout() |
| 84 | + switch g.Output { |
| 85 | + case render.FormatJSON: |
| 86 | + return writePlanJSON(out, env, dsn, p) |
| 87 | + case render.FormatYAML: |
| 88 | + return render.WriteYAML(out, planJSONModel(env, dsn, p)) |
| 89 | + default: |
| 90 | + if _, err := fmt.Fprintf(out, "Environment: %s\nState: %s\n\n", env, dsn); err != nil { |
| 91 | + return err |
| 92 | + } |
| 93 | + _, err := fmt.Fprint(out, plan.FormatPlan(p)) |
| 94 | + return err |
| 95 | + } |
| 96 | +} |
| 97 | + |
| 98 | +func planJSONModel(env, dsn string, p *plan.Plan) map[string]any { |
| 99 | + if p == nil { |
| 100 | + return map[string]any{ |
| 101 | + "environment": env, |
| 102 | + "statePath": dsn, |
| 103 | + "summary": map[string]any{"add": 0, "change": 0, "delete": 0}, |
| 104 | + "operations": []map[string]any{}, |
| 105 | + "risk": []string{}, |
| 106 | + } |
| 107 | + } |
| 108 | + nC, nU, nD := planCounts(p) |
| 109 | + ops := make([]map[string]any, 0, len(p.Operations)) |
| 110 | + for _, op := range p.Operations { |
| 111 | + entry := map[string]any{ |
| 112 | + "action": op.Action, |
| 113 | + "target": op.Target.String(), |
| 114 | + } |
| 115 | + if len(op.Diff) > 0 { |
| 116 | + diffs := make([]map[string]any, len(op.Diff)) |
| 117 | + for i, d := range op.Diff { |
| 118 | + diffs[i] = map[string]any{"path": d.Path, "old": d.Old, "new": d.New} |
| 119 | + } |
| 120 | + entry["diff"] = diffs |
| 121 | + } |
| 122 | + ops = append(ops, entry) |
| 123 | + } |
| 124 | + return map[string]any{ |
| 125 | + "environment": env, |
| 126 | + "statePath": dsn, |
| 127 | + "summary": map[string]any{ |
| 128 | + "add": nC, |
| 129 | + "change": nU, |
| 130 | + "delete": nD, |
| 131 | + }, |
| 132 | + "operations": ops, |
| 133 | + "risk": riskStrings(p), |
| 134 | + } |
| 135 | +} |
| 136 | + |
| 137 | +func riskStrings(p *plan.Plan) []string { |
| 138 | + if p == nil || len(p.Risk.Messages) == 0 { |
| 139 | + return []string{} |
| 140 | + } |
| 141 | + return p.Risk.Messages |
| 142 | +} |
| 143 | + |
| 144 | +func writePlanJSON(w io.Writer, env, dsn string, p *plan.Plan) error { |
| 145 | + return render.WriteJSON(w, planJSONModel(env, dsn, p)) |
| 146 | +} |
| 147 | + |
| 148 | +func planCounts(p *plan.Plan) (create, update, delete int) { |
| 149 | + if p == nil { |
| 150 | + return 0, 0, 0 |
| 151 | + } |
| 152 | + for _, op := range p.Operations { |
| 153 | + switch op.Action { |
| 154 | + case plan.ActionCreate: |
| 155 | + create++ |
| 156 | + case plan.ActionUpdate: |
| 157 | + update++ |
| 158 | + case plan.ActionDelete: |
| 159 | + delete++ |
| 160 | + } |
| 161 | + } |
| 162 | + return create, update, delete |
| 163 | +} |
0 commit comments