-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathiteratePackages.go
47 lines (39 loc) · 1.2 KB
/
iteratePackages.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
package parser
import (
"golang.org/x/tools/go/packages"
)
type packagesIterator = func(pkg *packages.Package, logger LoggerCLI) error
// iteratePackages will iterate over the parsed packages.
//
// Note that if the focus is set to packagePath, it will iterate only over
// the specified package
func (p *GoParser) iteratePackages(callback packagesIterator, optionalLogger ...LoggerCLI) error {
logger := p.logger
if len(optionalLogger) != 0 {
logger = optionalLogger[0]
}
if len(p.pkgs) == 0 {
logger.Trace("There are no packages to iterate...")
return nil
}
logger = logger.Trace("Iterating over packages...")
for _, currPkg := range p.pkgs {
currLogger := logger.Trace("Analysing *packages.Package '%s %s'...", currPkg.Name, currPkg.PkgPath)
if len(currPkg.Errors) != 0 {
errorsLog := currLogger.Error("Package '%s %s' contain errors. Skipping it...", currPkg.Name, currPkg.PkgPath)
for _, currError := range currPkg.Errors {
errorsLog.Error(currError.Error())
}
continue
}
if !p.focus.is(focusPackagePath, currPkg.PkgPath) {
currLogger.Trace("Skipped (not the focus)...")
continue
}
e := callback(currPkg, currLogger)
if e != nil {
return e
}
}
return nil
}