-
Notifications
You must be signed in to change notification settings - Fork 3
/
magefile.go
179 lines (150 loc) · 3.89 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
//go:build mage
package main
import (
"bufio"
"context"
"fmt"
"os"
"path/filepath"
"github.com/magefile/mage/mg"
"github.com/magefile/mage/sh"
"github.com/mholt/archiver/v4"
)
const releaseVersion = "v0.1.0"
var productName = "telegraf-companion"
type platform struct {
OS string
ARCH string
Extension string
ArchiveFormat string
}
var platforms = []platform{
{
OS: "linux",
ARCH: "amd64",
ArchiveFormat: "tar.gz",
},
{
OS: "windows",
ARCH: "amd64",
Extension: ".exe",
ArchiveFormat: "zip",
},
{
OS: "darwin",
ARCH: "amd64",
ArchiveFormat: "tar.gz",
},
}
// Release packages all the binaries and records checksums
func Release() error {
if _, err := os.Stat("release"); os.IsNotExist(err) {
os.MkdirAll("release", 0777) // Create your file
}
var binPaths []string
for _, p := range platforms {
binPath, err := build(p)
if err != nil {
return err
}
binPaths = append(binPaths, binPath)
// map files on disk to their paths in the archive
files, err := archiver.FilesFromDisk(nil, map[string]string{
binPath: filepath.Base(binPath),
"LICENSE": "",
"README.md": "",
})
if err != nil {
return err
}
// create the output file we'll write to
fileName := fmt.Sprintf("%s_%s_%s_%s.%s", productName, releaseVersion, p.OS, p.ARCH, p.ArchiveFormat)
out, err := os.Create(filepath.Join("release", fileName))
if err != nil {
return err
}
defer out.Close()
// we can use the CompressedArchive type to gzip a tarball
// (compression is not required; you could use Tar directly)
var format archiver.CompressedArchive
switch p.ArchiveFormat {
case "tar.gz":
format = archiver.CompressedArchive{
Compression: archiver.Gz{},
Archival: archiver.Tar{},
}
case "zip":
format = archiver.CompressedArchive{
Archival: archiver.Zip{},
}
}
// create the archive
err = format.Archive(context.Background(), out, files)
if err != nil {
return err
}
}
checksumFilename := fmt.Sprintf("%s_%s_checksum.txt", productName, releaseVersion)
checksumPath := filepath.Join("release", checksumFilename)
f, err := os.Create(checksumPath)
if err != nil {
return err
}
w := bufio.NewWriter(f)
for _, f := range binPaths {
output, err := sh.OutputWith(map[string]string{}, "sha256sum", f)
if err != nil {
fmt.Fprint(os.Stderr, output)
}
w.WriteString(output + "\n")
}
w.Flush()
return nil
}
type Build mg.Namespace
// Builds a Linux binary 64 bit.
func (Build) Linux() error {
_, err := build(platforms[0])
return err
}
// Builds a Windows binary 64 bit.
func (Build) Windows() error {
_, err := build(platforms[1])
return err
}
// Builds a Darwin binary 64 bit.
func (Build) Darwin() error {
_, err := build(platforms[2])
return err
}
// build will build telegraf-companion for the passed platform and returns the filepath
func build(p platform) (string, error) {
env := map[string]string{"GOOS": p.OS, "GOARCH": p.ARCH}
folderName := fmt.Sprintf("%s_%s", p.OS, p.ARCH)
binName := productName + p.Extension
filePath := filepath.Join("bin", folderName, binName)
err := runCmd(env, "go", "build", "-o", filePath, "cmd/main.go")
if err != nil {
return "", err
}
return filePath, nil
}
// Test runs all go tests.
func Test() error {
return runCmd(map[string]string{}, "go", "test", "./...")
}
// UpdateSampleConfigs updates the plugin sample configurations to the latest version
func UpdateSampleConfigs() error {
// TODO: this should update the telegraf version mentioned in README.md
return runCmd(map[string]string{}, "go", "generate", "./plugins/sampleConfigs.go")
}
func runCmd(env map[string]string, cmd string, args ...string) error {
if mg.Verbose() {
return sh.RunWith(env, cmd, args...)
}
output, err := sh.OutputWith(env, cmd, args...)
if err != nil {
fmt.Fprint(os.Stderr, output)
}
return err
}