-
Notifications
You must be signed in to change notification settings - Fork 0
/
go.go
52 lines (45 loc) · 1.28 KB
/
go.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
package internal
import (
"go/parser"
"go/token"
"path"
"github.com/CircleCI-Public/circleci-config/labeling/codebase"
"github.com/CircleCI-Public/circleci-config/labeling/labels"
)
var GoRules = []labels.Rule{
func(c codebase.Codebase, ls labels.LabelSet) (label labels.Label, err error) {
label.Key = labels.DepsGo
goModPath, err := c.FindFile("go.mod")
label.Valid = goModPath != ""
label.BasePath = path.Dir(goModPath)
lockFilePath, _ := c.FindFile("go.sum")
label.LabelData.HasLockFile = lockFilePath != ""
return label, err
},
func(c codebase.Codebase, ls labels.LabelSet) (label labels.Label, err error) {
label.Key = labels.ArtifactGoExecutable
if !ls[labels.DepsGo].Valid {
return label, err
}
label.Valid = containsMainGoFiles(c)
return label, err
},
}
func containsMainGoFiles(c codebase.Codebase) bool {
_, err := c.FindFileMatching(
func(path string) bool {
return isMainPackageGoFile(c, path)
},
"*.go",
)
return err == nil
}
func isMainPackageGoFile(c codebase.Codebase, path string) bool {
contents, innerErr := c.ReadFile(path)
if innerErr != nil {
return false
}
fileSet := token.NewFileSet()
fileAst, err := parser.ParseFile(fileSet, "", contents, parser.PackageClauseOnly)
return err == nil && fileAst.Name.Name == "main"
}