-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcleanup.go
59 lines (54 loc) · 1.68 KB
/
cleanup.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
package main
import (
"io/fs"
"log/slog"
"os"
"path/filepath"
"strings"
"time"
)
func cleanupBinariesAtime(atimeAge time.Duration) {
dir := filepath.Join(config.DataDir, "result")
err := filepath.WalkDir(dir, func(path string, d fs.DirEntry, err error) error {
if err != nil {
slog.Error("cleanup binaries: walking", "err", err, "path", path)
return nil
}
if d.Name() != "binary.gz" {
return nil
}
if fi, err := d.Info(); err != nil {
slog.Error("cleanup binaries: stat", "err", err, "path", path)
} else if t, err := atime(fi); err != nil {
slog.Error("cleanup binaries: get access time", "err", err, "path", path)
} else if time.Since(t) > atimeAge {
if err := os.Remove(path); err != nil {
slog.Error("cleanup binaries: removing old binary", "err", err, "path", path)
} else {
slog.Info("cleanup binaries: removed aging binary", "path", path)
}
}
return nil
})
if err != nil {
slog.Error("walking result directory for old binary.gz files", "err", err)
}
}
func cleanupGoBuildCache() {
slog.Debug("clearing go build cache")
goversion, err := ensureMostRecentSDK()
if err != nil {
slog.Error("cleaning up go build cache: ensuring most recent toolchain while resolving module version", "err", err)
return
}
gobin, err := ensureGobin(goversion.String())
if err != nil {
slog.Error("cleaning up go build cache: ensuring go version is available while resolving module version", "err", err)
return
}
cmd := makeCommand(goversion.String(), false, emptyDir, false, nil, gobin, "clean", "-cache")
output, err := cmd.Output()
if err != nil {
slog.Error("running go clean -cache", "err", err, "output", strings.TrimSpace(string(output)))
}
}