-
Notifications
You must be signed in to change notification settings - Fork 6
/
generate.go
119 lines (88 loc) · 2.41 KB
/
generate.go
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
package main
import (
"flag"
"fmt"
"log"
"os"
"path/filepath"
"regexp"
)
const (
gopath = "$GOPATH"
runArg = "run=%s"
)
var (
flagDir = flag.String("i", gopath, "Directory to recurse through, default "+gopath)
flagRun = flag.String("run", "", "if non-empty, specifies a regular expression to select directives whose full original source text (excluding any trailing spaces and final newline) matches the expression")
flagMatch = flag.String("match", "", "Regexp for directories to run the command for")
flagIgnore = flag.String("ignore", "", "Regexp for dirs we should ignore")
flagPrintProcessed = flag.Bool("v", false, "flag prints the names of packages and files as they are")
flagPrintSimulate = flag.Bool("n", false, "flag prints commands that would be executed")
flagPrintExecute = flag.Bool("x", false, "flag prints commands as they are executed")
v, n, x, run string
matchRegexp *regexp.Regexp
ignoreRegexp *regexp.Regexp
)
func main() {
parseFlags()
generate()
}
func parseFlags() {
flag.Parse()
if len(*flagDir) == 0 {
panic("**invalid Input Directory")
}
s := filepath.Clean(os.ExpandEnv(*flagDir))
flagDir = &s
if *flagDir == "." {
panic("**invalid Input Directoy '" + *flagDir + "'")
}
if len(*flagMatch) > 0 {
var err error
matchRegexp, err = regexp.Compile(*flagMatch)
if err != nil {
panic("**Error Compiling match Regex:" + err.Error())
}
}
if len(*flagIgnore) > 0 {
var err error
ignoreRegexp, err = regexp.Compile(*flagIgnore)
if err != nil {
panic("**Error Compiling ignore Regex:" + err.Error())
}
}
if *flagPrintExecute {
x = "-x"
}
if *flagPrintSimulate {
n = "-n"
}
if *flagPrintProcessed {
v = "-v"
}
if len(*flagRun) > 0 {
run = fmt.Sprintf(runArg, *flagRun)
}
}
func generate() {
walker := func(path string, info os.FileInfo, err error) error {
if info == nil || !info.IsDir() {
return nil
}
if matchRegexp != nil && !matchRegexp.MatchString(path) {
return nil
}
if ignoreRegexp != nil && ignoreRegexp.MatchString(path) {
return nil
}
fmt.Println("Processing:", path)
if err := os.Chdir(path); err != nil {
log.Fatalf("\n**error changing DIR '%s'\n%s\n", path, err)
}
executeCmd("go", "generate", x, v, n, run)
return nil
}
if err := filepath.Walk(*flagDir, walker); err != nil {
log.Fatalf("\n**could not walk project path '%s'\n%s\n", *flagDir, err)
}
}