-
Notifications
You must be signed in to change notification settings - Fork 2
/
magefile.go
123 lines (105 loc) · 3.09 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
// +build mage
package main
import (
"context"
"fmt"
"os"
"os/exec"
"path/filepath"
"strings"
"github.com/magefile/mage/mg" // mg contains helpful utility functions, like Deps
"github.com/magefile/mage/sh"
)
const (
BasePackage = "github.com/Scalingo/sand"
)
// Default target to run when none is specified
// If not set, running mage will list available targets
var Default = Build
// A build step that requires additional params, or platform specific steps for example
func Build(ctx context.Context) error {
mg.CtxDeps(ctx, InstallDeps)
fmt.Println("Building…")
cmd := exec.Command("go", "build", "-i", "github.com/Scalingo/sand/cmd/sand-agent")
return cmd.Run()
}
// Run specs on the project
func Test(ctx context.Context) error {
fmt.Println("Testing…")
return sh.RunV("go", "test", "./...")
}
// Generate the mocks of used interface for `mockgen/gomock`
func GenerateMocks(ctx context.Context) error {
mg.CtxDeps(ctx, InstallTestDeps)
mocks := []struct {
Package string
Interface string
}{
{Package: "netlink", Interface: "Handler"},
{Package: "ipallocator", Interface: "IPAllocator"},
{Package: "netnsbuilder", Interface: "Manager"},
{Package: "idmanager", Interface: "Manager"},
{Package: "store", Interface: "Store"},
{Package: "store", Interface: "Watcher"},
{Package: "endpoint", Interface: "Repository"},
{Package: "network", Interface: "Repository"},
{Package: "network/netmanager", Interface: "NetManager"},
{Package: "network/overlay", Interface: "NetworkEndpointListener"},
{Package: "client/sand", Interface: "Client"},
}
for _, mock := range mocks {
fmt.Printf("Building mock of %v#%v\n", mock.Package, mock.Interface)
mockPackage := mock.Package + "mock"
mockDirectory := fmt.Sprintf("test/mocks/%s", mockPackage)
filePackage := filepath.Base(mockPackage)
mockFile := fmt.Sprintf("%s/%s_mock.go", mockDirectory, strings.ToLower(mock.Interface))
err := os.MkdirAll(mockDirectory, 0755)
if err != nil {
return err
}
err = sh.Run(
"mockgen",
"-destination", mockFile,
"-package", filePackage,
fmt.Sprintf("%s/%s", BasePackage, mock.Package),
mock.Interface,
)
if err != nil {
return err
}
err = sh.Run("sed", "-i", fmt.Sprintf("s,%s/vendor/,,", BasePackage), mockFile)
if err != nil {
return err
}
err = sh.RunV("goimports", "-w", mockFile)
if err != nil {
return err
}
}
return nil
}
// A custom install step if you need your bin someplace other than go/bin
func Install(ctx context.Context) error {
mg.CtxDeps(ctx, Build)
fmt.Println("Installing…")
return sh.Run("go", "install", "github.com/Scalingo/sand/cmd/sand-agent")
}
// Manage your deps, or running package managers.
func InstallDeps() error {
return nil
}
// Install test dependencies like gomock/mockgen
func InstallTestDeps() error {
cmd := exec.Command("go", "get", "github.com/golang/mock/gomock")
err := cmd.Run()
if err != nil {
return err
}
cmd = exec.Command("go", "get", "github.com/golang/mock/mockgen")
return cmd.Run()
}
// Clean up after yourself
func Clean() {
fmt.Println("Cleaning…")
os.RemoveAll("./sand-agent")
}