Skip to content

Commit

Permalink
dev-tools/mage: use "go list -m" to locate beats (#16445)
Browse files Browse the repository at this point in the history
* dev-tools/mage: use "go list -m" to locate beats

Use the "go list" tool to locate the root directory of the
elastic/beats module. This removes some assumptions about
the use of vendoring, and hard-coded knowledge of the apm-server
directory structure.

* Account for versioned import paths
  • Loading branch information
axw authored and kvch committed Feb 27, 2020
1 parent 0d74ae5 commit e0f7506
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 38 deletions.
5 changes: 1 addition & 4 deletions dev-tools/mage/gomod.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,13 +80,10 @@ func Vendor() error {
if err != nil {
return err
}
if len(path) != 1 {
return fmt.Errorf("unexpected number of paths")
}
fmt.Println(path)

for _, f := range p.filesToCopy {
from := filepath.Join(path[0], f)
from := filepath.Join(path, f)
to := filepath.Join(vendorFolder, p.name, f)
copyTask := &CopyTask{Source: from, Dest: to, DirMode: os.ModeDir | 0750}
err = copyTask.Execute()
Expand Down
14 changes: 10 additions & 4 deletions dev-tools/mage/gotool/go.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,14 +94,20 @@ func ListTestFiles(pkg string) ([]string, error) {
}

// ListModulePath returns the path to the module in the cache.
func ListModulePath(pkg string) ([]string, error) {
func ListModulePath(pkg string) (string, error) {
const tmpl = `{{.Dir}}`

// make sure to look in the module cache
env := map[string]string{
// make sure to look in the module cache
"GOFLAGS": "",
}
return getLines(callGo(env, "list", "-f", tmpl, pkg))
lines, err := getLines(callGo(env, "list", "-m", "-f", tmpl, pkg))
if err != nil {
return "", err
}
if n := len(lines); n != 1 {
return "", fmt.Errorf("expected 1 line, got %d", n)
}
return lines[0], nil
}

// HasTests returns true if the given package contains test files.
Expand Down
49 changes: 19 additions & 30 deletions dev-tools/mage/settings.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@ import (
"io/ioutil"
"log"
"os"
"path"
"path/filepath"
"reflect"
"regexp"
"strconv"
"strings"
Expand Down Expand Up @@ -262,39 +264,26 @@ func ElasticBeatsDir() (string, error) {
return elasticBeatsDirValue, elasticBeatsDirErr
}

// findElasticBeatsDir attempts to find the root of the Elastic Beats directory.
// It checks to see if the current project is elastic/beats, and then if not
// checks the vendor directory.
// findElasticBeatsDir returns the root directory of the Elastic Beats module, using "go list".
//
// If your project places the Beats files in a different location (specifically
// the dev-tools/ contents) then you can use SetElasticBeatsDir().
// When running within the Elastic Beats repo, this will return the repo root. Otherwise,
// it will return the root directory of the module from within the module cache or vendor
// directory.
func findElasticBeatsDir() (string, error) {
repo, err := GetProjectRepoInfo()
if err != nil {
return "", err
}

if repo.IsElasticBeats() {
return repo.RootDir, nil
}

const devToolsImportPath = elasticBeatsImportPath + "/dev-tools/mage"

// Search in project vendor directories. Order is relevant
searchPaths := []string{
// beats directory of apm-server
filepath.Join(repo.RootDir, "_beats/dev-tools/vendor"),
filepath.Join(repo.RootDir, repo.SubDir, "vendor", devToolsImportPath),
filepath.Join(repo.RootDir, "vendor", devToolsImportPath),
}

for _, path := range searchPaths {
if _, err := os.Stat(path); err == nil {
return filepath.Join(path, "../.."), nil
// Find the import path for the package containing this file.
type foo struct{}
typ := reflect.TypeOf(foo{})
magepkgpath := typ.PkgPath()

// Walk up the import path until we find the elastic/beats module path.
pkgpath := magepkgpath
for extractCanonicalRootImportPath(pkgpath) != elasticBeatsImportPath {
pkgpath = path.Dir(pkgpath)
if pkgpath == "." {
return "", errors.Errorf("failed to find %q from %q", elasticBeatsImportPath, magepkgpath)
}
}

return "", errors.Errorf("failed to find %v in the project's vendor", devToolsImportPath)
return gotool.ListModulePath(pkgpath)
}

var (
Expand Down Expand Up @@ -552,7 +541,7 @@ type ProjectRepoInfo struct {
// IsElasticBeats returns true if the current project is
// github.com/elastic/beats.
func (r *ProjectRepoInfo) IsElasticBeats() bool {
return strings.HasPrefix(r.RootImportPath, elasticBeatsImportPath)
return r.CanonicalRootImportPath == elasticBeatsImportPath
}

var (
Expand Down

0 comments on commit e0f7506

Please sign in to comment.