/
mage.go
109 lines (86 loc) · 1.73 KB
/
mage.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
package main
import (
"fmt"
"io"
"log"
"net/http"
"os"
"github.com/magefile/mage/mg"
"github.com/magefile/mage/sh"
"github.com/magefile/mage/target"
)
const (
ouiURL = "https://standards-oui.ieee.org/oui/oui.csv"
ouiCSV = "oui.csv"
ouiData = "data.go"
)
// Build builds the package
func Build() {
mg.Deps(mg.F(generate, ouiCSV, ouiData))
}
// Lint runs golangci-lint
func Lint() error {
mg.Deps(Build)
return sh.RunV("golangci-lint", "run", "--skip-dirs", "magefiles")
}
// Test runs go test with coverage
func Test() error {
mg.Deps(Build)
return sh.RunV("go", "test", "-v", "-coverprofile=coverage.txt", "-covermode=count", "./...")
}
// Clean removes build and test artifacts
func Clean() error {
for _, file := range []string{ouiCSV, "coverage.txt"} {
if err := sh.Rm(file); err != nil {
return err
}
}
return nil
}
// Nuke remove all artifact includes the generated code
func Nuke() error {
mg.Deps(Clean)
return sh.Rm(ouiData)
}
func download() error {
stale, err := target.Path(ouiCSV, "magefiles/mage.go", "magefiles/gen.go")
if err != nil {
return err
}
if !stale {
return nil
}
log.Printf("downloading %q", ouiURL)
resp, err := http.Get(ouiURL)
if err != nil {
return err
}
defer resp.Body.Close()
if resp.StatusCode != 200 {
return fmt.Errorf("download failed: %w", err)
}
fout, err := os.Create("oui.csv")
if err != nil {
return err
}
defer fout.Close()
_, err = io.Copy(fout, resp.Body)
if err != nil {
return err
}
return nil
}
func data() error {
old, err := target.Path("data.go", "oui.csv")
if err != nil {
return err
}
if old {
return download()
}
return nil
}
func init() {
os.Setenv("MAGEFILE_ENABLE_COLOR", "1")
os.Setenv("MAGEFILE_VERBOSE", "1")
}