-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy pathmagefile.go
174 lines (144 loc) · 4.44 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
// Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
// or more contributor license agreements. Licensed under the Elastic License;
// you may not use this file except in compliance with the Elastic License.
//go:build mage
// +build mage
package main
import (
"errors"
"fmt"
"os"
"strings"
"github.com/magefile/mage/mg"
"github.com/magefile/mage/sh"
)
const (
goLintRepo = "golang.org/x/lint/golint"
goLicenserRepo = "github.com/elastic/go-licenser"
goProtocGenGo = "google.golang.org/protobuf/cmd/protoc-gen-go@v1.28"
goProtocGenGoGRPC = "google.golang.org/grpc/cmd/protoc-gen-go-grpc@v1.2"
)
// Aliases for commands required by master makefile
var Aliases = map[string]interface{}{
"prepare": Prepare.All,
"update": Update.All,
"fmt": Format.All,
"format": Format.All,
"check": Check.All,
}
// Prepare tasks related to bootstrap the environment or get information about the environment.
type Prepare mg.Namespace
// Update updates the generated GRPC code.
type Update mg.Namespace
// Format automatically format the code.
type Format mg.Namespace
// Check namespace contains tasks related check the actual code quality.
type Check mg.Namespace
// InstallGoLicenser install go-licenser to check license of the files.
func (Prepare) InstallGoLicenser() error {
return GoInstall(fmt.Sprintf("%s@latest", goLicenserRepo))
}
// InstallGoLint for the code.
func (Prepare) InstallGoLint() error {
return GoInstall(fmt.Sprintf("%s@latest", goLintRepo))
}
// All runs prepare:installGoLicenser and prepare:installGoLint.
func (Prepare) All() {
mg.SerialDeps(Prepare.InstallGoLicenser, Prepare.InstallGoLint)
}
// Prepare installs the required GRPC tools for generation to occur.
func (Update) Prepare() error {
if err := GoInstall(goProtocGenGo); err != nil {
return err
}
return GoInstall(goProtocGenGoGRPC)
}
// Generate generates the necessary GRPC and Go code. It generates both,
// then reports all errors if any.
func (Update) Generate() error {
defer mg.SerialDeps(Format.All)
errGRPC := sh.RunV(
"protoc",
"--go_out=pkg/proto",
"--go_opt=paths=source_relative",
"--go-grpc_out=pkg/proto",
"--go-grpc_opt=paths=source_relative",
"elastic-agent-client.proto",
"elastic-agent-client-future.proto",
"elastic-agent-client-deprecated.proto",
)
if errGRPC != nil {
errGRPC = fmt.Errorf("failed to generate GRPC code: %w", errGRPC)
}
errGenerate := sh.RunV("go", "generate", "./...")
if errGenerate != nil {
errGenerate = fmt.Errorf("failed to run go generate: %w", errGenerate)
}
switch {
case errGRPC != nil && errGenerate != nil:
return fmt.Errorf("all code generation failed: '%v' and '%v'",
errGRPC, errGenerate)
case errGRPC != nil:
return errGRPC
case errGenerate != nil:
return errGenerate
}
return nil
}
// All runs update:prepare then update:generate.
func (Update) All() {
mg.SerialDeps(Update.Prepare, Update.Generate)
}
// All format automatically all the codes.
func (Format) All() {
mg.SerialDeps(Format.License)
}
// License applies the right license header.
func (Format) License() error {
mg.Deps(Prepare.InstallGoLicenser)
return errors.Join(
sh.RunV("go-licenser", "-license", "Elastic"),
sh.RunV("go-licenser", "-license", "Elastic", "-ext", ".proto"),
)
}
// All run all the code checks.
func (Check) All() {
mg.SerialDeps(Check.License, Check.GoLint)
}
// GoLint run the code through the linter.
func (Check) GoLint() error {
mg.Deps(Prepare.InstallGoLint)
packagesString, err := sh.Output("go", "list", "./...")
if err != nil {
return err
}
var errs []error
packages := strings.Split(packagesString, "\n")
for _, pkg := range packages {
if strings.Contains(pkg, "/vendor/") {
continue
}
if e := sh.RunV("golint", "-set_exit_status", pkg); e != nil {
errs = append(errs, e)
}
}
return errors.Join(errs...)
}
// License makes sure that all the Golang files have the appropriate license header.
func (Check) License() error {
mg.Deps(Prepare.InstallGoLicenser)
// exclude copied files until we come up with a better option
return errors.Join(
sh.RunV("go-licenser", "-d", "-license", "Elastic"),
)
}
// GoGet fetch a remote dependencies.
func GoGet(link string) error {
_, err := sh.Exec(map[string]string{}, os.Stdout, os.Stderr, "go", "get", link)
return err
}
// GoInstall installs a remote dependencies.
func GoInstall(link string) error {
_, err := sh.Exec(map[string]string{}, os.Stdout, os.Stderr, "go", "install", link)
return err
}