-
Notifications
You must be signed in to change notification settings - Fork 0
/
magefile.go
163 lines (142 loc) · 3.95 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
// +build mage
package main
import (
"fmt"
"os"
"os/exec"
"github.com/magefile/mage/mg"
)
// Default target to run when none is specified
// If not set, running mage will list available targets
// var Default = Build
// Build builds the binary
func Build() error {
mg.Deps(InstallDeps)
os.Setenv("CGO_ENABLED", "0")
os.Setenv("GOOS", "linux")
fmt.Println("Building...")
cmd := exec.Command("go", "build", "-o", "api_server", "./cmd/api/server.go")
return cmd.Run()
}
// Install installs the binary in /usr/local/bin
func Install() error {
mg.Deps(Build)
fmt.Println("Installing...")
return os.Rename("./api_server", "/usr/bin/api_server")
}
// InstallDeps installs all the needed dependencies
func InstallDeps() error {
fmt.Println("Installing Deps...")
cmd := exec.Command("go", "get", "./...")
return cmd.Run()
}
// Clean cleans up everything.
func Clean() {
fmt.Println("Cleaning...")
}
// Start starts the API HTPP server. Currently using pm2 as process manager
func Start() error {
mg.Deps(InstallDeps, Build)
cmd := exec.Command("pm2", "start", "./api_server", "--name", "api")
return cmd.Run()
}
// Start starts the API HTPP server. Currently using pm2 as process manager
func StartAPI() error {
mg.Deps(InstallDeps, Build)
os.Setenv("GIN_MODE", "release")
cmd := exec.Command("./api_server")
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
return cmd.Run()
}
// Stop stops the API HTTP server.
func Stop() error {
cmd := exec.Command("pm2", "stop", "api")
return cmd.Run()
}
// StartDev startdev bootstraps a working dev environment.
func StartDev() error {
cwd, err := os.Getwd()
if err != nil {
fmt.Printf("Error: ", err.Error())
}
dataVol := fmt.Sprintf("%s/internal/db/data:/var/lib/mysql", cwd)
sqlVol := fmt.Sprintf("%s/internal/db/sql:/sql", cwd)
cmd := exec.Command("docker", "run", "--name", "mariadb", "-e", "MYSQL_RANDOM_ROOT_PASSWORD=yes", "-e",
"MYSQL_PASSWORD=workday", "-e", "MYSQL_USER=workday", "-e", "MYSQL_DATABASE=workday", "-v",
dataVol, "-v", sqlVol, "-p", "3306:3306", "-d", "mariadb:10.4",
)
err = cmd.Run()
if err != nil {
fmt.Println(err.Error())
os.Exit(1)
}
return nil
}
// CreateVM creates a vagrant box already provisioned
func CreateVM() error {
cwd, err := os.Getwd()
if err != nil {
fmt.Printf("Error: ", err.Error())
}
cmd := exec.Command("packer", "build", "-var", "'version=10.2.0'", "debian10.json")
cmd.Dir = cwd + "/.packer"
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
return cmd.Run()
}
// ProvisionVM runs an Ansible playbook to provision the configured host.
func ProvisionVM() error {
cwd, err := os.Getwd()
if err != nil {
fmt.Printf("Error: ", err.Error())
}
cmd := exec.Command("ansible-playbook", "-i", "env/packer", "workday.yml")
cmd.Dir = cwd + "/.packer/ansible"
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
return cmd.Run()
}
// Test runs the tests suite.
func Test() error {
fmt.Println("Running tests")
os.Setenv("GIN_MODE", "test")
cmd := exec.Command("go", "test", "-v", "./...")
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
err := cmd.Run()
if err != nil {
fmt.Println(err.Error())
os.Exit(1)
}
return nil
}
// TestVerbose runs the tests suite with verbosity
func TestVerbose() error {
fmt.Println("Running tests")
os.Setenv("GIN_MODE", "test")
cmd := exec.Command("ginkgo", "-v", "test", "./...")
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
err := cmd.Run()
if err != nil {
fmt.Println(err.Error())
os.Exit(1)
}
return nil
}
// TestAndCoverage runs tests and generate the code coverage.
func TestAndCoverage() error {
fmt.Println("Running tests and generating code coverage")
os.Setenv("GIN_MODE", "test")
cmd := exec.Command("go", "test", "-v", "-cover", "-coverprofile=workday.coverprofile", "./...")
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
err := cmd.Run()
if err != nil {
fmt.Println(err.Error())
os.Exit(1)
}
cmd = exec.Command("go", "tool", "cover", "-html=workday.coverprofile", "-o", "docs/static/coverage.html")
return cmd.Run()
}