init
This commit is contained in:
24 files changed
+3822
No files matched your search
@@ -0,0 +1,356 @@
|
||||
// i-am-gcroot lists, filters and deletes Nix garbage collector roots, from a
|
||||
// TUI, an optional web UI, or the command line.
|
||||
package main
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"context"
|
||||
"encoding/json"
|
||||
"flag"
|
||||
"fmt"
|
||||
"os"
|
||||
"os/signal"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"syscall"
|
||||
"text/tabwriter"
|
||||
"time"
|
||||
|
||||
tea "github.com/charmbracelet/bubbletea"
|
||||
|
||||
"github.com/illustris/i-am-gcroot/internal/filter"
|
||||
"github.com/illustris/i-am-gcroot/internal/roots"
|
||||
"github.com/illustris/i-am-gcroot/internal/tui"
|
||||
"github.com/illustris/i-am-gcroot/internal/web"
|
||||
)
|
||||
|
||||
var version = "dev"
|
||||
|
||||
const usage = `i-am-gcroot: list, filter and delete Nix GC roots
|
||||
|
||||
Usage:
|
||||
i-am-gcroot [tui] [-q QUERY] [--web] [--addr ADDR] [--runtime] [--force]
|
||||
i-am-gcroot list [-q QUERY] [--kind K,..] [--sort KEY] [--desc] [--sizes] [--json] [--runtime] [--all]
|
||||
i-am-gcroot rm [-n] [-y] [-c] [--force] [--all] (-q QUERY | LINK...)
|
||||
i-am-gcroot gc
|
||||
i-am-gcroot web [--addr ADDR] [--no-auth] [--force]
|
||||
i-am-gcroot version
|
||||
|
||||
Query syntax (terms ANDed, prefix - to negate):
|
||||
foo /regex/ kind:indirect,dangling owner:me under:/tmp
|
||||
older:30d newer:2h size>1G size<10M is:protected is:current is:hidden
|
||||
|
||||
rm -c deletes the roots, then garbage collects only the store paths that
|
||||
nothing else keeps alive (much faster than a full gc).
|
||||
|
||||
Nix cache roots (~/.cache/nix/...) are hidden unless --all or is:hidden.
|
||||
Runtime roots (held by running processes) are hidden unless --runtime or
|
||||
kind:runtime is used, and can never be deleted.
|
||||
`
|
||||
|
||||
func main() {
|
||||
ctx, cancel := signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGTERM)
|
||||
defer cancel()
|
||||
|
||||
args := os.Args[1:]
|
||||
cmd := "tui"
|
||||
if len(args) > 0 && !strings.HasPrefix(args[0], "-") {
|
||||
cmd, args = args[0], args[1:]
|
||||
}
|
||||
var err error
|
||||
switch cmd {
|
||||
case "tui":
|
||||
err = runTUI(ctx, args)
|
||||
case "list", "ls":
|
||||
err = runList(ctx, args)
|
||||
case "rm", "delete":
|
||||
err = runRm(ctx, args)
|
||||
case "gc":
|
||||
var s string
|
||||
s, err = roots.GC(ctx, roots.DefaultConfig())
|
||||
if err == nil {
|
||||
fmt.Println(s)
|
||||
}
|
||||
case "web":
|
||||
err = runWeb(ctx, args)
|
||||
case "version":
|
||||
fmt.Println(version)
|
||||
case "help", "-h", "--help":
|
||||
fmt.Print(usage)
|
||||
default:
|
||||
fmt.Fprint(os.Stderr, usage)
|
||||
os.Exit(2)
|
||||
}
|
||||
if err != nil {
|
||||
fmt.Fprintln(os.Stderr, "error:", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
}
|
||||
|
||||
func newFlags(name string) *flag.FlagSet {
|
||||
fs := flag.NewFlagSet(name, flag.ExitOnError)
|
||||
fs.Usage = func() { fmt.Fprint(os.Stderr, usage) }
|
||||
return fs
|
||||
}
|
||||
|
||||
const defaultAddr = "127.0.0.1:8377"
|
||||
|
||||
func runTUI(ctx context.Context, args []string) error {
|
||||
fs := newFlags("tui")
|
||||
q := fs.String("q", "", "initial filter query")
|
||||
withWeb := fs.Bool("web", false, "also serve the web UI")
|
||||
addr := fs.String("addr", defaultAddr, "web UI listen address")
|
||||
noAuth := fs.Bool("no-auth", false, "disable the web UI token")
|
||||
runtime := fs.Bool("runtime", false, "include runtime roots")
|
||||
force := fs.Bool("force", false, "allow deleting protected roots")
|
||||
fs.Parse(args)
|
||||
|
||||
set := roots.NewSet(roots.DefaultConfig())
|
||||
set.SetRuntime(*runtime)
|
||||
opts := tui.Options{Query: *q, Force: *force}
|
||||
if *withWeb {
|
||||
srv, err := web.New(ctx, set, web.Options{Addr: *addr, NoAuth: *noAuth, Force: *force})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
ln, url, err := srv.Listen()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
opts.WebURL = url
|
||||
go srv.Serve(ln)
|
||||
}
|
||||
m := tui.New(ctx, set, opts)
|
||||
_, err := tea.NewProgram(m, tea.WithAltScreen(), tea.WithContext(ctx)).Run()
|
||||
if err == tea.ErrProgramKilled && ctx.Err() != nil {
|
||||
return nil
|
||||
}
|
||||
if *withWeb && opts.WebURL != "" {
|
||||
// keep the token discoverable after the alt screen is gone
|
||||
fmt.Fprintln(os.Stderr, "web UI was at", opts.WebURL)
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
// kindsFlag turns "--kind a,b" into a kind set; empty means the default
|
||||
// (everything except runtime, unless runtime is requested).
|
||||
func kindsFlag(s string, runtime bool) (map[roots.Kind]bool, error) {
|
||||
k := map[roots.Kind]bool{}
|
||||
if s == "" {
|
||||
for _, kind := range roots.AllKinds {
|
||||
k[kind] = kind != roots.KindRuntime || runtime
|
||||
}
|
||||
return k, nil
|
||||
}
|
||||
for _, p := range strings.Split(s, ",") {
|
||||
kind, err := filter.ParseKind(p)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
k[kind] = true
|
||||
}
|
||||
return k, nil
|
||||
}
|
||||
|
||||
// load scans roots and, if sizes is set, waits for closure sizes.
|
||||
func load(ctx context.Context, runtime, sizes bool) (*roots.Set, error) {
|
||||
set := roots.NewSet(roots.DefaultConfig())
|
||||
set.SetRuntime(runtime)
|
||||
if err := set.Refresh(ctx); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
for sizes && set.Status().Sizing {
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
return nil, ctx.Err()
|
||||
case <-time.After(50 * time.Millisecond):
|
||||
}
|
||||
}
|
||||
return set, nil
|
||||
}
|
||||
|
||||
func runList(ctx context.Context, args []string) error {
|
||||
fs := newFlags("list")
|
||||
q := fs.String("q", "", "filter query")
|
||||
kinds := fs.String("kind", "", "comma separated kinds")
|
||||
sortKey := fs.String("sort", "link", "sort key: link, age, size, target, kind")
|
||||
desc := fs.Bool("desc", false, "reverse sort")
|
||||
sizes := fs.Bool("sizes", false, "compute closure sizes (slower)")
|
||||
asJSON := fs.Bool("json", false, "JSON output")
|
||||
runtime := fs.Bool("runtime", false, "include runtime roots")
|
||||
all := fs.Bool("all", false, "include hidden nix cache roots")
|
||||
fs.Parse(args)
|
||||
if fs.NArg() > 0 && *q == "" {
|
||||
*q = strings.Join(fs.Args(), " ")
|
||||
}
|
||||
|
||||
query, err := filter.Parse(*q)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
ks, err := kindsFlag(*kinds, *runtime || strings.Contains(*q, "runtime"))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
wantRuntime := ks[roots.KindRuntime]
|
||||
needSizes := *sizes || *sortKey == "size" || strings.Contains(*q, "size")
|
||||
set, err := load(ctx, wantRuntime, needSizes)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
rows := filter.Apply(set.Snapshot(), query, ks, *all, filter.ParseSort(*sortKey), *desc)
|
||||
if *asJSON {
|
||||
enc := json.NewEncoder(os.Stdout)
|
||||
enc.SetIndent("", " ")
|
||||
if rows == nil {
|
||||
rows = []roots.Root{}
|
||||
}
|
||||
return enc.Encode(rows)
|
||||
}
|
||||
tw := tabwriter.NewWriter(os.Stdout, 0, 0, 2, ' ', 0)
|
||||
now := time.Now()
|
||||
fmt.Fprintln(tw, "KIND\tOWNER\tAGE\tCLOSURE\tLINK\tTARGET")
|
||||
var total int64
|
||||
for _, r := range rows {
|
||||
size := "-"
|
||||
if needSizes {
|
||||
size = filter.HumanSize(r.ClosureSize)
|
||||
if r.ClosureSize > 0 {
|
||||
total += r.ClosureSize
|
||||
}
|
||||
}
|
||||
target := r.Target
|
||||
if r.Kind == roots.KindRuntime && r.Count > 1 {
|
||||
target = fmt.Sprintf("(%d paths)", r.Count)
|
||||
}
|
||||
kind := string(r.Kind)
|
||||
if r.Protected {
|
||||
kind += "!"
|
||||
}
|
||||
fmt.Fprintf(tw, "%s\t%s\t%s\t%s\t%s\t%s\n", kind, r.Owner, filter.HumanAge(r.ModTime, now), size, r.Link, target)
|
||||
}
|
||||
tw.Flush()
|
||||
if needSizes {
|
||||
fmt.Fprintf(os.Stderr, "%d roots, closures sum to %s (shared paths counted repeatedly)\n", len(rows), filter.HumanSize(total))
|
||||
} else {
|
||||
fmt.Fprintf(os.Stderr, "%d roots\n", len(rows))
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func runRm(ctx context.Context, args []string) error {
|
||||
fs := newFlags("rm")
|
||||
q := fs.String("q", "", "delete every root matching this query")
|
||||
dry := fs.Bool("n", false, "dry run")
|
||||
yes := fs.Bool("y", false, "do not ask for confirmation")
|
||||
force := fs.Bool("force", false, "allow deleting protected roots")
|
||||
collect := fs.Bool("c", false, "garbage collect the paths only these roots kept alive")
|
||||
all := fs.Bool("all", false, "let -q match hidden nix cache roots")
|
||||
fs.Parse(args)
|
||||
if *q == "" && fs.NArg() == 0 {
|
||||
return fmt.Errorf("rm needs -q QUERY or link paths")
|
||||
}
|
||||
|
||||
set, err := load(ctx, false, false)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
var targets []roots.Root
|
||||
if *q != "" {
|
||||
query, err := filter.Parse(*q)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
ks, _ := kindsFlag("", false)
|
||||
targets = filter.Apply(set.Snapshot(), query, ks, *all, filter.SortLink, false)
|
||||
}
|
||||
if fs.NArg() > 0 {
|
||||
var links []string
|
||||
for _, a := range fs.Args() {
|
||||
if abs, err := filepath.Abs(a); err == nil {
|
||||
a = abs
|
||||
}
|
||||
links = append(links, a)
|
||||
}
|
||||
found := set.Lookup(links)
|
||||
if len(found) != len(links) {
|
||||
have := map[string]bool{}
|
||||
for _, r := range found {
|
||||
have[r.Link] = true
|
||||
}
|
||||
for _, l := range links {
|
||||
if !have[l] {
|
||||
return fmt.Errorf("%s is not a known GC root", l)
|
||||
}
|
||||
}
|
||||
}
|
||||
targets = append(targets, found...)
|
||||
}
|
||||
if len(targets) == 0 {
|
||||
fmt.Fprintln(os.Stderr, "nothing matched")
|
||||
return nil
|
||||
}
|
||||
|
||||
if !*dry && !*yes {
|
||||
for _, r := range targets {
|
||||
fmt.Fprintf(os.Stderr, " %-9s %s\n", r.Kind, r.Link)
|
||||
}
|
||||
fmt.Fprintf(os.Stderr, "Delete %d root(s)? [y/N] ", len(targets))
|
||||
line, _ := bufio.NewReader(os.Stdin).ReadString('\n')
|
||||
if a := strings.TrimSpace(strings.ToLower(line)); a != "y" && a != "yes" {
|
||||
return fmt.Errorf("aborted")
|
||||
}
|
||||
}
|
||||
failed := 0
|
||||
results := roots.Delete(ctx, set.Cfg, targets, roots.DeleteOpts{DryRun: *dry, Force: *force})
|
||||
for _, res := range results {
|
||||
switch {
|
||||
case !res.OK():
|
||||
failed++
|
||||
fmt.Printf("fail %s: %s\n", res.Link, res.Err)
|
||||
case *dry:
|
||||
fmt.Printf("would %s\n", res.Action)
|
||||
default:
|
||||
fmt.Printf("ok %s\n", res.Action)
|
||||
}
|
||||
}
|
||||
if *collect && !*dry {
|
||||
c, err := roots.Collect(ctx, set.Cfg, roots.CollectedTargets(results))
|
||||
fmt.Println(c)
|
||||
for _, p := range c.Kept {
|
||||
fmt.Printf("kept %s (still alive)\n", p)
|
||||
}
|
||||
if err != nil {
|
||||
return fmt.Errorf("gc: %w", err)
|
||||
}
|
||||
}
|
||||
if failed > 0 {
|
||||
return fmt.Errorf("%d of %d deletions failed", failed, len(targets))
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func runWeb(ctx context.Context, args []string) error {
|
||||
fs := newFlags("web")
|
||||
addr := fs.String("addr", defaultAddr, "listen address")
|
||||
noAuth := fs.Bool("no-auth", false, "disable the access token")
|
||||
force := fs.Bool("force", false, "allow deleting protected roots")
|
||||
runtime := fs.Bool("runtime", false, "include runtime roots")
|
||||
fs.Parse(args)
|
||||
|
||||
set := roots.NewSet(roots.DefaultConfig())
|
||||
set.SetRuntime(*runtime)
|
||||
if err := set.Refresh(ctx); err != nil {
|
||||
return err
|
||||
}
|
||||
srv, err := web.New(ctx, set, web.Options{Addr: *addr, NoAuth: *noAuth, Force: *force})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
ln, url, err := srv.Listen()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
fmt.Println("serving", url)
|
||||
return srv.Serve(ln)
|
||||
}
|
||||
Reference in new issue
Block a user