| 1 | // Copyright 2024 The Go Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style |
| 3 | // license that can be found in the LICENSE file. |
| 4 | |
| 5 | // A command for building and maintaining the module cache |
| 6 | // a.out <flags> <command> <args> |
| 7 | // The commands are: |
| 8 | // 'update', which attempts to update an existing index, |
| 9 | // 'query', which looks up things in the index. |
| 10 | // 'clean', which remove obsolete index files. |
| 11 | // If the command is invoked with no arguments, it defaults to 'update'. |
| 12 | package main |
| 13 | |
| 14 | import ( |
| 15 | "bytes" |
| 16 | "flag" |
| 17 | "fmt" |
| 18 | "log" |
| 19 | "os" |
| 20 | "os/exec" |
| 21 | "path/filepath" |
| 22 | "strings" |
| 23 | "time" |
| 24 | |
| 25 | "golang.org/x/tools/internal/modindex" |
| 26 | ) |
| 27 | |
| 28 | var verbose = flag.Int("v", 0, "how much information to print") |
| 29 | |
| 30 | type cmd struct { |
| 31 | name string |
| 32 | f func(string) |
| 33 | doc string |
| 34 | } |
| 35 | |
| 36 | var cmds = []cmd{ |
| 37 | {"update", update, "if there is an existing index of GOMODCACHE, update it. Otherwise create one."}, |
| 38 | {"clean", clean, "removed unreferenced indexes more than an hour old"}, |
| 39 | {"query", query, "not yet implemented"}, |
| 40 | } |
| 41 | |
| 42 | func goEnv(s string) string { |
| 43 | out, err := exec.Command("go", "env", s).Output() |
| 44 | if err != nil { |
| 45 | return "" |
| 46 | } |
| 47 | out = bytes.TrimSpace(out) |
| 48 | return string(out) |
| 49 | } |
| 50 | |
| 51 | func main() { |
| 52 | flag.Parse() |
| 53 | log.SetFlags(log.Lshortfile) |
| 54 | gomodcache := goEnv("GOMODCACHE") |
| 55 | if gomodcache == "" { |
| 56 | log.Fatal("can't find GOMODCACHE") |
| 57 | } |
| 58 | if flag.NArg() == 0 { |
| 59 | update(gomodcache) |
| 60 | return |
| 61 | } |
| 62 | for _, c := range cmds { |
| 63 | if flag.Arg(0) == c.name { |
| 64 | c.f(gomodcache) |
| 65 | return |
| 66 | } |
| 67 | } |
| 68 | flag.Usage() |
| 69 | } |
| 70 | |
| 71 | func init() { |
| 72 | var sb strings.Builder |
| 73 | fmt.Fprintf(&sb, "usage:\n") |
| 74 | for _, c := range cmds { |
| 75 | fmt.Fprintf(&sb, "'%s': %s\n", c.name, c.doc) |
| 76 | } |
| 77 | msg := sb.String() |
| 78 | flag.Usage = func() { |
| 79 | fmt.Fprint(os.Stderr, msg) |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | func update(dir string) { |
| 84 | if _, err := modindex.Update(dir); err != nil { |
| 85 | log.Print(err) |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | func query(dir string) { |
| 90 | panic("implement") |
| 91 | } |
| 92 | |
| 93 | func clean(_ string) { |
| 94 | des := modindex.IndexDir |
| 95 | // look at the files starting with 'index' |
| 96 | // the current ones of each version are pointed to by |
| 97 | // index-name-%d files. Any others more than an hour old |
| 98 | // are deleted. |
| 99 | dis, err := os.ReadDir(des) |
| 100 | if err != nil { |
| 101 | log.Fatal(err) |
| 102 | } |
| 103 | cutoff := time.Now().Add(-time.Hour) |
| 104 | var inames []string // older files named index* |
| 105 | curnames := make(map[string]bool) // current versions of index (different CurrentVersion) |
| 106 | for _, de := range dis { |
| 107 | if !strings.HasPrefix(de.Name(), "index") { |
| 108 | continue |
| 109 | } |
| 110 | if strings.HasPrefix(de.Name(), "index-name-") { |
| 111 | buf, err := os.ReadFile(filepath.Join(des, de.Name())) |
| 112 | if err != nil { |
| 113 | log.Print(err) |
| 114 | continue |
| 115 | } |
| 116 | curnames[string(buf)] = true |
| 117 | if *verbose > 1 { |
| 118 | log.Printf("latest index is %s", string(buf)) |
| 119 | } |
| 120 | } |
| 121 | info, err := de.Info() |
| 122 | if err != nil { |
| 123 | log.Print(err) |
| 124 | continue |
| 125 | } |
| 126 | if info.ModTime().Before(cutoff) && !strings.HasPrefix(de.Name(), "index-name-") { |
| 127 | // add to the list of files to be removed. index-name-%d files are never removed |
| 128 | inames = append(inames, de.Name()) |
| 129 | if *verbose > 0 { |
| 130 | log.Printf("%s:%s", de.Name(), cutoff.Sub(info.ModTime())) |
| 131 | } |
| 132 | } |
| 133 | } |
| 134 | for _, nm := range inames { |
| 135 | if curnames[nm] { |
| 136 | continue |
| 137 | } |
| 138 | err := os.Remove(filepath.Join(des, nm)) |
| 139 | if err != nil && *verbose > 0 { |
| 140 | log.Printf("%s not removed (%v)", nm, err) |
| 141 | } |
| 142 | } |
| 143 | } |