This repository has been archived by the owner on May 5, 2023. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
main.go
344 lines (309 loc) · 10.4 KB
/
main.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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
package main
import (
"bufio"
"crypto/sha256"
"encoding/hex"
"fmt"
"io"
"log"
"os"
"os/exec"
"os/signal"
"path"
"sort"
"strings"
"github.com/alecthomas/kong"
"github.com/goreleaser/goreleaser/pkg/config"
)
var (
cli Cli
version = "dev"
name = "goreleaser-xx"
desc = "Cross compilation helper for GoReleaser"
url = "https://github.com/crazy-max/goreleaser-xx"
)
// Cli holds command line args, flags and cmds
type Cli struct {
Version kong.VersionFlag
Debug bool `kong:"name='debug',env='DEBUG',default='false',help='Enable debug.'"`
GitRef string `kong:"name='git-ref',env='GIT_REF',help='The branch or tag like refs/tags/v1.0.0 (default to your working tree info).'"`
GoReleaser string `kong:"name='goreleaser',env='GORELEASER_PATH',default='goreleaser',help='Set a specific GoReleaser binary to use.'"`
Config string `kong:"name='config',type='path',env='GORELEASER_CONFIG',help='Load GoReleaser configuration from file.'"`
GoBinary string `kong:"name='go-binary',env='GORELEASER_GOBINARY',help='Set a specific go binary to use when building.'"`
Name string `kong:"name='name',env='GORELEASER_NAME',help='Project name.'"`
Dist string `kong:"name='dist',env='GORELEASER_DIST',default='./dist',help='Dist folder where artifact will be stored.'"`
Artifacts []string `kong:"name='artifacts',env='GORELEASER_ARTIFACTS',enum='archive,bin',default='archive',help='Types of artifact to create.'"`
Main string `kong:"name='main',env='GORELEASER_MAIN',default='.',help='Path to main.go file or main package.'"`
Flags string `kong:"name='flags',env='GORELEASER_FLAGS',help='Custom flags templates.'"`
Asmflags string `kong:"name='asmflags',env='GORELEASER_ASMFLAGS',help='Custom asmflags templates.'"`
Gcflags string `kong:"name='gcflags',env='GORELEASER_GCFLAGS',help='Custom gcflags templates.'"`
Ldflags string `kong:"name='ldflags',env='GORELEASER_LDFLAGS',help='Custom ldflags templates.'"`
Tags string `kong:"name='tags',env='GORELEASER_TAGS',help='Custom build tags templates.'"`
Files []string `kong:"name='files',env='GORELEASER_FILES',help='Additional files/template/globs you want to add to the archive.'"`
Replacements map[string]string `kong:"name='replacements',env='GORELEASER_REPLACEMENTS',help='Replacements for GOOS and GOARCH in the archive/binary name.'"`
Envs []string `kong:"name='envs',env='GORELEASER_ENVS',help='Custom environment variables to be set during the build.'"`
PreHooks []string `kong:"name='pre-hooks',env='GORELEASER_PRE_HOOKS',help='Hooks which will be executed before the build.'"`
PostHooks []string `kong:"name='post-hooks',env='GORELEASER_POST_HOOKS',help='Hooks which will be executed after the build.'"`
Snapshot bool `kong:"name='snapshot',env='GORELEASER_SNAPSHOT',default='false',help='Run in snapshot mode.'"`
Checksum bool `kong:"name='checksum',env='GORELEASER_CHECKSUM',default='true',help='Create checksum.'"`
// Deprecated flags
ArtifactType string `kong:"hidden,name='artifact-type',env='GORELEASER_ARTIFACTTYPE',help='artifacts'"`
Hooks []string `kong:"hidden,name='hooks',env='GORELEASER_HOOKS',help='pre-hooks'"`
BuildPreHooks []string `kong:"hidden,name='build-pre-hooks',env='GORELEASER_BUILD_PRE_HOOKS',help='pre-hooks'"`
BuildPostHooks []string `kong:"hidden,name='build-post-hooks',env='GORELEASER_BUILD_POST_HOOKS',help='post-hooks'"`
}
func main() {
var err error
// Parse command line
ctx := kong.Parse(&cli,
kong.Name(name),
kong.Description(fmt.Sprintf("%s. More info: %s", desc, url)),
kong.UsageOnError(),
kong.Vars{
"version": version,
},
kong.ConfigureHelp(kong.HelpOptions{
Compact: true,
Summary: true,
}))
// Init
log.SetFlags(0)
log.Printf("INF: starting %s", fmt.Sprintf("%s/%s", name, version))
// Handle os signals
channel := make(chan os.Signal)
signal.Notify(channel, os.Interrupt, SIGTERM)
go func() {
sig := <-channel
log.Printf("WARN: caught signal %v", sig)
os.Exit(0)
}()
if cli.Debug {
log.Println("DBG: environment:")
printEnv()
}
// Warn on deprecated flag usage and assign to new flag
for _, f := range ctx.Flags() {
if f.Hidden && f.Set {
for _, fa := range ctx.Flags() {
if fa.Name != f.Help {
continue
}
if fa.Set {
continue
}
switch f.Name {
case "artifact-type":
cli.Artifacts = []string{f.Target.String()}
case "hooks", "build-pre-hooks":
cli.PreHooks = f.Target.Interface().([]string)
case "build-post-hooks":
cli.PostHooks = f.Target.Interface().([]string)
}
break
}
log.Printf("WARN: --%s is deprecated and will be removed in a future release. Use --%s instead.", f.Name, f.Help)
}
}
// Seek GoReleaser bin
goreleaserPath, err := exec.LookPath(cli.GoReleaser)
if err != nil {
log.Fatalf("ERR: %s not found in PATH", cli.GoReleaser)
}
// Get target
target := getTarget()
if cli.Debug {
log.Println("DBG: target:")
log.Printf(" GOOS=%s", target.Os)
log.Printf(" GOARCH=%s", target.Arch)
log.Printf(" GOARM=%s", target.Arm)
log.Printf(" GOMIPS=%s", target.Mips)
log.Printf(" GOMIPS64=%s", target.Mips64)
log.Printf(" GOAMD64=%s", target.Amd64)
}
// Get compilers
compilers := getCompilers(target)
// GoReleaser config
config, err := getConfig(cli, target, compilers)
if err != nil {
log.Fatalf("ERR: %v", err)
}
defer func() {
_ = os.Remove(config.Path)
_ = os.RemoveAll(config.Project.Dist)
}()
if cli.Debug {
log.Println("DBG: go env:")
printGoEnv(config.Project, target)
}
// Set GoReleaser flags
goreleaserFlags := []string{"release", "--config", config.Path}
// Git tag
if strings.HasPrefix(cli.GitRef, "refs/tags/v") {
if err := os.Setenv("GORELEASER_CURRENT_TAG", strings.TrimLeft(cli.GitRef, "refs/tags/")); err != nil {
log.Printf("WARN: cannot set GORELEASER_CURRENT_TAG env var: %v", err)
}
}
gitTag, err := getGitTag()
if err != nil {
gitTag = "v0.0.0"
}
// Git validate
gitDirty := isGitDirty()
gitWrongRef := isWrongRef(gitTag)
if gitDirty || gitWrongRef || cli.Snapshot {
goreleaserFlags = append(goreleaserFlags, "--snapshot")
}
// Git status
log.Println("INF: git status:")
log.Printf(" tag=%s", gitTag)
log.Printf(" dirty=%t", gitDirty)
log.Printf(" wrongref=%t", gitWrongRef)
// Start GoReleaser
log.Printf("INF: %s %s", goreleaserPath, strings.Join(goreleaserFlags, " "))
goreleaser := exec.Command(cli.GoReleaser, goreleaserFlags...)
goreleaser.Stdout = os.Stdout
goreleaser.Stderr = os.Stderr
if err := goreleaser.Run(); err != nil {
log.Fatalf("ERR: goreleaser failed: %v", err)
}
// Create dist folder
if err := os.Mkdir(cli.Dist, 0755); err != nil {
log.Fatal(err)
}
// Post build
fdist, err := os.Open(config.Project.Dist)
if err != nil {
log.Printf("WARN: cannot open dist folder: %v", err)
}
defer fdist.Close()
fis, err := fdist.Readdir(-1)
if err != nil {
log.Printf("WARN: cannot read dist folder: %v", err)
}
for _, fi := range fis {
if fi.IsDir() {
continue
}
archiveExt := ""
for _, ext := range []string{".tar.gz", ".tar.xz", ".tar", ".gz", ".zip"} {
if strings.HasSuffix(fi.Name(), ext) {
archiveExt = ext
break
}
}
if len(archiveExt) > 0 {
for _, atf := range cli.Artifacts {
var atfPath string
switch atf {
case "bin":
binName := config.Project.ProjectName
atfName := strings.TrimSuffix(fi.Name(), archiveExt)
if target.Os == "windows" {
atfName += ".exe"
binName += ".exe"
}
atfPath = path.Join(cli.Dist, atfName)
if err := copyFile(path.Join("/usr/local/bin", binName), atfPath); err != nil {
log.Fatalf("ERR: cannot copy binary: %v", err)
}
case "archive":
atfPath = path.Join(cli.Dist, fi.Name())
if err := copyFile(path.Join(fdist.Name(), fi.Name()), atfPath); err != nil {
log.Fatalf("ERR: cannot copy archive: %v", err)
}
default:
log.Fatalf("ERR: unknown artifact type: %s", atf)
}
log.Printf("INF: successfully created: %s", atfPath)
if cli.Checksum {
checksum(atfPath)
}
}
} else {
atfPath := path.Join(cli.Dist, fi.Name())
if err := copyFile(path.Join(fdist.Name(), fi.Name()), atfPath); err != nil {
log.Fatalf("ERR: cannot copy from dist: %v", err)
}
log.Printf("INF: successfully created: %s", atfPath)
}
}
}
func copyFile(src string, dest string) error {
srcFile, err := os.Open(src)
if err != nil {
return err
}
defer srcFile.Close()
destFile, err := os.Create(dest)
if err != nil {
return err
}
defer destFile.Close()
_, err = io.Copy(destFile, srcFile)
if err != nil {
return err
}
err = destFile.Sync()
if err != nil {
return err
}
return nil
}
func checksum(filename string) {
file, err := os.Open(filename)
if err != nil {
log.Fatalf("ERR: failed to open file to compute: %v", err)
}
defer file.Close()
checksumFile := filename + ".sha256"
h := sha256.New()
_, err = io.Copy(h, file)
if err != nil {
log.Fatalf("ERR: failed to create checksum: %v", err)
}
sha256file, err := os.OpenFile(checksumFile, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0o644)
if err != nil {
log.Fatalf("ERR: failed to open file: %v", err)
}
defer sha256file.Close()
_, err = sha256file.WriteString(hex.EncodeToString(h.Sum(nil)))
if err != nil {
log.Fatalf("ERR: failed to write file: %v", err)
}
log.Printf("INF: checksum file created: %s", checksumFile)
}
func printEnv() {
envs := os.Environ()
sort.Strings(envs)
for _, e := range envs {
log.Printf(" %s", e)
}
}
func printGoEnv(config config.Project, target Target) {
bin := "go"
if cli.GoBinary != "" {
bin = cli.GoBinary
}
cmd := exec.Command(bin, "env")
cmd.Env = append(os.Environ(), append([]string{
fmt.Sprintf("GOOS=%s", target.Os),
fmt.Sprintf("GOARCH=%s", target.Arch),
fmt.Sprintf("GOARM=%s", target.Arm),
fmt.Sprintf("GOMIPS=%s", target.Mips),
fmt.Sprintf("GOMIPS64=%s", target.Mips64),
fmt.Sprintf("GOAMD64=%s", target.Amd64),
}, config.Env...)...)
stdout, err := cmd.StdoutPipe()
if err != nil {
log.Fatalf("ERR: %v", err)
}
err = cmd.Start()
if err != nil {
log.Fatalf("ERR: %v", err)
}
defer cmd.Wait()
s := bufio.NewScanner(stdout)
for s.Scan() {
log.Printf(" %s", s.Text())
}
}