Exemplo n.º 1
0
// List looks for all dependent packages in the specified Path
func (g *Local) List(pkg string, recursive bool) ([]string, error) {
	searched := `"` + pkg
	if !recursive {
		searched += `"`
	}
	dirs := g.dirs()
	if len(dirs) == 0 {
		return nil, fmt.Errorf("no packages within scope %s", g.Path)
	}
	cmd := exec.Command("find", dirs...)
	cmd.Args = append(cmd.Args,
		strings.Split(fmt.Sprintf(
			"-type f -name *.go -exec grep -F %s -sl {} +",
			searched,
		), " ")...,
	)
	var stdout bytes.Buffer
	var stderr bytes.Buffer
	cmd.Stdout = &stdout
	cmd.Stderr = &stderr
	if err := cmd.Run(); err != nil {
		return nil, fmt.Errorf("%s - %s", err, stderr.String())
	}
	scanner := bufio.NewScanner(&stdout)
	list := deduplist(make(map[string]bool))
	for scanner.Scan() {
		p := strings.TrimPrefix(util.StripGopath(path.Dir(scanner.Text())), "/")
		list.add(p)
	}
	if err := scanner.Err(); err != nil {
		return nil, err
	}
	return list.list(), nil
}
Exemplo n.º 2
0
func importPath(p string) (string, error) {
	var clean string
	if clean = strings.TrimSuffix(p, "/..."); p != clean {
		recursive = true
	}
	cwd, err := os.Getwd()
	if err != nil {
		return "", err
	}
	pkg, err := build.Import(clean, cwd, build.FindOnly)
	if err == nil {
		return pkg.ImportPath, nil
	}
	if !godoc {
		return "", err
	}
	if strings.HasPrefix(clean, ".") {
		clean, _ = filepath.Abs(clean)
	}
	return util.StripGopath(clean), nil
}