|
| 1 | +package cli |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "fmt" |
| 6 | + "os" |
| 7 | + "path/filepath" |
| 8 | + |
| 9 | + "github.com/LAA-Software-Engineering/agentic-control-plane/internal/project" |
| 10 | + "github.com/LAA-Software-Engineering/agentic-control-plane/internal/render" |
| 11 | + "github.com/spf13/cobra" |
| 12 | +) |
| 13 | + |
| 14 | +func newFmtCmd() *cobra.Command { |
| 15 | + var check bool |
| 16 | + cmd := &cobra.Command{ |
| 17 | + Use: "fmt", |
| 18 | + Short: "Normalize YAML formatting for project.yaml and imports", |
| 19 | + Long: `Reformat every YAML file in the project closure (root project.yaml or project.yml plus |
| 20 | +all paths from spec.imports), using the same discovery rules as validate/load (design doc §10.2). |
| 21 | +
|
| 22 | +Writes 2-space indented YAML. Running fmt twice should make no further changes (idempotent). |
| 23 | +
|
| 24 | +WARNING: commit or back up your work before formatting. YAML comments may be dropped or moved |
| 25 | +because formatting round-trips through gopkg.in/yaml.v3. |
| 26 | +
|
| 27 | +With --check, no files are modified; the command exits with status 1 if any file would change |
| 28 | +(useful in CI). |
| 29 | +
|
| 30 | +Exit codes (§11.2): 0 success, 1 check failed or I/O error, 2 invalid project or unparseable YAML.`, |
| 31 | + SilenceUsage: true, |
| 32 | + RunE: func(cmd *cobra.Command, args []string) error { |
| 33 | + if len(args) > 0 { |
| 34 | + return NewExitError(ExitValidationError, fmt.Errorf("fmt: unexpected arguments")) |
| 35 | + } |
| 36 | + return runFmt(cmd, check) |
| 37 | + }, |
| 38 | + } |
| 39 | + cmd.Flags().BoolVar(&check, "check", false, "do not write; exit 1 if any file would be reformatted") |
| 40 | + return cmd |
| 41 | +} |
| 42 | + |
| 43 | +func runFmt(cmd *cobra.Command, check bool) error { |
| 44 | + g := Globals() |
| 45 | + root, err := filepath.Abs(filepath.Clean(g.ProjectRoot)) |
| 46 | + if err != nil { |
| 47 | + return NewExitError(ExitValidationError, fmt.Errorf("fmt: project root: %w", err)) |
| 48 | + } |
| 49 | + paths, err := project.ListProjectYAMLFiles(root) |
| 50 | + if err != nil { |
| 51 | + return NewExitError(ExitValidationError, fmt.Errorf("fmt: %w", err)) |
| 52 | + } |
| 53 | + |
| 54 | + wouldChange := 0 |
| 55 | + written := 0 |
| 56 | + for _, p := range paths { |
| 57 | + b, err := os.ReadFile(p) |
| 58 | + if err != nil { |
| 59 | + return fmt.Errorf("fmt: read %s: %w", p, err) |
| 60 | + } |
| 61 | + norm, err := project.NormalizeYAML(b) |
| 62 | + if err != nil { |
| 63 | + return NewExitError(ExitValidationError, fmt.Errorf("fmt: %s: %w", p, err)) |
| 64 | + } |
| 65 | + if bytes.Equal(b, norm) { |
| 66 | + continue |
| 67 | + } |
| 68 | + wouldChange++ |
| 69 | + if check { |
| 70 | + continue |
| 71 | + } |
| 72 | + info, err := os.Stat(p) |
| 73 | + if err != nil { |
| 74 | + return fmt.Errorf("fmt: stat %s: %w", p, err) |
| 75 | + } |
| 76 | + mode := info.Mode().Perm() |
| 77 | + if err := os.WriteFile(p, norm, mode); err != nil { |
| 78 | + return fmt.Errorf("fmt: write %s: %w", p, err) |
| 79 | + } |
| 80 | + written++ |
| 81 | + } |
| 82 | + |
| 83 | + out := cmd.OutOrStdout() |
| 84 | + switch g.Output { |
| 85 | + case render.FormatJSON: |
| 86 | + if err := render.WriteJSON(out, map[string]any{ |
| 87 | + "projectRoot": root, |
| 88 | + "check": check, |
| 89 | + "files": len(paths), |
| 90 | + "changed": wouldChange, |
| 91 | + "written": written, |
| 92 | + }); err != nil { |
| 93 | + return err |
| 94 | + } |
| 95 | + case render.FormatYAML: |
| 96 | + if err := render.WriteYAML(out, map[string]any{ |
| 97 | + "projectRoot": root, |
| 98 | + "check": check, |
| 99 | + "files": len(paths), |
| 100 | + "changed": wouldChange, |
| 101 | + "written": written, |
| 102 | + }); err != nil { |
| 103 | + return err |
| 104 | + } |
| 105 | + default: |
| 106 | + if check { |
| 107 | + if wouldChange > 0 { |
| 108 | + _, _ = fmt.Fprintf(out, "%d file(s) would be reformatted\n", wouldChange) |
| 109 | + } else { |
| 110 | + _, _ = fmt.Fprintln(out, "All YAML files already formatted.") |
| 111 | + } |
| 112 | + } else { |
| 113 | + _, _ = fmt.Fprintf(out, "Formatted %d file(s); %d unchanged (%d total).\n", written, len(paths)-wouldChange, len(paths)) |
| 114 | + } |
| 115 | + } |
| 116 | + |
| 117 | + if check && wouldChange > 0 { |
| 118 | + return NewExitError(ExitGenericFailure, fmt.Errorf("fmt: %d file(s) need formatting (run without --check)", wouldChange)) |
| 119 | + } |
| 120 | + return nil |
| 121 | +} |
0 commit comments