1// +build mage 2 3package main 4 5import ( 6 "encoding/json" 7 "fmt" 8 "os" 9 "path" 10 11 "github.com/magefile/mage/mg" 12 "github.com/magefile/mage/sh" 13) 14 15// getBuildMatrix returns the build matrix from the current version of the go compiler 16func getBuildMatrix() (map[string][]string, error) { 17 jsonData, err := sh.Output("go", "tool", "dist", "list", "-json") 18 if err != nil { 19 return nil, err 20 } 21 var data []struct { 22 Goos string 23 Goarch string 24 } 25 if err := json.Unmarshal([]byte(jsonData), &data); err != nil { 26 return nil, err 27 } 28 29 matrix := map[string][]string{} 30 for _, v := range data { 31 if val, ok := matrix[v.Goos]; ok { 32 matrix[v.Goos] = append(val, v.Goarch) 33 } else { 34 matrix[v.Goos] = []string{v.Goarch} 35 } 36 } 37 38 return matrix, nil 39} 40 41func CrossBuild() error { 42 matrix, err := getBuildMatrix() 43 if err != nil { 44 return err 45 } 46 47 for os, arches := range matrix { 48 for _, arch := range arches { 49 env := map[string]string{ 50 "GOOS": os, 51 "GOARCH": arch, 52 } 53 if mg.Verbose() { 54 fmt.Printf("Building for GOOS=%s GOARCH=%s\n", os, arch) 55 } 56 if err := sh.RunWith(env, "go", "build", "./..."); err != nil { 57 return err 58 } 59 } 60 } 61 return nil 62} 63 64func Lint() error { 65 gopath := os.Getenv("GOPATH") 66 if gopath == "" { 67 return fmt.Errorf("cannot retrieve GOPATH") 68 } 69 70 return sh.Run(path.Join(gopath, "bin", "golangci-lint"), "run", "./...") 71} 72 73// Run the test suite 74func Test() error { 75 return sh.RunWith(map[string]string{"GORACE": "halt_on_error=1"}, 76 "go", "test", "-race", "-v", "./...") 77} 78