Skip to content

Commit

Permalink
Address review comments
Browse files Browse the repository at this point in the history
Rather than having arguments for ListModulePath,
create separate functions for listing in the module
cache and vendor directory. Create an unexported
function in dev-tools/mage which calls the appropriate
one depending on the value of UseVendor.
  • Loading branch information
axw committed Mar 6, 2020
1 parent 3bd725d commit 95f21ec
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 27 deletions.
12 changes: 12 additions & 0 deletions dev-tools/mage/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ import (
"github.com/magefile/mage/sh"
"github.com/magefile/mage/target"
"github.com/pkg/errors"

"github.com/elastic/beats/v7/dev-tools/mage/gotool"
)

// Expand expands the given Go text/template string.
Expand Down Expand Up @@ -791,3 +793,13 @@ func ParseVersion(version string) (major, minor, patch int, err error) {
patch, _ = strconv.Atoi(data["patch"])
return
}

// listModuleDir calls gotool.ListModuleVendorDir or
// gotool.ListModuleCacheDir, depending on the value of
// UseVendor.
func listModuleDir(modpath string) (string, error) {
if UseVendor {
return gotool.ListModuleVendorDir(modpath)
}
return gotool.ListModuleCacheDir(modpath)
}
14 changes: 3 additions & 11 deletions dev-tools/mage/godaemon.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@ import (
"log"
"os"
"path/filepath"

"github.com/elastic/beats/v7/dev-tools/mage/gotool"
)

var (
Expand All @@ -44,19 +42,13 @@ func BuildGoDaemon() error {
"only be executed within the golang-crossbuild docker environment.")
}

// Locate tsg/go-daemon.
var args []string
if UseVendor {
args = append(args, "-mod=vendor")
}
godaemonDir, err := gotool.ListModulePath("github.com/tsg/go-daemon", args...)
// Test if binaries are up-to-date.
godaemonDir, err := listModuleDir("github.com/tsg/go-daemon")
if err != nil {
return err
}

// Test if binaries are up-to-date.
output := MustExpand("build/golang-crossbuild/god-{{.Platform.GOOS}}-{{.Platform.Arch}}")
input := filepath.Join(godaemonDir, "src", "god.c")
output := MustExpand("build/golang-crossbuild/god-{{.Platform.GOOS}}-{{.Platform.Arch}}")
if IsUpToDate(output, input) {
log.Println(">>> buildGoDaemon is up-to-date for", Platform.Name)
return nil
Expand Down
2 changes: 1 addition & 1 deletion dev-tools/mage/gomod.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ func Vendor() error {

// copy packages which require the whole tree
for _, p := range copyAll {
path, err := gotool.ListModulePath(p.name)
path, err := gotool.ListModuleVendorDir(p.name)
if err != nil {
return err
}
Expand Down
29 changes: 20 additions & 9 deletions dev-tools/mage/gotool/go.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,18 +93,29 @@ func ListTestFiles(pkg string) ([]string, error) {
return getLines(callGo(nil, "list", "-f", tmpl, pkg))
}

// ListModulePath returns the path to the specified module.
//
// Additional parameters like "-mod=vendor" may be passed
// to control behaviour.
func ListModulePath(pkg string, args ...string) (string, error) {
// ListModuleCacheDir returns the module cache directory containing
// the specified module. If the module does not exist in the cache,
// an error will be returned.
func ListModuleCacheDir(pkg string) (string, error) {
return listModuleDir(pkg, false)
}

// ListModuleVendorDir returns the vendor directory containing the
// specified module. If the module has not been vendored, an error
// will be returned.
func ListModuleVendorDir(pkg string) (string, error) {
return listModuleDir(pkg, true)
}

func listModuleDir(pkg string, vendor bool) (string, error) {
env := map[string]string{
// make sure to look in the module cache,
// unless otherwise configured in args.
// Make sure GOFLAGS does not influence behaviour.
"GOFLAGS": "",
}
args = append([]string{"-m", "-f", "{{.Dir}}"}, args...)
args = append(args, pkg)
args := []string{"-m", "-f", "{{.Dir}}", pkg}
if vendor {
args = append(args, "-mod=vendor")
}
lines, err := getLines(callGo(env, "list", args...))
if err != nil {
return "", err
Expand Down
6 changes: 1 addition & 5 deletions dev-tools/mage/settings.go
Original file line number Diff line number Diff line change
Expand Up @@ -284,11 +284,7 @@ func findElasticBeatsDir() (string, error) {
if repo.IsElasticBeats() {
return repo.RootDir, nil
}
var args []string
if UseVendor {
args = append(args, "-mod=vendor")
}
return gotool.ListModulePath("github.com/elastic/beats/v7")
return listModuleDir("github.com/elastic/beats/v7")
}

var (
Expand Down
2 changes: 1 addition & 1 deletion generator/common/beatgen/setup/setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ func CopyVendor() error {
return err
}

path, err := gotool.ListModulePath("github.com/elastic/beats/v7")
path, err := gotool.ListModuleCacheDir("github.com/elastic/beats/v7")
if err != nil {
return err
}
Expand Down

0 comments on commit 95f21ec

Please sign in to comment.