| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
package main |
| 19 |
|
| 20 |
import ( |
| 21 |
"bytes" |
| 22 |
"flag" |
| 23 |
"fmt" |
| 24 |
"log" |
| 25 |
"os" |
| 26 |
"os/exec" |
| 27 |
"path/filepath" |
| 28 |
"strings" |
| 29 |
|
| 30 |
"golang.org/x/mod/module" |
| 31 |
"golang.org/x/tools/txtar" |
| 32 |
) |
| 33 |
|
| 34 |
func usage() { |
| 35 |
fmt.Fprintf(os.Stderr, "usage: txtar-addmod dir path@version...\n") |
| 36 |
flag.PrintDefaults() |
| 37 |
|
| 38 |
fmt.Fprintf(os.Stderr, ` |
| 39 |
The txtar-addmod command adds a module as a txtar archive to the |
| 40 |
testdata module directory as understood by the goproxytest package |
| 41 |
(see https://godoc.org/github.com/rogpeppe/go-internal/goproxytest). |
| 42 |
|
| 43 |
The dir argument names to directory to add the module to. If dir is "-", |
| 44 |
the result will instead be written to the standard output in a form |
| 45 |
suitable for embedding directly into a testscript txtar file, with each |
| 46 |
file prefixed with the ".gomodproxy" directory. |
| 47 |
|
| 48 |
In general, txtar-addmod is intended to be used only for very small |
| 49 |
modules - we do not want to check very large files into testdata/mod. |
| 50 |
|
| 51 |
It is acceptable to edit the archive afterward to remove or shorten files. |
| 52 |
`) |
| 53 |
os.Exit(2) |
| 54 |
} |
| 55 |
|
| 56 |
var tmpdir string |
| 57 |
|
| 58 |
func fatalf(format string, args ...any) { |
| 59 |
os.RemoveAll(tmpdir) |
| 60 |
log.Fatalf(format, args...) |
| 61 |
} |
| 62 |
|
| 63 |
const goCmd = "go" |
| 64 |
|
| 65 |
var allFiles = flag.Bool("all", false, "include all source files") |
| 66 |
|
| 67 |
func main() { |
| 68 |
flag.Usage = usage |
| 69 |
flag.Parse() |
| 70 |
if flag.NArg() < 2 { |
| 71 |
usage() |
| 72 |
} |
| 73 |
targetDir := flag.Arg(0) |
| 74 |
modules := flag.Args()[1:] |
| 75 |
|
| 76 |
log.SetPrefix("txtar-addmod: ") |
| 77 |
log.SetFlags(0) |
| 78 |
|
| 79 |
var err error |
| 80 |
tmpdir, err = os.MkdirTemp("", "txtar-addmod-") |
| 81 |
if err != nil { |
| 82 |
log.Fatal(err) |
| 83 |
} |
| 84 |
|
| 85 |
run := func(command string, args ...string) string { |
| 86 |
cmd := exec.Command(command, args...) |
| 87 |
cmd.Dir = tmpdir |
| 88 |
var stderr bytes.Buffer |
| 89 |
cmd.Stderr = &stderr |
| 90 |
out, err := cmd.Output() |
| 91 |
if err != nil { |
| 92 |
fatalf("%s %s: %v\n%s", command, strings.Join(args, " "), err, stderr.Bytes()) |
| 93 |
} |
| 94 |
return string(out) |
| 95 |
} |
| 96 |
|
| 97 |
gopath := strings.TrimSpace(run("go", "env", "GOPATH")) |
| 98 |
if gopath == "" { |
| 99 |
fatalf("cannot find GOPATH") |
| 100 |
} |
| 101 |
|
| 102 |
exitCode := 0 |
| 103 |
for _, arg := range modules { |
| 104 |
if err := os.WriteFile(filepath.Join(tmpdir, "go.mod"), []byte("module m\n"), 0o666); err != nil { |
| 105 |
fatalf("%v", err) |
| 106 |
} |
| 107 |
run(goCmd, "get", "-d", arg) |
| 108 |
path := arg |
| 109 |
if i := strings.Index(path, "@"); i >= 0 { |
| 110 |
path = path[:i] |
| 111 |
} |
| 112 |
out := run(goCmd, "list", "-m", "-f={{.Path}} {{.Version}} {{.Dir}}", path) |
| 113 |
f := strings.Fields(out) |
| 114 |
if len(f) != 3 { |
| 115 |
log.Printf("go list -m %s: unexpected output %q", arg, out) |
| 116 |
exitCode = 1 |
| 117 |
continue |
| 118 |
} |
| 119 |
path, vers, dir := f[0], f[1], f[2] |
| 120 |
|
| 121 |
encpath, err := module.EscapePath(path) |
| 122 |
if err != nil { |
| 123 |
log.Printf("failed to encode path %q: %v", path, err) |
| 124 |
continue |
| 125 |
} |
| 126 |
path = encpath |
| 127 |
|
| 128 |
mod, err := os.ReadFile(filepath.Join(gopath, "pkg/mod/cache/download", path, "@v", vers+".mod")) |
| 129 |
if err != nil { |
| 130 |
log.Printf("%s: %v", arg, err) |
| 131 |
exitCode = 1 |
| 132 |
continue |
| 133 |
} |
| 134 |
info, err := os.ReadFile(filepath.Join(gopath, "pkg/mod/cache/download", path, "@v", vers+".info")) |
| 135 |
if err != nil { |
| 136 |
log.Printf("%s: %v", arg, err) |
| 137 |
exitCode = 1 |
| 138 |
continue |
| 139 |
} |
| 140 |
|
| 141 |
a := new(txtar.Archive) |
| 142 |
title := arg |
| 143 |
if !strings.Contains(arg, "@") { |
| 144 |
title += "@" + vers |
| 145 |
} |
| 146 |
dir = filepath.Clean(dir) |
| 147 |
modDir := strings.ReplaceAll(path, "/", "_") + "_" + vers |
| 148 |
filePrefix := "" |
| 149 |
if targetDir == "-" { |
| 150 |
filePrefix = ".gomodproxy/" + modDir + "/" |
| 151 |
} else { |
| 152 |
|
| 153 |
a.Comment = fmt.Appendf(nil, "module %s\n\n", title) |
| 154 |
} |
| 155 |
a.Files = []txtar.File{ |
| 156 |
{Name: filePrefix + ".mod", Data: mod}, |
| 157 |
{Name: filePrefix + ".info", Data: info}, |
| 158 |
} |
| 159 |
err = filepath.Walk(dir, func(path string, info os.FileInfo, err error) error { |
| 160 |
if err != nil { |
| 161 |
return err |
| 162 |
} |
| 163 |
if !info.Mode().IsRegular() { |
| 164 |
return nil |
| 165 |
} |
| 166 |
|
| 167 |
|
| 168 |
name := info.Name() |
| 169 |
switch { |
| 170 |
case *allFiles: |
| 171 |
case name == "go.mod": |
| 172 |
case strings.HasSuffix(name, ".go"): |
| 173 |
default: |
| 174 |
|
| 175 |
|
| 176 |
return nil |
| 177 |
} |
| 178 |
data, err := os.ReadFile(path) |
| 179 |
if err != nil { |
| 180 |
return err |
| 181 |
} |
| 182 |
a.Files = append(a.Files, txtar.File{ |
| 183 |
Name: filePrefix + strings.TrimPrefix(path, dir+string(filepath.Separator)), |
| 184 |
Data: data, |
| 185 |
}) |
| 186 |
return nil |
| 187 |
}) |
| 188 |
if err != nil { |
| 189 |
log.Printf("%s: %v", arg, err) |
| 190 |
exitCode = 1 |
| 191 |
continue |
| 192 |
} |
| 193 |
|
| 194 |
data := txtar.Format(a) |
| 195 |
if targetDir == "-" { |
| 196 |
if _, err := os.Stdout.Write(data); err != nil { |
| 197 |
log.Printf("cannot write output: %v", err) |
| 198 |
exitCode = 1 |
| 199 |
break |
| 200 |
} |
| 201 |
} else { |
| 202 |
if err := os.WriteFile(filepath.Join(targetDir, modDir+".txtar"), data, 0o666); err != nil { |
| 203 |
log.Printf("%s: %v", arg, err) |
| 204 |
exitCode = 1 |
| 205 |
continue |
| 206 |
} |
| 207 |
} |
| 208 |
} |
| 209 |
os.RemoveAll(tmpdir) |
| 210 |
os.Exit(exitCode) |
| 211 |
} |