-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild_archive.go
124 lines (101 loc) · 2.76 KB
/
build_archive.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
package mage
import (
"archive/tar"
"compress/gzip"
"context"
"fmt"
"io"
"os"
"path/filepath"
"github.com/dosquad/mage/dyndep"
"github.com/dosquad/mage/helper"
"github.com/dosquad/mage/loga"
"github.com/magefile/mage/mg"
"github.com/na4ma4/go-permbits"
)
// Archive creates the archive artifacts.
func (Build) Archive(ctx context.Context) error {
dyndep.CtxDeps(ctx, dyndep.Build)
mg.CtxDeps(ctx, Build.Release)
paths := helper.MustCommandPaths()
helper.MustMakeDir(helper.MustGetArtifactPath("archives"), permbits.MustString("ug=rwx,o=rx"))
for _, cmdPath := range paths {
ct := helper.NewCommandTemplate(false, cmdPath)
if err := buildPlatformIterator(ctx, ct, func(_ context.Context, ct *helper.CommandTemplate) error {
// buildArtifact(ctx, ct)
loga.PrintInfo("Create Archive for %s/%s (%s)",
ct.GoOS, ct.GoArch,
ct.OutputArtifact,
)
if err := buildTar(ct); err != nil {
return err
}
return nil
}); err != nil {
return err
}
}
return nil
}
func buildTar(ct *helper.CommandTemplate) error {
fileName := fmt.Sprintf("%s_%s_%s.tar.gz", ct.CommandName, ct.GoOS, ct.GoArch)
if ct.GoArm != "" {
fileName = fmt.Sprintf("%s_%s_%s_%s.tar.gz", ct.CommandName, ct.GoOS, ct.GoArch, ct.GoArm)
}
archivePath := helper.MustGetArtifactPath("archives", fileName)
var f *os.File
{
var err error
f, err = os.Create(archivePath)
if err != nil {
return err
}
}
defer f.Close()
gzipWriter := gzip.NewWriter(f)
defer gzipWriter.Close()
tarWriter := tar.NewWriter(gzipWriter)
defer tarWriter.Close()
if readmeFile := helper.MustGetWD("README.md"); helper.FileExists(readmeFile) {
if err := tarAddFile(tarWriter, readmeFile, permbits.MustString("ugo=r")); err != nil {
return err
}
}
if binFile := ct.OutputArtifact; helper.FileExists(binFile) {
if err := tarAddFile(tarWriter, binFile, permbits.MustString("ug=rwx,o=rx")); err != nil {
return err
}
}
return nil
}
func tarAddFile(tarWriter *tar.Writer, filename string, mode os.FileMode) error {
var src *os.File
{
var err error
src, err = os.Open(filename)
if err != nil {
return fmt.Errorf("unable to open source file[%s]: %w", filename, err)
}
}
var srcStat os.FileInfo
{
var err error
srcStat, err = src.Stat()
if err != nil {
return fmt.Errorf("unable to stat source file[%s]: %w", filename, err)
}
}
hdr := &tar.Header{
Name: filepath.Base(filename),
Mode: int64(mode),
Size: srcStat.Size(),
ModTime: helper.GitCommitTime(),
}
if err := tarWriter.WriteHeader(hdr); err != nil {
return fmt.Errorf("unable to write tar header for file[%s]: %w", filename, err)
}
if _, err := io.Copy(tarWriter, src); err != nil {
return fmt.Errorf("unable to write contents to tar for file[%s]: %w", filename, err)
}
return nil
}