|
| 1 | +package cli |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "path/filepath" |
| 6 | + |
| 7 | + "github.com/LAA-Software-Engineering/agentic-control-plane/internal/project" |
| 8 | + "github.com/LAA-Software-Engineering/agentic-control-plane/internal/render" |
| 9 | + "github.com/LAA-Software-Engineering/agentic-control-plane/internal/runtime/local" |
| 10 | + "github.com/LAA-Software-Engineering/agentic-control-plane/internal/spec" |
| 11 | + "github.com/spf13/cobra" |
| 12 | +) |
| 13 | + |
| 14 | +func newValidateCmd() *cobra.Command { |
| 15 | + return &cobra.Command{ |
| 16 | + Use: "validate", |
| 17 | + Short: "Validate project YAML, references, and schema files", |
| 18 | + SilenceUsage: true, |
| 19 | + Long: `Load the project from --project, apply Project defaults, optionally apply |
| 20 | +the selected Environment (-e / --env), then run spec validation. |
| 21 | +
|
| 22 | +Exit code 2 indicates validation failure (design doc sections 10.2, 11.2).`, |
| 23 | + RunE: runValidate, |
| 24 | + } |
| 25 | +} |
| 26 | + |
| 27 | +func runValidate(cmd *cobra.Command, args []string) error { |
| 28 | + _ = args |
| 29 | + g := Globals() |
| 30 | + root, err := filepath.Abs(filepath.Clean(g.ProjectRoot)) |
| 31 | + if err != nil { |
| 32 | + return NewExitErrorf(ExitValidationError, "project root: %w", err) |
| 33 | + } |
| 34 | + |
| 35 | + graph, err := project.LoadProject(root) |
| 36 | + if err != nil { |
| 37 | + return NewExitErrorf(ExitValidationError, "%w", err) |
| 38 | + } |
| 39 | + |
| 40 | + spec.NormalizeProjectGraph(graph) |
| 41 | + |
| 42 | + graph, err = local.ApplyEnvironment(graph, g.Env) |
| 43 | + if err != nil { |
| 44 | + return NewExitErrorf(ExitValidationError, "%w", err) |
| 45 | + } |
| 46 | + |
| 47 | + if err := spec.ValidateProjectGraph(graph, root); err != nil { |
| 48 | + return NewExitError(ExitValidationError, err) |
| 49 | + } |
| 50 | + |
| 51 | + return writeValidateSuccess(cmd, graph, g) |
| 52 | +} |
| 53 | + |
| 54 | +func writeValidateSuccess(cmd *cobra.Command, graph *spec.ProjectGraph, g *Global) error { |
| 55 | + out := cmd.OutOrStdout() |
| 56 | + envLabel := g.Env |
| 57 | + if envLabel == "" { |
| 58 | + envLabel = "(none)" |
| 59 | + } |
| 60 | + projName := graph.Meta.Name |
| 61 | + if projName == "" { |
| 62 | + projName = "(unnamed)" |
| 63 | + } |
| 64 | + n := resourceCount(graph) |
| 65 | + |
| 66 | + switch g.Output { |
| 67 | + case render.FormatJSON: |
| 68 | + payload := struct { |
| 69 | + Project string `json:"project"` |
| 70 | + Environment string `json:"environment"` |
| 71 | + ResourceCount int `json:"resourceCount"` |
| 72 | + Valid bool `json:"valid"` |
| 73 | + Message string `json:"message"` |
| 74 | + }{ |
| 75 | + Project: projName, |
| 76 | + Environment: envLabel, |
| 77 | + ResourceCount: n, |
| 78 | + Valid: true, |
| 79 | + Message: "Validation successful", |
| 80 | + } |
| 81 | + return render.WriteJSON(out, payload) |
| 82 | + case render.FormatYAML: |
| 83 | + return render.WriteYAML(out, map[string]any{ |
| 84 | + "project": projName, |
| 85 | + "environment": envLabel, |
| 86 | + "resourceCount": n, |
| 87 | + "valid": true, |
| 88 | + "message": "Validation successful", |
| 89 | + }) |
| 90 | + default: |
| 91 | + p := passPrefix(g) |
| 92 | + if _, err := fmt.Fprintf(out, "Project: %s\nEnvironment: %s\n\n", projName, envLabel); err != nil { |
| 93 | + return err |
| 94 | + } |
| 95 | + if _, err := fmt.Fprintf(out, "%s Loaded %d resources\n", p, n); err != nil { |
| 96 | + return err |
| 97 | + } |
| 98 | + if _, err := fmt.Fprintf(out, "%s References resolved\n", p); err != nil { |
| 99 | + return err |
| 100 | + } |
| 101 | + if _, err := fmt.Fprintf(out, "%s Schemas valid\n", p); err != nil { |
| 102 | + return err |
| 103 | + } |
| 104 | + if _, err := fmt.Fprintf(out, "%s All workflows valid\n", p); err != nil { |
| 105 | + return err |
| 106 | + } |
| 107 | + _, err := fmt.Fprintf(out, "\nValidation successful\n") |
| 108 | + return err |
| 109 | + } |
| 110 | +} |
| 111 | + |
| 112 | +func resourceCount(g *spec.ProjectGraph) int { |
| 113 | + if g == nil { |
| 114 | + return 0 |
| 115 | + } |
| 116 | + return len(g.Agents) + len(g.Tools) + len(g.Workflows) + len(g.Policies) + len(g.Environments) |
| 117 | +} |
| 118 | + |
| 119 | +func passPrefix(g *Global) string { |
| 120 | + if g != nil && g.NoColor { |
| 121 | + return "*" |
| 122 | + } |
| 123 | + return "✓" |
| 124 | +} |
0 commit comments