/
please_go_filter.go
45 lines (36 loc) · 1008 Bytes
/
please_go_filter.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
// Package main implements please_go_filter, a tool for filtering go source files.
// This command parses source files and file names to determine whether they
// match the build constraints that you'd usually encounter with `go build`.
package main
import (
"flag"
"fmt"
"go/build"
"os"
"path/filepath"
"strings"
)
var taglist = flag.String("tags", "", "Comma-separated list of extra build tags to apply as build constraints.")
func main() {
flag.Parse()
ctxt := build.Default
ctxt.BuildTags = strings.Split(*taglist, ",")
for _, f := range flag.Args() {
dir, file := filepath.Split(f)
// MatchFile skips _ prefixed files by default, assuming they're editor
// temporary files - but we need to include cgo generated files.
if strings.HasPrefix(file, "_cgo_") {
fmt.Println(f)
continue
}
ok, err := ctxt.MatchFile(dir, file)
if err != nil {
fmt.Fprintf(os.Stderr, "Error checking %v: %v\n", f, err)
os.Exit(1)
}
if ok {
fmt.Println(f)
}
}
os.Exit(0)
}