forked from elastic/package-registry
-
Notifications
You must be signed in to change notification settings - Fork 0
/
magefile.go
142 lines (117 loc) · 3.57 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
// 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
package main
import (
"fmt"
"os"
"path/filepath"
"strings"
"github.com/magefile/mage/mg"
"github.com/magefile/mage/sh"
)
const (
// GoImportsImportPath controls the import path used to install goimports.
GoImportsImportPath = "golang.org/x/tools/cmd/goimports"
// GoImportsLocalPrefix is a string prefix matching imports that should be
// grouped after third-party packages.
GoImportsLocalPrefix = "github.com/elastic"
// GoLicenserImportPath controls the import path used to install go-licenser.
GoLicenserImportPath = "github.com/elastic/go-licenser"
// StaticcheckImport path is the import path of the staticcheck tool.
StaticcheckImportPath = "honnef.co/go/tools/cmd/staticcheck"
buildDir = "./build"
)
func Build() error {
return sh.Run("go", "build", ".")
}
func Check() error {
mg.SerialDeps(
Format,
Build,
ModTidy,
Staticcheck,
)
// Check if no changes are shown
err := sh.RunV("git", "update-index", "--refresh")
if err != nil {
return err
}
return sh.RunV("git", "diff-index", "--exit-code", "HEAD", "--")
}
func Test() error {
return sh.RunV("go", "test", "./...", "-v")
}
// Format adds license headers, formats .go files with goimports, and formats
// .py files with autopep8.
func Format() {
// Don't run AddLicenseHeaders and GoImports concurrently because they
// both can modify the same files.
mg.SerialDeps(
AddLicenseHeaders,
GoImports,
)
}
// GoImports executes goimports against all .go files in and below the CWD. It
// ignores vendor/ directories.
func GoImports() error {
goFiles, err := FindFilesRecursive(func(path string, _ os.FileInfo) bool {
return filepath.Ext(path) == ".go" && !strings.Contains(path, "vendor/")
})
if err != nil {
return err
}
if len(goFiles) == 0 {
return nil
}
fmt.Println(">> fmt - goimports: Formatting Go code")
args := append(
[]string{"run", GoImportsImportPath, "-local", GoImportsLocalPrefix, "-l", "-w"},
goFiles...,
)
return sh.RunV("go", args...)
}
// AddLicenseHeaders adds license headers to .go files. It applies the
// appropriate license header based on the value of mage.BeatLicense.
func AddLicenseHeaders() error {
fmt.Println(">> fmt - go-licenser: Adding missing headers")
return sh.RunV("go", "run", GoLicenserImportPath, "-license", "Elastic")
}
// FindFilesRecursive recursively traverses from the CWD and invokes the given
// match function on each regular file to determine if the given path should be
// returned as a match.
func FindFilesRecursive(match func(path string, info os.FileInfo) bool) ([]string, error) {
var matches []string
err := filepath.Walk(".", func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if !info.Mode().IsRegular() {
// continue
return nil
}
if match(filepath.ToSlash(path), info) {
matches = append(matches, path)
}
return nil
})
return matches, err
}
func Clean() error {
err := os.RemoveAll(buildDir)
if err != nil {
return err
}
return os.RemoveAll("package-registry")
}
// ModTidy cleans unused dependencies.
func ModTidy() error {
fmt.Println(">> fmt - go mod tidy: Generating go mod files")
return sh.RunV("go", "mod", "tidy")
}
// Staticcheck runs a static code analyzer.
func Staticcheck() error {
fmt.Println(">> check - staticcheck: Running static code analyzer")
return sh.RunV("go", "run", StaticcheckImportPath, "./...")
}