1
2
3
4
5 package load
6
7 import (
8 "path/filepath"
9 "strings"
10
11 "cmd/go/internal/search"
12 )
13
14
15 func MatchPackage(pattern, cwd string) func(*Package) bool {
16 switch {
17 case search.IsRelativePath(pattern):
18
19
20 var dir string
21 i := strings.Index(pattern, "...")
22 if i < 0 {
23 dir, pattern = pattern, ""
24 } else {
25 j := strings.LastIndex(pattern[:i], "/")
26 dir, pattern = pattern[:j], pattern[j+1:]
27 }
28 dir = filepath.Join(cwd, dir)
29 if pattern == "" {
30 return func(p *Package) bool { return p.Dir == dir }
31 }
32 matchPath := search.MatchPattern(pattern)
33 return func(p *Package) bool {
34
35 rel, err := filepath.Rel(dir, p.Dir)
36 if err != nil {
37
38 return false
39 }
40 rel = filepath.ToSlash(rel)
41 if rel == ".." || strings.HasPrefix(rel, "../") {
42 return false
43 }
44 return matchPath(rel)
45 }
46 case pattern == "all":
47 return func(p *Package) bool { return true }
48 case pattern == "std":
49 return func(p *Package) bool { return p.Standard }
50 case pattern == "cmd":
51 return func(p *Package) bool { return p.Standard && strings.HasPrefix(p.ImportPath, "cmd/") }
52 default:
53 matchPath := search.MatchPattern(pattern)
54 return func(p *Package) bool { return matchPath(p.ImportPath) }
55 }
56 }
57
View as plain text