|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "bufio" |
| 5 | + "fmt" |
| 6 | + "io/fs" |
| 7 | + "os" |
| 8 | + "path/filepath" |
| 9 | + "strings" |
| 10 | + |
| 11 | + "github.com/spf13/cobra" |
| 12 | +) |
| 13 | + |
| 14 | +func main() { |
| 15 | + var rootCmd = &cobra.Command{ |
| 16 | + Use: "multiline-sql-strings-lint", |
| 17 | + Short: "Lints multiline sql strings, replacing tabs with spaces", |
| 18 | + } |
| 19 | + dirPath := rootCmd.Flags().String("dir", "./internal/migration_acceptance_tests", "Directory path containing files to process") |
| 20 | + fix := rootCmd.Flags().Bool("fix", false, "Apply changes (without this flag, only shows what would change)") |
| 21 | + rootCmd.RunE = func(cmd *cobra.Command, args []string) error { |
| 22 | + if !*fix { |
| 23 | + fmt.Println("Running in dry-run mode. Use --fix to apply changes.") |
| 24 | + } |
| 25 | + |
| 26 | + var filesRequiringChanges []string |
| 27 | + if err := filepath.Walk(*dirPath, func(path string, info fs.FileInfo, err error) error { |
| 28 | + if err != nil { |
| 29 | + return err |
| 30 | + } |
| 31 | + |
| 32 | + fi, err := os.Stat(path) |
| 33 | + if err != nil { |
| 34 | + return fmt.Errorf("os.Stat: %w", err) |
| 35 | + } |
| 36 | + |
| 37 | + if fi.IsDir() { |
| 38 | + return nil |
| 39 | + } |
| 40 | + |
| 41 | + cr, err := processFile(path, *fix) |
| 42 | + if err != nil { |
| 43 | + return fmt.Errorf("processFile: %w", err) |
| 44 | + } |
| 45 | + if cr { |
| 46 | + filesRequiringChanges = append(filesRequiringChanges, path) |
| 47 | + } |
| 48 | + return nil |
| 49 | + }); err != nil { |
| 50 | + return fmt.Errorf("filepath.Walk: %w", err) |
| 51 | + } |
| 52 | + |
| 53 | + if len(filesRequiringChanges) == 0 { |
| 54 | + fmt.Println("No changes required!") |
| 55 | + return nil |
| 56 | + } |
| 57 | + |
| 58 | + verb := "require changes" |
| 59 | + if *fix { |
| 60 | + verb = "fixed" |
| 61 | + } |
| 62 | + fmt.Printf("The following files %s:\n", verb) |
| 63 | + for _, f := range filesRequiringChanges { |
| 64 | + fmt.Printf(" - %s\n", f) |
| 65 | + } |
| 66 | + if !*fix { |
| 67 | + // Return a non-zero exit code, since the linter failed, and fixes could not be applied. |
| 68 | + os.Exit(1) |
| 69 | + } |
| 70 | + return nil |
| 71 | + } |
| 72 | + if err := rootCmd.Execute(); err != nil { |
| 73 | + fmt.Println(err) |
| 74 | + os.Exit(1) |
| 75 | + } |
| 76 | +} |
| 77 | + |
| 78 | +// processFile replaces tabs with spaces in multi-line comments (sql strings). It will return if changes need to be made |
| 79 | +// and an error if any occurred. If fix is false, the files won't be updated. |
| 80 | +func processFile(filePath string, fix bool) (bool, error) { |
| 81 | + // Create a temporary file |
| 82 | + tempFile, err := os.CreateTemp(filepath.Dir(filePath), "temp_*") |
| 83 | + if err != nil { |
| 84 | + return false, fmt.Errorf("failed to create temp file: %w", err) |
| 85 | + } |
| 86 | + defer os.Remove(tempFile.Name()) // Clean up in case of failure |
| 87 | + defer tempFile.Close() |
| 88 | + |
| 89 | + // Open source file for reading |
| 90 | + srcFile, err := os.Open(filePath) |
| 91 | + if err != nil { |
| 92 | + return false, fmt.Errorf("failed to open source file: %w", err) |
| 93 | + } |
| 94 | + defer srcFile.Close() |
| 95 | + |
| 96 | + scanner := bufio.NewScanner(srcFile) |
| 97 | + writer := bufio.NewWriter(tempFile) |
| 98 | + |
| 99 | + inMultilineString := false |
| 100 | + changesRequired := false |
| 101 | + for scanner.Scan() { |
| 102 | + line := scanner.Text() |
| 103 | + newLine := line |
| 104 | + if inMultilineString && !strings.Contains(line, "`") && strings.Contains(line, "\t") { |
| 105 | + changesRequired = true |
| 106 | + newLine = strings.ReplaceAll(line, "\t", " ") |
| 107 | + } |
| 108 | + if _, err = writer.WriteString(newLine + "\n"); err != nil { |
| 109 | + return false, fmt.Errorf("writing new line: %w", err) |
| 110 | + } |
| 111 | + |
| 112 | + if strings.Contains(line, "`") { |
| 113 | + inMultilineString = !inMultilineString |
| 114 | + } |
| 115 | + } |
| 116 | + |
| 117 | + if err := scanner.Err(); err != nil { |
| 118 | + return false, fmt.Errorf("reading source file: %w", err) |
| 119 | + } |
| 120 | + |
| 121 | + if err := writer.Flush(); err != nil { |
| 122 | + return false, fmt.Errorf("lush buffer: %w", err) |
| 123 | + } |
| 124 | + |
| 125 | + return changesRequired, os.Rename(tempFile.Name(), filePath) |
| 126 | +} |
0 commit comments