-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
110 lines (91 loc) · 2.8 KB
/
Copy pathmain.go
File metadata and controls
110 lines (91 loc) · 2.8 KB
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
package main
import (
"context"
"log"
"log/slog"
"os"
"os/exec"
"strings"
"sync"
"github.com/alecthomas/kong"
"github.com/AlekSi/hardcache/internal/commands"
"github.com/AlekSi/hardcache/internal/sigterm"
"github.com/AlekSi/hardcache/internal/unit"
)
// GOCACHE returns the Go build cache directory.
var GOCACHE = sync.OnceValue(func() string {
// in theory, someone might not have go in the PATH
if v := os.Getenv("GOCACHE"); v != "" {
return v
}
// that handles `go env -w` and the default value
b, err := exec.Command("go", "env", "GOCACHE").Output()
if err != nil {
panic(err)
}
return strings.TrimSpace(string(b))
})
// cli represents CLI arguments and flags.
//
//nolint:vet // for readability
var cli struct {
Local struct {
Dir string `default:"${local_dir_default}" type:"path" help:"Directory to use."`
Trim struct {
UnusedFor unit.Duration `default:"5d" help:"${local_unused_for_help}"`
MaxSize string `default:"0GB" help:"${local_max_size_help}"`
} `cmd:"" help:"Trim local cache."`
Trimd struct {
UnusedFor unit.Duration `default:"5d" help:"${local_unused_for_help}"`
MaxSize string `default:"0GB" help:"${local_max_size_help}"`
Interval unit.Duration `short:"i" default:"1h" help:"Interval between trimmings."`
} `cmd:"" help:"Trim local cache continuously."`
} `cmd:""`
Debug bool `help:"Enable debug logging."`
}
func main() {
opts := []kong.Option{
kong.Name("hardcache"),
kong.Description("Tool for managing the Go build cache."),
kong.Vars{
"local_dir_default": GOCACHE(),
"local_unused_for_help": "Always remove entries unused for this duration. Pass 0 to disable.",
"local_max_size_help": "Remove entries, starting from least recently used, " +
"if cache size is larger than this value. " +
"Supports MiB, GB, etc. suffixes, or percentage of the total disk space (e.g., 5%). " +
"Pass 0 to disable.",
},
kong.ConfigureHelp(kong.HelpOptions{
Tree: true,
WrapUpperBound: 120,
}),
}
kongCtx := kong.Parse(&cli, opts...)
log.SetPrefix("hardcache: ")
log.SetFlags(log.Lmicroseconds)
if cli.Debug {
slog.SetLogLoggerLevel(slog.LevelDebug)
}
l := slog.Default()
ctx, cancel := sigterm.Ctx(context.Background())
defer cancel()
var err error
switch kongCtx.Command() {
case "local trim":
err = commands.LocalTrim(&commands.LocalTrimOpts{
Dir: cli.Local.Dir,
UnusedFor: cli.Local.Trim.UnusedFor,
MaxSize: cli.Local.Trim.MaxSize,
}, l)
case "local trimd":
err = commands.LocalTrimd(ctx, &commands.LocalTrimdOpts{
Dir: cli.Local.Dir,
UnusedFor: cli.Local.Trimd.UnusedFor,
MaxSize: cli.Local.Trimd.MaxSize,
Interval: cli.Local.Trimd.Interval,
}, l)
default:
kongCtx.Fatalf("unknown command: %q", kongCtx.Command())
}
kongCtx.FatalIfErrorf(err)
}