@@ -36,6 +36,22 @@ import (
3636func usage () {
3737 fmt .Fprintf (os .Stderr , "usage: txtar-addmod dir path@version...\n " )
3838 flag .PrintDefaults ()
39+
40+ fmt .Fprintf (os .Stderr , `
41+ The txtar-addmod command adds a module as a txtar archive to the
42+ testdata module directory as understood by the goproxytest package
43+ (see https://godoc.org/github.com/rogpeppe/go-internal/goproxytest).
44+
45+ The dir argument names to directory to add the module to. If dir is "-",
46+ the result will instead be written to the standard output in a form
47+ suitable for embedding directly into a testscript txtar file, with each
48+ file prefixed with the ".gomodproxy" directory.
49+
50+ In general, txtar-addmod is intended to be used only for very small
51+ modules - we do not want to check very large files into testdata/mod.
52+
53+ It is acceptable to edit the archive afterward to remove or shorten files.
54+ ` )
3955 os .Exit (2 )
4056}
4157
@@ -133,12 +149,19 @@ func main1() int {
133149 if ! strings .Contains (arg , "@" ) {
134150 title += "@" + vers
135151 }
136- a .Comment = []byte (fmt .Sprintf ("module %s\n \n " , title ))
152+ dir = filepath .Clean (dir )
153+ modDir := strings .Replace (path , "/" , "_" , - 1 ) + "_" + vers
154+ filePrefix := ""
155+ if targetDir == "-" {
156+ filePrefix = ".gomodproxy/" + modDir + "/"
157+ } else {
158+ // No comment if we're writing to stdout.
159+ a .Comment = []byte (fmt .Sprintf ("module %s\n \n " , title ))
160+ }
137161 a .Files = []txtar.File {
138- {Name : ".mod" , Data : mod },
139- {Name : ".info" , Data : info },
162+ {Name : filePrefix + ".mod" , Data : mod },
163+ {Name : filePrefix + ".info" , Data : info },
140164 }
141- dir = filepath .Clean (dir )
142165 err = filepath .Walk (dir , func (path string , info os.FileInfo , err error ) error {
143166 if ! info .Mode ().IsRegular () {
144167 return nil
@@ -159,7 +182,10 @@ func main1() int {
159182 if err != nil {
160183 return err
161184 }
162- a .Files = append (a .Files , txtar.File {Name : strings .TrimPrefix (path , dir + string (filepath .Separator )), Data : data })
185+ a .Files = append (a .Files , txtar.File {
186+ Name : filePrefix + strings .TrimPrefix (path , dir + string (filepath .Separator )),
187+ Data : data ,
188+ })
163189 return nil
164190 })
165191 if err != nil {
@@ -169,11 +195,18 @@ func main1() int {
169195 }
170196
171197 data := txtar .Format (a )
172- target := filepath .Join (targetDir , strings .Replace (path , "/" , "_" , - 1 )+ "_" + vers + ".txt" )
173- if err := ioutil .WriteFile (target , data , 0666 ); err != nil {
174- log .Printf ("%s: %v" , arg , err )
175- exitCode = 1
176- continue
198+ if targetDir == "-" {
199+ if _ , err := os .Stdout .Write (data ); err != nil {
200+ log .Printf ("cannot write output: %v" , err )
201+ exitCode = 1
202+ break
203+ }
204+ } else {
205+ if err := ioutil .WriteFile (filepath .Join (targetDir , modDir + ".txt" ), data , 0666 ); err != nil {
206+ log .Printf ("%s: %v" , arg , err )
207+ exitCode = 1
208+ continue
209+ }
177210 }
178211 }
179212 os .RemoveAll (tmpdir )
0 commit comments