This repository has been archived by the owner on Oct 16, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmagefile.go
120 lines (98 loc) · 2.56 KB
/
magefile.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
// +build mage
package main
import (
"fmt"
"os"
"time"
"github.com/magefile/mage/sh"
)
const (
entryPoint = "cmd/lbs.go"
ldFlags = "-X $PACKAGE/version/version.commitHash=$COMMIT_HASH " +
"-X $PACKAGE/version/version.buildDate=$BUILD_DATE"
protoPlugins = "plugins=grpc"
protoDir = "pb"
protoFileName = "bitcoin/service.proto"
protoArtifactModule = "github.com/ledgerhq/bitcoin-lib-grpc/pb"
)
// Allow user to override executables on UNIX-like systems.
var goexe = "go" // GOEXE=xxx mage build
var protoc = "protoc" // PROTOC=xxx mage proto
var buf = "buf" // BUF=xxx mage protolist
func init() {
if exe := os.Getenv("GOEXE"); exe != "" {
goexe = exe
}
if exe := os.Getenv("PROTOC"); exe != "" {
protoc = exe
}
if exe := os.Getenv("BUF"); exe != "" {
buf = exe
}
// We want to use Go 1.11 modules even if the source lives inside GOPATH.
// The default is "auto".
os.Setenv("GO111MODULE", "on")
}
func Proto() error {
return sh.Run(protoc,
fmt.Sprintf("--go_out=%s:%s", protoPlugins, protoDir), // protoc flags
fmt.Sprintf("%s/%s", protoDir, protoFileName), // input .proto
fmt.Sprintf("--go_opt=module=%s", protoArtifactModule), // module output
)
}
func Buf() error {
// Verify if the proto files can be compiled.
if err := sh.Run(buf, "image", "build", "-o /dev/null"); err != nil {
return err
}
// Run Buf lint checks on the protobuf files.
if err := sh.Run(buf, "check", "lint"); err != nil {
return err
}
return nil
}
// Build binary
func Build() error {
if err := Proto(); err != nil {
return err
}
return sh.RunWith(flagEnv(), goexe, "build", "-ldflags", ldFlags,
entryPoint)
}
// Run tests
func Test() error {
return sh.Run(goexe, "test", "./...")
}
// Run tests with race detector
func TestRace() error {
return sh.Run(goexe, "test", "-race", "./...")
}
// Run tests with race-detector and code-coverage.
// Useful on CI, but can be run locally too.
func TestRaceCover() error {
return sh.Run(
goexe, "test", "-race", "-coverprofile=coverage.txt",
"-covermode=atomic", "./...")
}
// Run basic golangci-lint check.
func Lint() error {
linterArgs := []string{
"run",
"--disable-all",
"--enable=govet",
"--enable=gofmt",
"--enable=gosec",
}
if err := sh.Run("golangci-lint", linterArgs...); err != nil {
return err
}
return nil
}
func flagEnv() map[string]string {
hash, _ := sh.Output("git", "rev-parse", "--short", "HEAD")
return map[string]string{
"PACKAGE": entryPoint,
"COMMIT_HASH": hash,
"BUILD_DATE": time.Now().Format("2006-01-02T15:04:05Z0700"),
}
}