Skip to content

Commit 988bdb9

Browse files
Cleanup linting script (#218)
1 parent e112755 commit 988bdb9

File tree

3 files changed

+134
-178
lines changed

3 files changed

+134
-178
lines changed

Makefile

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
.PHONY: code_gen format go_lint go_lint_fix go_mod_tidy lint sqlc sql_lint sql_lint_fix vendor sql_whitespace_dry_run sql_whitespace_fix
1+
# Sort targets alphabetically.
2+
.PHONY: code_gen format go_lint go_lint_fix go_mod_tidy lint multiline_sql_strings_lint_fix sqlc sql_lint sql_lint_fix vendor
23

34
code_gen: go_mod_tidy sqlc
45

@@ -11,9 +12,12 @@ go_lint_fix:
1112
go_mod_tidy:
1213
go mod tidy
1314

14-
lint: go_lint sql_lint
15+
lint: go_lint multiline_sql_strings_lint_fix sql_lint
1516

16-
lint_fix: go_lint_fix sql_lint_fix
17+
lint_fix: go_lint_fix multiline_sql_strings_lint_fix sql_lint_fix
18+
19+
multiline_sql_strings_lint_fix:
20+
go run ./scripts/lint/multiline_sql_strings_lint.go --fix
1721

1822
sqlc:
1923
cd internal/queries && sqlc generate
@@ -25,11 +29,4 @@ sql_lint_fix:
2529
sqlfluff fix
2630

2731
vendor:
28-
go mod vendor
29-
30-
sql_whitespace_dry_run:
31-
go run ./scripts/acceptance_test_sql_linter/main.go
32-
33-
sql_whitespace_fix:
34-
go run ./scripts/acceptance_test_sql_linter/main.go --fix
35-
32+
go mod vendor

scripts/acceptance_test_sql_linter/main.go

Lines changed: 0 additions & 167 deletions
This file was deleted.
Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
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

Comments
 (0)