This repository has been archived by the owner on Jul 5, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 154
/
magefile.go
191 lines (154 loc) · 4.14 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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.
//go:build mage
// +build mage
package main
import (
"errors"
"fmt"
"os"
"strings"
"github.com/magefile/mage/mg"
"github.com/magefile/mage/sh"
)
var Default = Build
var GOPATH = os.Getenv("GOPATH")
var Aliases = map[string]interface{}{
"test": Test.Unit,
}
type Test mg.Namespace
func gopath(path string) string {
return GOPATH + path
}
func getPackages() ([]string, error) {
out, err := sh.Output("go", "list", "./...")
if err != nil {
return nil, err
}
return strings.Split(out, "\n"), nil
}
func getPackageFiles(name string) ([]string, error) {
out, err := sh.Output("go", "list", "-f", "{{range .GoFiles}}{{$.Dir}}/{{.}} {{end}}", name)
if err != nil {
return nil, err
}
return strings.Split(strings.Trim(out, " "), " "), nil
}
// Builds the mmctl binary
func Build() {
mg.SerialDeps(Vendor, Check)
sh.RunV("go", "build", "-mod=vendor")
}
// Installs the mmctl binary in the GOPATH
func Install() {
mg.SerialDeps(Vendor, Check)
sh.RunV("go", "install", "-mod=vendor")
}
// Packages mmctl in the build directory
func Package() error {
mg.SerialDeps(Vendor, Check)
sh.RunV("mkdir", "-p", "build")
fmt.Println("Building Linux amd64")
if err := sh.RunWith(map[string]string{"GOOS": "linux", "GOARCH": "amd64"}, "go", "build", "-mod=vendor"); err != nil {
return err
}
sh.RunV("tar", "cf", "build/linux_amd64.tar", "mmctl")
fmt.Println("Building OSX amd64")
if err := sh.RunWith(map[string]string{"GOOS": "darwin", "GOARCH": "amd64"}, "go", "build", "-mod=vendor"); err != nil {
return err
}
sh.RunV("tar", "cf", "build/darwin_amd64.tar", "mmctl")
fmt.Println("Build Windows amd64")
if err := sh.RunWith(map[string]string{"GOOS": "windows", "GOARCH": "amd64"}, "go", "build", "-mod=vendor"); err != nil {
return err
}
sh.RunV("zip", "build/windows_amd64.zip", "mmctl.exe")
sh.RunV("rm", "mmctl", "mmctl.exe")
return nil
}
// Runs gofmt through the mmctl codebase
func Gofmt() error {
fmt.Println("Running gofmt")
packages, err := getPackages()
if err != nil {
return err
}
for _, p := range packages {
fmt.Printf("Checking %s\n", p)
files, err := getPackageFiles(p)
if err != nil {
return err
}
if len(files) > 0 {
args := append([]string{"-d", "-s"}, files...)
out, err := sh.Output("gofmt", args...)
if err != nil {
return err
}
if out != "" {
fmt.Println(out)
return errors.New("Gofmt failure")
}
}
}
fmt.Println("Gofmt success")
return nil
}
// Runs govet through the mmctl codebase
func Govet() error {
fmt.Println("Running govet")
packages, err := getPackages()
if err != nil {
return err
}
if err := sh.RunWith(map[string]string{"GO111MODULE": "off"}, "go", "get", "golang.org/x/tools/go/analysis/passes/shadow/cmd/shadow"); err != nil {
return err
}
args := append([]string{"vet"}, packages...)
sh.RunV("go", args...)
args = append([]string{"vet", "-vettool=" + gopath("/bin/shadow")}, packages...)
sh.RunV("go", args...)
fmt.Println("Govet success")
return nil
}
// Runs the unit test suite (Alias: test)
func (Test) Unit() error {
fmt.Println("Running unit tests")
packages, err := getPackages()
if err != nil {
return err
}
args := append([]string{"test", "-mod=vendor", "-race", "-v", "-count", "1", "-tags", "unit"}, packages...)
sh.RunV("go", args...)
return nil
}
// Runs e2e the test suite
func (Test) E2e() error {
fmt.Println("Running e2e tests")
packages, err := getPackages()
if err != nil {
return err
}
args := append([]string{"test", "-mod=vendor", "-race", "-v", "-count", "1", "-tags", "e2e"}, packages...)
sh.RunV("go", args...)
return nil
}
// Runs all test suites
func (Test) All() error {
fmt.Println("Running all tests")
packages, err := getPackages()
if err != nil {
return err
}
args := append([]string{"test", "-mod=vendor", "-race", "-v", "-count", "1", "-tags", "unit e2e"}, packages...)
sh.RunV("go", args...)
return nil
}
// Runs all checks on the mmctl codebase
func Check() {
mg.SerialDeps(Gofmt, Govet)
}
// Downloads all the dependencies to the vendor folder
func Vendor() {
sh.RunV("go", "mod", "vendor")
}