forked from qur/withmock
/
constraints.go
82 lines (64 loc) · 1.42 KB
/
constraints.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
// Copyright 2013 Julian Phillips. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package lib
import (
"go/build"
"go/ast"
"strings"
)
var (
goos = build.Default.GOOS
goarch = build.Default.GOARCH
)
func goodOSArchConstraints(file *ast.File) (ok bool) {
max := file.Package
for _, comment := range file.Comments {
if comment.Pos() >= max {
break
}
if len(comment.List) == 0 {
continue
}
line := comment.List[0].Text
line = strings.TrimLeft(line, "/")
line = strings.TrimSpace(line)
if !strings.HasPrefix(line, "+build ") {
continue
}
// Loop over lines == AND
for _, cmt := range comment.List {
line := cmt.Text
line = strings.TrimLeft(line, "/")
line = strings.TrimSpace(line)
if len(line) == 0 {
continue
}
line = line[7:]
satisfied := false
// Loop over groups == OR
for _, group := range strings.Split(line, " ") {
gSatisfied := true
// Loop over constraints == AND
for _, constraint := range strings.Split(group, ",") {
if constraint == goos || constraint == goarch {
continue
}
if knownOS[constraint] || knownArch[constraint] {
gSatisfied = false
}
if constraint == "ignore" {
gSatisfied = false
}
}
if gSatisfied {
satisfied = true
}
}
if !satisfied {
return false
}
}
}
return true
}