...

Source file src/pkg/cmd/go/internal/load/search.go

     1	// Copyright 2017 The Go Authors. All rights reserved.
     2	// Use of this source code is governed by a BSD-style
     3	// license that can be found in the LICENSE file.
     4	
     5	package load
     6	
     7	import (
     8		"path/filepath"
     9		"strings"
    10	
    11		"cmd/go/internal/search"
    12	)
    13	
    14	// MatchPackage(pattern, cwd)(p) reports whether package p matches pattern in the working directory cwd.
    15	func MatchPackage(pattern, cwd string) func(*Package) bool {
    16		switch {
    17		case search.IsRelativePath(pattern):
    18			// Split pattern into leading pattern-free directory path
    19			// (including all . and .. elements) and the final pattern.
    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				// Compute relative path to dir and see if it matches the pattern.
    35				rel, err := filepath.Rel(dir, p.Dir)
    36				if err != nil {
    37					// Cannot make relative - e.g. different drive letters on Windows.
    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