/
go.go
243 lines (205 loc) · 5.8 KB
/
go.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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
// package mageutil provides util functions for [Magefile].
// For usage please refer to [documentation] provided by Magefile.
// For autocompletions see [completions].
//
// [Magefile]: https://magefile.org/
// [documentation]: https://magefile.org/importing/
// [completions]: https://github.com/elisasre/mageutil/tree/main/completions
package mageutil
import (
"context"
"fmt"
"os"
"path"
"github.com/elisasre/mageutil/golang"
"github.com/magefile/mage/mg"
"github.com/magefile/mage/sh"
)
const (
CmdDir = "./cmd/"
TargetDir = "./target/"
ReportsDir = TargetDir + "reports/"
)
// BuildInfo contains relevant information about produced binary.
type BuildInfo struct {
BinPath string
GOOS string
GOARCH string
}
// Deprecated: use sub package.
func Go(ctx context.Context, args ...string) error {
deprecated()
return golang.Go(ctx, args...)
}
// Deprecated: use sub package.
func GoWith(ctx context.Context, env map[string]string, args ...string) error {
deprecated()
return golang.GoWith(ctx, env, args...)
}
// Deprecated: use sub package.
func Targets(ctx context.Context) ([]string, error) {
deprecated()
entries, err := os.ReadDir(CmdDir)
if err != nil {
return nil, err
}
targets := make([]string, 0, len(entries))
for _, e := range entries {
if e.IsDir() {
targets = append(targets, e.Name())
}
}
return targets, nil
}
// Deprecated: use sub package.
func BuildAll(ctx context.Context) error {
deprecated()
targets, err := Targets(ctx)
if err != nil {
return fmt.Errorf("failed to get list of targets: %w", err)
}
for _, target := range targets {
// Building is CPU intensive and already scales to all available cores hence use of the mg.Serial.
mg.SerialCtxDeps(ctx, mg.F(Build, target))
}
return nil
}
// Deprecated: use sub package.
func Build(ctx context.Context, name string) error {
deprecated()
_, err := BuildWithInfo(ctx, name)
return err
}
// Deprecated: use sub package.
func BuildWithInfo(ctx context.Context, name string) (BuildInfo, error) {
deprecated()
goos, err := sh.Output("go", "env", "GOOS")
if err != nil {
return BuildInfo{}, err
}
goarch, err := sh.Output("go", "env", "GOARCH")
if err != nil {
return BuildInfo{}, err
}
return BuildForWithInfo(ctx, goos, goarch, name)
}
// Deprecated: use sub package.
func BuildWithSHA(ctx context.Context, goos, goarch, name string) {
deprecated()
mg.CtxDeps(ctx, func() error {
_, err := BuildWithSHAWithInfo(ctx, goos, goarch, name)
return err
})
}
// Deprecated: use sub package.
func BuildWithSHAWithInfo(ctx context.Context, goos, goarch, name string) (BuildInfo, error) {
deprecated()
info, err := BuildForWithInfo(ctx, goos, goarch, name)
if err != nil {
return BuildInfo{}, err
}
return info, SHA256Sum(ctx, info.BinPath)
}
// Deprecated: use sub package.
func BuildFor(ctx context.Context, goos, goarch, name string) error {
deprecated()
_, err := BuildForWithInfo(ctx, goos, goarch, name)
return err
}
// Deprecated: use sub package.
func BuildForWithInfo(ctx context.Context, goos, goarch, name string) (BuildInfo, error) {
deprecated()
cmdPath := CmdDir + name
binaryPath := path.Join(TargetDir, "bin", goos, goarch, name)
env := map[string]string{
"GOOS": goos,
"GOARCH": goarch,
}
res := BuildInfo{BinPath: binaryPath, GOOS: goos, GOARCH: goarch}
return res, GoWith(ctx, env, "build", "-o", binaryPath, cmdPath)
}
// Deprecated: use sub package.
func BuildForLinux(ctx context.Context, name string) {
deprecated()
BuildWithSHA(ctx, "linux", "amd64", name)
}
// Deprecated: use sub package.
func BuildForLinuxWithInfo(ctx context.Context, name string) (BuildInfo, error) {
deprecated()
return BuildWithSHAWithInfo(ctx, "linux", "amd64", name)
}
// Deprecated: use sub package.
func BuildForMac(ctx context.Context, name string) {
BuildWithSHA(ctx, "darwin", "amd64", name)
}
// Deprecated: use sub package.
func BuildForMacWithInfo(ctx context.Context, name string) (BuildInfo, error) {
deprecated()
return BuildWithSHAWithInfo(ctx, "darwin", "amd64", name)
}
// Deprecated: use sub package.
func BuildForArmMac(ctx context.Context, name string) {
BuildWithSHA(ctx, "darwin", "arm64", name)
}
// Deprecated: use sub package.
func BuildForArmMacWithInfo(ctx context.Context, name string) (BuildInfo, error) {
deprecated()
return BuildWithSHAWithInfo(ctx, "darwin", "arm64", name)
}
// Deprecated: use sub package.
func BuildForWindows(ctx context.Context, name string) {
BuildWithSHA(ctx, "windows", "amd64", name)
}
// Deprecated: use sub package.
func BuildForWindowsWithInfo(ctx context.Context, name string) (BuildInfo, error) {
deprecated()
return BuildWithSHAWithInfo(ctx, "windows", "amd64", name)
}
// Deprecated: use sub package.
func Run(ctx context.Context, name string, args ...string) error {
deprecated()
bd, err := BinDir()
if err != nil {
return err
}
binaryPath := path.Join(bd, name)
return sh.RunV(binaryPath, args...)
}
// Deprecated: use sub package.
func GoList(ctx context.Context, target string) ([]string, error) {
deprecated()
return golang.ListPackages(ctx, target)
}
// Deprecated: use sub package.
func BinDir() (string, error) {
deprecated()
goos, err := sh.Output("go", "env", "GOOS")
if err != nil {
return "", err
}
goarch, err := sh.Output("go", "env", "GOARCH")
if err != nil {
return "", err
}
return path.Join(TargetDir, "bin", goos, goarch), nil
}
// Deprecated: use sub package.
func Ensure(ctx context.Context) error {
deprecated()
return golang.Tidy(ctx)
}
// Deprecated: use sub package.
func EnsureInSync(ctx context.Context) error {
deprecated()
return golang.TidyAndVerify(ctx)
}
// Deprecated: use sub package.
func Tidy(ctx context.Context) error {
deprecated()
return golang.Tidy(ctx)
}
// Deprecated: use sub package.
func TidyAndVerifyNoChanges(ctx context.Context) error {
deprecated()
return golang.TidyAndVerify(ctx)
}