-
Notifications
You must be signed in to change notification settings - Fork 1k
Expand file tree
/
Copy pathmain.go
More file actions
85 lines (76 loc) · 1.84 KB
/
main.go
File metadata and controls
85 lines (76 loc) · 1.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
package main
import (
"encoding/json"
"fmt"
"log"
"os"
"os/exec"
"path/filepath"
"strings"
)
func parseExecCommand(path string) (string, error) {
var exec = struct {
Command string `json:"command"`
}{
Command: "generate",
}
execJsonPath := filepath.Join(path, "exec.json")
if _, err := os.Stat(execJsonPath); !os.IsNotExist(err) {
blob, err := os.ReadFile(execJsonPath)
if err != nil {
return "", err
}
if err := json.Unmarshal(blob, &exec); err != nil {
return "", err
}
}
return exec.Command, nil
}
func regenerate(dir string) error {
return filepath.Walk(dir, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if info.IsDir() {
return nil
}
if strings.HasSuffix(path, "sqlc.json") || strings.HasSuffix(path, "sqlc.yaml") || strings.HasSuffix(path, "sqlc.yml") {
cwd := filepath.Dir(path)
command, err := parseExecCommand(cwd)
if err != nil {
return fmt.Errorf("failed to parse exec.json: %w", err)
}
if command != "generate" {
return nil
}
var expectFailure bool
if _, err := os.Stat(filepath.Join(cwd, "stderr.txt")); !os.IsNotExist(err) {
expectFailure = true
}
cmd := exec.Command("sqlc-dev", "generate")
cmd.Env = append(cmd.Env, "SQLC_DUMMY_VALUE=true")
cmd.Dir = cwd
out, failed := cmd.CombinedOutput()
if failed != nil && !expectFailure {
return fmt.Errorf("%s: sqlc-dev generate failed\n%s", cwd, out)
}
if expectFailure {
if err := os.WriteFile(filepath.Join(cwd, "stderr.txt"), out, 0644); err != nil {
return fmt.Errorf("failed to update stderr.txt: %v", err)
}
}
}
return nil
})
}
func main() {
dirs := []string{
filepath.Join("internal", "endtoend", "testdata"),
filepath.Join("examples"),
}
for _, d := range dirs {
if err := regenerate(d); err != nil {
log.Fatal(err)
}
}
}