This repository has been archived by the owner on Oct 20, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 45
/
magefile.go
222 lines (176 loc) · 4.41 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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
//go:build mage
// +build mage
package main
import (
"archive/zip"
"fmt"
"io"
"os"
"os/exec"
"github.com/magefile/mage/mg"
"path/filepath"
"strings"
)
// Default target to run when none is specified
// If not set, running mage will list available targets
// var Default = Build
const (
// DIST is the name of the dist directory
DIST = "dist"
)
type target struct {
goos string
goarch string
}
// Build Builds dave and davecli and moves it to the dist directory
func Build() error {
mg.Deps(Clean)
if _, err := os.Stat(DIST); os.IsNotExist(err) {
os.Mkdir(DIST, os.ModePerm)
fmt.Printf("Created dist dir: %s\n", DIST)
}
fmt.Println("Building...")
buildSpecific(target{})
fmt.Printf("Compiled files moved to folder: %s\n", DIST)
return nil
}
// BuildReleases Builds dave and davecli for different OS and package them to a zip file for each os
func BuildReleases() error {
mg.Deps(Clean)
targets := []target{
{"windows", "amd64"},
{"windows", "386"},
{"darwin", "amd64"},
{"linux", "amd64"},
{"linux", "386"},
}
for _, t := range targets {
fmt.Printf("Building for OS %s and architecture %s\n", t.goos, t.goarch)
dave, daveCli, _ := buildSpecific(t)
files := []string{
dave,
daveCli,
"Readme.md",
filepath.Join("examples", "config-sample.yaml"),
}
archiveName := fmt.Sprintf("dave-%s-%s.zip", t.goos, t.goarch)
zipFiles(filepath.Join("dist", archiveName), files)
os.Remove(dave)
os.Remove(daveCli)
}
return nil
}
// Fmt Formats the code via gofmt
func Fmt() error {
fmt.Println("Formatting code ...")
err := execCommand("gofmt", "-s", "-l", "-w", ".").Run()
if err != nil {
return err
}
return nil
}
// Check Runs golint and go tool vet on each .go file.
func Check() error {
fmt.Println("Checking code ...")
vetOut, err := execCommand("go", "vet", "./...").CombinedOutput()
if len(vetOut) > 0 {
fmt.Println(string(vetOut))
}
if err != nil {
return err
}
lintOut, err := execCommand("golint", "./...").CombinedOutput()
if len(lintOut) > 0 {
fmt.Println(string(lintOut))
}
if err != nil {
return err
}
return nil
}
// Install Installs dave and davecli to your $GOPATH/bin folder
func Install() error {
fmt.Println("Installing...")
return execCommand("go", "install", "./...").Run()
}
// Clean Removes the dist directory
func Clean() {
fmt.Println("Cleaning...")
os.RemoveAll(DIST)
}
func execCommand(name string, arg ...string) *exec.Cmd {
if mg.Verbose() {
fmt.Println("Executing:", name, strings.Join(arg, " "))
}
return exec.Command(name, arg...)
}
func buildSpecific(t target) (string, string, error) {
env := os.Environ()
if t.goos != "" && t.goarch != "" {
env = append(env, fmt.Sprintf("GOOS=%s", t.goos))
env = append(env, fmt.Sprintf("GOARCH=%s", t.goarch))
}
daveSource := filepath.Join("cmd", "dave", "main.go")
daveExe := filepath.Join(DIST, "dave")
if t.goos == "windows" {
daveExe += ".exe"
}
daveCommand := execCommand("go", "build", "-o", daveExe, daveSource)
daveCommand.Env = env
err := daveCommand.Run()
if err != nil {
return "", "", err
}
daveCliSource := filepath.Join("cmd", "davecli", "main.go")
daveCliExe := filepath.Join(DIST, "davecli")
if t.goos == "windows" {
daveCliExe += ".exe"
}
daveCliCommand := execCommand("go", "build", "-o", daveCliExe, daveCliSource)
daveCliCommand.Env = env
err = daveCliCommand.Run()
if err != nil {
return "", "", err
}
return daveExe, daveCliExe, nil
}
// zipFiles compresses one or many files into a single zip archive file.
// The original code was published under MIT licence under https://golangcode.com/create-zip-files-in-go/
func zipFiles(filename string, files []string) error {
newfile, err := os.Create(filename)
if err != nil {
return err
}
defer newfile.Close()
zipWriter := zip.NewWriter(newfile)
defer zipWriter.Close()
// Add files to zip
for _, file := range files {
zipfile, err := os.Open(file)
if err != nil {
return err
}
defer zipfile.Close()
// Get the file information
info, err := zipfile.Stat()
if err != nil {
return err
}
header, err := zip.FileInfoHeader(info)
if err != nil {
return err
}
// Change to deflate to gain better compression
// see http://golang.org/pkg/archive/zip/#pkg-constants
header.Method = zip.Deflate
writer, err := zipWriter.CreateHeader(header)
if err != nil {
return err
}
_, err = io.Copy(writer, zipfile)
if err != nil {
return err
}
}
return nil
}