|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "errors" |
| 5 | + "flag" |
| 6 | + "fmt" |
| 7 | + "io/fs" |
| 8 | + "os" |
| 9 | + "path/filepath" |
| 10 | + "sort" |
| 11 | + "strings" |
| 12 | +) |
| 13 | + |
| 14 | +func fmtCommand(args []string) error { |
| 15 | + fs := flag.NewFlagSet("fmt", flag.ContinueOnError) |
| 16 | + fs.SetOutput(new(flagErrorSink)) |
| 17 | + write := fs.Bool("w", false, "write result to source files instead of stdout") |
| 18 | + check := fs.Bool("check", false, "fail if any source file needs formatting") |
| 19 | + if err := fs.Parse(args); err != nil { |
| 20 | + return err |
| 21 | + } |
| 22 | + |
| 23 | + targets := fs.Args() |
| 24 | + if len(targets) == 0 { |
| 25 | + return errors.New("vibes fmt: path required") |
| 26 | + } |
| 27 | + |
| 28 | + files, err := collectVibeFiles(targets) |
| 29 | + if err != nil { |
| 30 | + return err |
| 31 | + } |
| 32 | + if len(files) == 0 { |
| 33 | + return nil |
| 34 | + } |
| 35 | + |
| 36 | + changedCount := 0 |
| 37 | + for _, path := range files { |
| 38 | + originalBytes, err := os.ReadFile(path) |
| 39 | + if err != nil { |
| 40 | + return fmt.Errorf("read %s: %w", path, err) |
| 41 | + } |
| 42 | + original := string(originalBytes) |
| 43 | + formatted := formatVibeSource(original) |
| 44 | + changed := formatted != original |
| 45 | + if changed { |
| 46 | + changedCount++ |
| 47 | + } |
| 48 | + |
| 49 | + switch { |
| 50 | + case *write && changed: |
| 51 | + info, err := os.Stat(path) |
| 52 | + if err != nil { |
| 53 | + return fmt.Errorf("stat %s: %w", path, err) |
| 54 | + } |
| 55 | + if err := os.WriteFile(path, []byte(formatted), info.Mode().Perm()); err != nil { |
| 56 | + return fmt.Errorf("write %s: %w", path, err) |
| 57 | + } |
| 58 | + case !*write && !*check: |
| 59 | + fmt.Print(formatted) |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + if *check && changedCount > 0 { |
| 64 | + return fmt.Errorf("vibes fmt: %d file(s) need formatting", changedCount) |
| 65 | + } |
| 66 | + |
| 67 | + return nil |
| 68 | +} |
| 69 | + |
| 70 | +func collectVibeFiles(targets []string) ([]string, error) { |
| 71 | + seen := make(map[string]struct{}) |
| 72 | + files := make([]string, 0) |
| 73 | + addFile := func(path string) { |
| 74 | + if filepath.Ext(path) != ".vibe" { |
| 75 | + return |
| 76 | + } |
| 77 | + abs, err := filepath.Abs(path) |
| 78 | + if err != nil { |
| 79 | + return |
| 80 | + } |
| 81 | + if _, ok := seen[abs]; ok { |
| 82 | + return |
| 83 | + } |
| 84 | + seen[abs] = struct{}{} |
| 85 | + files = append(files, abs) |
| 86 | + } |
| 87 | + |
| 88 | + for _, target := range targets { |
| 89 | + info, err := os.Stat(target) |
| 90 | + if err != nil { |
| 91 | + return nil, fmt.Errorf("stat %s: %w", target, err) |
| 92 | + } |
| 93 | + if !info.IsDir() { |
| 94 | + addFile(target) |
| 95 | + continue |
| 96 | + } |
| 97 | + err = filepath.WalkDir(target, func(path string, entry fs.DirEntry, walkErr error) error { |
| 98 | + if walkErr != nil { |
| 99 | + return walkErr |
| 100 | + } |
| 101 | + if entry.IsDir() { |
| 102 | + return nil |
| 103 | + } |
| 104 | + addFile(path) |
| 105 | + return nil |
| 106 | + }) |
| 107 | + if err != nil { |
| 108 | + return nil, fmt.Errorf("walk %s: %w", target, err) |
| 109 | + } |
| 110 | + } |
| 111 | + |
| 112 | + sort.Strings(files) |
| 113 | + return files, nil |
| 114 | +} |
| 115 | + |
| 116 | +func formatVibeSource(source string) string { |
| 117 | + normalized := strings.ReplaceAll(source, "\r\n", "\n") |
| 118 | + normalized = strings.ReplaceAll(normalized, "\r", "\n") |
| 119 | + |
| 120 | + lines := strings.Split(normalized, "\n") |
| 121 | + for i, line := range lines { |
| 122 | + lines[i] = strings.TrimRight(line, " \t") |
| 123 | + } |
| 124 | + |
| 125 | + joined := strings.Join(lines, "\n") |
| 126 | + joined = strings.TrimRight(joined, "\n") |
| 127 | + return joined + "\n" |
| 128 | +} |
0 commit comments