1
1
package main
2
2
3
3
import (
4
+ "encoding/json"
4
5
"fmt"
5
6
"log"
6
7
"os"
@@ -9,6 +10,27 @@ import (
9
10
"strings"
10
11
)
11
12
13
+ func parseExecCommand (path string ) (string , error ) {
14
+ var exec = struct {
15
+ Command string `json:"command"`
16
+ }{
17
+ Command : "generate" ,
18
+ }
19
+
20
+ execJsonPath := filepath .Join (path , "exec.json" )
21
+ if _ , err := os .Stat (execJsonPath ); ! os .IsNotExist (err ) {
22
+ blob , err := os .ReadFile (execJsonPath )
23
+ if err != nil {
24
+ return "" , err
25
+ }
26
+ if err := json .Unmarshal (blob , & exec ); err != nil {
27
+ return "" , err
28
+ }
29
+ }
30
+
31
+ return exec .Command , nil
32
+ }
33
+
12
34
func regenerate (dir string ) error {
13
35
return filepath .Walk (dir , func (path string , info os.FileInfo , err error ) error {
14
36
if err != nil {
@@ -19,6 +41,15 @@ func regenerate(dir string) error {
19
41
}
20
42
if strings .HasSuffix (path , "sqlc.json" ) || strings .HasSuffix (path , "sqlc.yaml" ) {
21
43
cwd := filepath .Dir (path )
44
+ command , err := parseExecCommand (cwd )
45
+ if err != nil {
46
+ return fmt .Errorf ("failed to parse exec.json: %w" , err )
47
+ }
48
+
49
+ if command != "generate" {
50
+ return nil
51
+ }
52
+
22
53
cmd := exec .Command ("sqlc-dev" , "generate" , "--experimental" )
23
54
cmd .Dir = cwd
24
55
out , failed := cmd .CombinedOutput ()
0 commit comments