|
| 1 | +//go:build mage |
| 2 | + |
| 3 | +package main |
| 4 | + |
| 5 | +import ( |
| 6 | + "context" |
| 7 | + "fmt" |
| 8 | + "log" |
| 9 | + "os" |
| 10 | + "runtime" |
| 11 | + |
| 12 | + "github.com/magefile/mage/mg" |
| 13 | + "github.com/rancherlabs/corral/magetools" |
| 14 | +) |
| 15 | + |
| 16 | +var Default = Build |
| 17 | +var g *magetools.Go |
| 18 | +var version string |
| 19 | +var commit string |
| 20 | + |
| 21 | +func Setup() error { |
| 22 | + var err error |
| 23 | + version, err = magetools.GetVersion() |
| 24 | + if err != nil { |
| 25 | + return err |
| 26 | + } |
| 27 | + commit, err = magetools.GetCommit() |
| 28 | + if err != nil { |
| 29 | + return err |
| 30 | + } |
| 31 | + g = magetools.NewGo(goarch(), goos(), version, commit, false, true) |
| 32 | + return nil |
| 33 | +} |
| 34 | + |
| 35 | +func Dependencies() error { |
| 36 | + mg.Deps(Setup) |
| 37 | + return g.Mod().Download() |
| 38 | +} |
| 39 | + |
| 40 | +func Build(ctx context.Context) error { |
| 41 | + mg.Deps(Dependencies) |
| 42 | + return g.Build("main.go", fmt.Sprintf("dist/corral-%s-%s", g.OS, g.Arch)) |
| 43 | +} |
| 44 | + |
| 45 | +func Validate() error { |
| 46 | + mg.Deps(Setup) |
| 47 | + log.Println("[Validate] Running: golangci-lint") |
| 48 | + if err := g.Lint(); err != nil { |
| 49 | + return err |
| 50 | + } |
| 51 | + |
| 52 | + log.Println("[Validate] Running: go fmt") |
| 53 | + if err := g.Fmt("./..."); err != nil { |
| 54 | + return err |
| 55 | + } |
| 56 | + |
| 57 | + log.Println("[Validate] Running: go mod tidy") |
| 58 | + if err := g.Mod().Tidy(); err != nil { |
| 59 | + return err |
| 60 | + } |
| 61 | + |
| 62 | + log.Println("[Validate] Running: go mod verify") |
| 63 | + if err := g.Mod().Verify(); err != nil { |
| 64 | + return err |
| 65 | + } |
| 66 | + |
| 67 | + log.Println("[Validate] Checking for dirty repo") |
| 68 | + if err := magetools.IsGitClean(); err != nil { |
| 69 | + return err |
| 70 | + } |
| 71 | + |
| 72 | + log.Println("[Validate] corral has been successfully validated") |
| 73 | + return nil |
| 74 | +} |
| 75 | + |
| 76 | +func Test() error { |
| 77 | + mg.Deps(Setup) |
| 78 | + log.Println("[Test] Running unit tests") |
| 79 | + if err := g.Test("", "./cmd/...", "./pkg/..."); err != nil { |
| 80 | + return err |
| 81 | + } |
| 82 | + log.Println("[Test] Running integration tests") |
| 83 | + if err := g.Test("./...", "./tests/..."); err != nil { |
| 84 | + return err |
| 85 | + } |
| 86 | + log.Println("[Test] corral has been successfully tested") |
| 87 | + return nil |
| 88 | +} |
| 89 | + |
| 90 | +func goos() string { |
| 91 | + if goos := os.Getenv("GOOS"); goos != "" { |
| 92 | + return goos |
| 93 | + } |
| 94 | + return runtime.GOOS |
| 95 | +} |
| 96 | + |
| 97 | +func goarch() string { |
| 98 | + if goarch := os.Getenv("GOARCH"); goarch != "" { |
| 99 | + return goarch |
| 100 | + } |
| 101 | + return runtime.GOARCH |
| 102 | +} |
0 commit comments