/
packages.go
188 lines (160 loc) · 4.13 KB
/
packages.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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
package structure
import (
"fmt"
"io"
"path"
"strings"
log "github.com/sirupsen/logrus"
"github.com/spf13/viper"
)
type PackageType string
const (
// External packages are external dependencies
External PackageType = "external"
// Common packages contain shared utilities and can import only External packages
Common PackageType = "common"
// Service packages are deployable services and can import Common and External packages
Service PackageType = "service"
// Utility packages can import whatever they want
Utility PackageType = "utility"
)
type PackageInfo struct {
// path is the path that can be used to run tests on a specific package.
path string
// name is the friendly name of a package.
name string
typ PackageType
}
var RootPkg = PackageInfo{
path: ".",
name: "project root",
typ: Common,
}
func (p PackageInfo) String() string {
return fmt.Sprintf("%s [%s]", p.name, p.typ)
}
func (p PackageInfo) Path() string {
return fmt.Sprintf("%s/...", p.path)
}
func (p PackageInfo) Name() string {
return p.name
}
func (p PackageInfo) Type() PackageType {
return p.typ
}
type baseStruct struct {
base string
rootDir string
external []string
utility []string
common []string
service []string
}
type BaseStruct interface {
GetPackageInfo(pkgPath string) PackageInfo
BuildPackageTree(path string) ImportTree
GetChangedPackages(r io.Reader) PackageSet
}
func NewBaseStruct() BaseStruct {
res := baseStruct{
base: viper.GetString("module_name"),
rootDir: viper.GetString("root_dir"),
external: viper.GetStringSlice("folders.external"),
utility: viper.GetStringSlice("folders.utility"),
common: viper.GetStringSlice("folders.common"),
service: viper.GetStringSlice("folders.service"),
}
return &res
}
func (s *baseStruct) match(pkgPath string, relPath string, typ PackageType) (match bool, info PackageInfo) {
prefix := strings.Join([]string{s.base, relPath}, "/")
hasWildcard := strings.HasSuffix(prefix, "*")
if hasWildcard {
prefix = strings.TrimSuffix(prefix, "*")
}
if strings.HasPrefix(pkgPath, prefix) {
var res PackageInfo
if hasWildcard {
name := strings.Split(strings.Replace(pkgPath, prefix, "", 1), "/")[0]
res = PackageInfo{
path: path.Join(prefix, name),
name: name,
typ: typ,
}
} else {
res = PackageInfo{
path: prefix,
name: relPath,
typ: typ,
}
}
log.Debugf("path %s belongs to package %s of type %s since it matches prefix %s", pkgPath, res, typ, prefix)
return true, res
}
return false, PackageInfo{}
}
func (s *baseStruct) GetPackageInfo(pkgPath string) PackageInfo {
if !strings.HasPrefix(pkgPath, s.base) {
return PackageInfo{
path: pkgPath,
name: pkgPath,
typ: External,
}
}
for _, path := range s.utility {
if ok, match := s.match(pkgPath, path, Utility); ok {
return match
}
}
for _, path := range s.external {
if ok, match := s.match(pkgPath, path, External); ok {
return match
}
}
for _, path := range s.common {
if ok, match := s.match(pkgPath, path, Common); ok {
return match
}
}
for _, path := range s.service {
if ok, match := s.match(pkgPath, path, Service); ok {
return match
}
}
return PackageInfo{
path: pkgPath,
name: pkgPath,
typ: External,
}
}
func (p PackageInfo) CanImport(o PackageInfo) bool {
if p.typ == External {
log.Tracef("skipping external package %s", p)
return true
}
if o.typ == External {
log.Tracef("package %s can import package %s: imported package is external", p, o)
return true
}
if p.typ == Utility {
log.Debugf("package %s can import package %s: package is utility", p, o)
return true
}
if o.typ == Utility {
log.Errorf("package %s cannot import package %s: no package can import utilities", p, o)
return false
}
if o.typ == Common {
log.Debugf("package %s can import package %s: imported package is common", p, o)
return true
}
if p.typ == Common {
log.Errorf("package %s cannot import package %s: common package cannot import service", p, o)
return false
}
if p.path != o.path {
log.Errorf("package %s cannot import package %s: packages belong to different services", p, o)
return false
}
return true
}