This repository has been archived by the owner on Mar 6, 2020. It is now read-only.
/
resolve.go
60 lines (53 loc) · 1.67 KB
/
resolve.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
package cmd
import (
"fmt"
"go/build"
"github.com/constabulary/gb"
"github.com/constabulary/gb/log"
)
// Resolver resolves packages.
type Resolver interface {
// Srcdirs returns []string{ "$PROJECT/src", "$PROJECT/vendor/src" }
Srcdirs() []string
// ResolvePackage resolves the import path to a *gb.Package
ResolvePackage(path string) (*gb.Package, error)
// ResolvePackagesWithTests is similar to ResolvePackages however
// it also loads the test and external test packages of args into
// the context.
ResolvePackageWithTests(path string) (*gb.Package, error)
}
// ResolvePackages resolves import paths to packages.
func ResolvePackages(r Resolver, paths ...string) ([]*gb.Package, error) {
var pkgs []*gb.Package
for _, path := range paths {
if path == "." {
return nil, fmt.Errorf("%q is not a package", r.Srcdirs()[0])
}
path = relImportPath(r.Srcdirs()[0], path)
pkg, err := r.ResolvePackage(path)
if err != nil {
return pkgs, fmt.Errorf("failed to resolve import path %q: %v", path, err)
}
pkgs = append(pkgs, pkg)
}
return pkgs, nil
}
// ResolvePackagesWithTests is similar to ResolvePackages however
// it also loads the test and external test packages of args into
// the context.
func ResolvePackagesWithTests(r Resolver, paths ...string) ([]*gb.Package, error) {
var pkgs []*gb.Package
for _, path := range paths {
path = relImportPath(r.Srcdirs()[0], path)
pkg, err := r.ResolvePackageWithTests(path)
if err != nil {
if _, ok := err.(*build.NoGoError); ok {
log.Debugf("skipping %q", path)
continue
}
return pkgs, fmt.Errorf("failed to resolve package %q: %v", path, err)
}
pkgs = append(pkgs, pkg)
}
return pkgs, nil
}