-
Notifications
You must be signed in to change notification settings - Fork 1
/
gofile.go
110 lines (95 loc) · 2.09 KB
/
gofile.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
package depgraph
import (
"bufio"
"go/build/constraint"
"os"
"path/filepath"
"strings"
"github.com/luno/jettison/errors"
"github.com/luno/jettison/j"
)
// listGoFiles returns a slice of go files in the folder ignoring _test.go files.
func listGoFiles(dir string) ([]string, error) {
files, err := filepath.Glob(filepath.Join(dir, "*.go"))
if err != nil {
return nil, err
}
var res []string
for _, f := range files {
if strings.Contains(f, "_test.go") {
continue
}
res = append(res, f)
}
return res, nil
}
// getGoImports returns the imports of the go file and true if it matches the build tags.
//
// It only scans the head of the file.
func getGoImports(file string, tags map[string]bool) ([]string, bool, error) {
f, err := os.Open(file)
if err != nil {
return nil, false, err
}
defer f.Close()
scanner := bufio.NewScanner(f)
var (
res []string
imports bool
singleImport bool
seenPkg bool
)
for scanner.Scan() {
line := scanner.Text()
if !seenPkg && strings.HasPrefix(line, "//go:build") {
c, err := constraint.Parse(line)
if err != nil {
return nil, false, errors.Wrap(err, "", j.KS("file", file))
}
if !c.Eval(func(tag string) bool {
return tags[tag]
}) {
return nil, false, nil
}
} else if strings.HasPrefix(line, "package ") {
seenPkg = true
}
if line == "import (" {
imports = true
continue
} else if strings.HasPrefix(line, "import \"") {
imports = true
singleImport = true
}
if !imports {
continue
}
if line == ")" {
break
}
line = strings.TrimSpace(line)
if line == "" {
continue
}
// Handle line comments
if strings.HasPrefix(line, "//") {
continue
}
// Handle block comments (only the simple case)
if strings.HasPrefix(line, "/*") && strings.HasSuffix(line, "*/") {
continue
}
if strings.Contains(line, " \"") {
// Import aliased
line = strings.Split(line, " ")[1]
}
res = append(res, strings.Trim(line, "\""))
if singleImport {
break
}
}
if err := scanner.Err(); err != nil {
return nil, false, err
}
return res, true, nil
}