From 4b818a50d40f7be2266e8cdab00da99121c59f77 Mon Sep 17 00:00:00 2001 From: David Bariod Date: Tue, 10 Nov 2020 14:42:46 +0100 Subject: [PATCH 1/3] migrate cross build target from bash script to mage --- .travis.yml | 2 +- go.mod | 1 + go.sum | 2 ++ mage.go | 10 +++++++++ magefile.go | 60 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 74 insertions(+), 1 deletion(-) create mode 100644 mage.go create mode 100644 magefile.go diff --git a/.travis.yml b/.travis.yml index 996f57ad4..57845ca1a 100644 --- a/.travis.yml +++ b/.travis.yml @@ -9,7 +9,7 @@ os: linux install: - ./travis/install.sh script: - - ./travis/cross_build.sh + - go run mage.go -v crossBuild - ./travis/lint.sh - export GOMAXPROCS=4 - export GORACE=halt_on_error=1 diff --git a/go.mod b/go.mod index b3919d5ea..37004ff34 100644 --- a/go.mod +++ b/go.mod @@ -2,6 +2,7 @@ module github.com/sirupsen/logrus require ( github.com/davecgh/go-spew v1.1.1 // indirect + github.com/magefile/mage v1.10.0 github.com/pmezard/go-difflib v1.0.0 // indirect github.com/stretchr/testify v1.2.2 golang.org/x/sys v0.0.0-20191026070338-33540a1f6037 diff --git a/go.sum b/go.sum index 1edc143be..bce26a188 100644 --- a/go.sum +++ b/go.sum @@ -1,5 +1,7 @@ github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/magefile/mage v1.10.0 h1:3HiXzCUY12kh9bIuyXShaVe529fJfyqoVM42o/uom2g= +github.com/magefile/mage v1.10.0/go.mod h1:z5UZb/iS3GoOSn0JgWuiw7dxlurVYTu+/jHXqQg881A= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/stretchr/testify v1.2.2 h1:bSDNvY7ZPG5RlJ8otE/7V6gMiyenm9RtJ7IUVIAoJ1w= diff --git a/mage.go b/mage.go new file mode 100644 index 000000000..4273031d1 --- /dev/null +++ b/mage.go @@ -0,0 +1,10 @@ +// +build ignore + +package main + +import ( + "github.com/magefile/mage/mage" + "os" +) + +func main() { os.Exit(mage.Main()) } diff --git a/magefile.go b/magefile.go new file mode 100644 index 000000000..78f91032f --- /dev/null +++ b/magefile.go @@ -0,0 +1,60 @@ +// +build mage + +package main + +import ( + "encoding/json" + "fmt" + + "github.com/magefile/mage/mg" + "github.com/magefile/mage/sh" +) + +// GenBuildMatrix regenerates the build matrix from the current version of the go compiler +func getBuildMatrix() (map[string][]string, error) { + jsonData, err := sh.Output("go", "tool", "dist", "list", "-json") + if err != nil { + return nil, err + } + var data []struct { + Goos string + Goarch string + } + if err := json.Unmarshal([]byte(jsonData), &data); err != nil { + return nil, err + } + + matrix := map[string][]string{} + for _, v := range data { + if val, ok := matrix[v.Goos]; ok { + matrix[v.Goos] = append(val, v.Goarch) + } else { + matrix[v.Goos] = []string{v.Goarch} + } + } + + return matrix, nil +} + +func CrossBuild() error { + matrix, err := getBuildMatrix() + if err != nil { + return err + } + + for os, arches := range matrix { + for _, arch := range arches { + env := map[string]string{ + "GOOS": os, + "GOARCH": arch, + } + if mg.Verbose() { + fmt.Printf("Building for GOOS=%s GOARCH=%s\n", os, arch) + } + if err := sh.RunWith(env, "go", "build", "./..."); err != nil { + return err + } + } + } + return nil +} From b02b418f8f5a16480f013bf876ab44a5ee0f2900 Mon Sep 17 00:00:00 2001 From: David Bariod Date: Thu, 17 Dec 2020 15:58:09 +0100 Subject: [PATCH 2/3] migrate lint script to a mage target --- .travis.yml | 2 +- magefile.go | 11 +++++++++++ travis/lint.sh | 7 ------- 3 files changed, 12 insertions(+), 8 deletions(-) delete mode 100755 travis/lint.sh diff --git a/.travis.yml b/.travis.yml index 57845ca1a..1d8a09ff1 100644 --- a/.travis.yml +++ b/.travis.yml @@ -10,7 +10,7 @@ install: - ./travis/install.sh script: - go run mage.go -v crossBuild - - ./travis/lint.sh + - go run mage.go lint - export GOMAXPROCS=4 - export GORACE=halt_on_error=1 - go test -race -v ./... diff --git a/magefile.go b/magefile.go index 78f91032f..02e05de7a 100644 --- a/magefile.go +++ b/magefile.go @@ -5,6 +5,8 @@ package main import ( "encoding/json" "fmt" + "os" + "path" "github.com/magefile/mage/mg" "github.com/magefile/mage/sh" @@ -58,3 +60,12 @@ func CrossBuild() error { } return nil } + +func Lint() error { + gopath := os.Getenv("GOPATH") + if gopath == "" { + return fmt.Errorf("cannot retrieve GOPATH") + } + + return sh.Run(path.Join(gopath, "bin", "golangci-lint"), "run", "./...") +} diff --git a/travis/lint.sh b/travis/lint.sh deleted file mode 100755 index 98fb5bbad..000000000 --- a/travis/lint.sh +++ /dev/null @@ -1,7 +0,0 @@ -#!/bin/bash - -if [[ "$TRAVIS_GO_VERSION" =~ ^1\.15\. ]] && [[ "$TRAVIS_OS_NAME" == "linux" ]] && [[ "$GO111MODULE" == "on" ]]; then - $(go env GOPATH)/bin/golangci-lint run ./... -else - echo "linter has not been run" -fi From 3986c92379760791473e00a46eebda4181a30009 Mon Sep 17 00:00:00 2001 From: David Bariod Date: Sat, 13 Feb 2021 06:45:48 +0100 Subject: [PATCH 3/3] add a test target in the magefile --- .travis.yml | 5 +---- magefile.go | 8 +++++++- 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/.travis.yml b/.travis.yml index 1d8a09ff1..e6ee8b3ab 100644 --- a/.travis.yml +++ b/.travis.yml @@ -11,7 +11,4 @@ install: script: - go run mage.go -v crossBuild - go run mage.go lint - - export GOMAXPROCS=4 - - export GORACE=halt_on_error=1 - - go test -race -v ./... - - if [[ "$TRAVIS_OS_NAME" == "linux" ]]; then go test -race -v -tags appengine ./... ; fi + - go run mage.go test diff --git a/magefile.go b/magefile.go index 02e05de7a..9aa603939 100644 --- a/magefile.go +++ b/magefile.go @@ -12,7 +12,7 @@ import ( "github.com/magefile/mage/sh" ) -// GenBuildMatrix regenerates the build matrix from the current version of the go compiler +// getBuildMatrix returns the build matrix from the current version of the go compiler func getBuildMatrix() (map[string][]string, error) { jsonData, err := sh.Output("go", "tool", "dist", "list", "-json") if err != nil { @@ -69,3 +69,9 @@ func Lint() error { return sh.Run(path.Join(gopath, "bin", "golangci-lint"), "run", "./...") } + +// Run the test suite +func Test() error { + return sh.RunWith(map[string]string{"GORACE": "halt_on_error=1"}, + "go", "test", "-race", "-v", "./...") +}