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
|
package mktarball
import (
"archive/tar"
"compress/gzip"
"fmt"
"io"
"log/slog"
"os"
"path/filepath"
"runtime"
"within.website/x/cmd/yeet/internal"
"within.website/x/cmd/yeet/internal/pkgmeta"
)
func Build(p pkgmeta.Package) (foutpath string, err error) {
defer func() {
if r := recover(); r != nil {
switch r.(type) {
case error:
err = r.(error)
slog.Error("mkrpm: error while building", "err", err)
default:
err = fmt.Errorf("%v", r)
slog.Error("mkrpm: error while building", "err", err)
}
}
}()
os.MkdirAll("./var", 0755)
os.WriteFile(filepath.Join("./var", ".gitignore"), []byte("*\n!.gitignore"), 0644)
if p.Version == "" {
p.Version = internal.GitVersion()
}
if p.Platform == "" {
p.Platform = "linux"
}
dir, err := os.MkdirTemp("", "yeet-mktarball")
if err != nil {
return "", fmt.Errorf("can't make temporary directory")
}
defer os.RemoveAll(dir)
pkgDir := filepath.Join(dir, fmt.Sprintf("%s-%s-%s-%s", p.Name, p.Version, p.Platform, p.Goarch))
os.MkdirAll(pkgDir, 0755)
fname := filepath.Join("var", fmt.Sprintf("%s-%s-%s-%s.tar.gz", p.Name, p.Version, p.Platform, p.Goarch))
fout, err := os.Create(fname)
if err != nil {
return "", fmt.Errorf("can't make output file: %w", err)
}
defer fout.Close()
gw, err := gzip.NewWriterLevel(fout, 9)
if err != nil {
return "", fmt.Errorf("can't make gzip writer: %w", err)
}
defer gw.Close()
tw := tar.NewWriter(gw)
defer tw.Close()
cgoEnabled := os.Getenv("CGO_ENABLED")
defer func() {
os.Setenv("GOARCH", runtime.GOARCH)
os.Setenv("GOOS", runtime.GOOS)
os.Setenv("CGO_ENABLED", cgoEnabled)
}()
os.Setenv("GOARCH", p.Goarch)
os.Setenv("GOOS", p.Platform)
os.Setenv("CGO_ENABLED", "0")
os.WriteFile(filepath.Join(pkgDir, "VERSION"), []byte(p.Version+"\n"), 0666)
bi := pkgmeta.BuildInput{
Output: pkgDir,
Bin: filepath.Join(pkgDir, "bin"),
Doc: filepath.Join(pkgDir, "doc"),
Etc: filepath.Join(pkgDir, "run"),
Man: filepath.Join(pkgDir, "man"),
Systemd: filepath.Join(pkgDir, "run"),
}
p.Build(bi)
for src, dst := range p.ConfigFiles {
if err := Copy(src, filepath.Join(bi.Etc, dst)); err != nil {
return "", fmt.Errorf("can't copy %s to %s: %w", src, dst, err)
}
}
for src, dst := range p.Documentation {
fname := filepath.Join(bi.Doc, dst)
if filepath.Base(fname) == "README.md" {
fname = filepath.Join(pkgDir, "README.md")
}
if err := Copy(src, fname); err != nil {
return "", fmt.Errorf("can't copy %s to %s: %w", src, dst, err)
}
}
root, err := os.OpenRoot(dir)
if err != nil {
return "", fmt.Errorf("can't open root FS %s: %w", dir, err)
}
if err := tw.AddFS(root.FS()); err != nil {
return "", fmt.Errorf("can't copy built files to tarball: %w", err)
}
slog.Info("built package", "name", p.Name, "arch", p.Goarch, "version", p.Version, "path", fout.Name())
return fname, nil
}
// Copy copies the contents of the file at srcpath to a regular file
// at dstpath. If the file named by dstpath already exists, it is
// truncated. The function does not copy the file mode, file
// permission bits, or file attributes.
func Copy(srcpath, dstpath string) (err error) {
r, err := os.Open(srcpath)
if err != nil {
return err
}
defer r.Close() // ignore error: file was opened read-only.
st, err := r.Stat()
if err != nil {
return err
}
os.MkdirAll(filepath.Dir(dstpath), 0755)
w, err := os.Create(dstpath)
if err != nil {
return err
}
if err := w.Chmod(st.Mode()); err != nil {
return err
}
defer func() {
// Report the error, if any, from Close, but do so
// only if there isn't already an outgoing error.
if c := w.Close(); err == nil {
err = c
}
}()
_, err = io.Copy(w, r)
return err
}
|