Skip to content

Commit bb30e2a

Browse files
authored
cmd/txtar-addmod: allow stdout destination (#61)
This makes it easier to produce inline module defintions for inclusion in txtar files.
1 parent 8969b75 commit bb30e2a

File tree

2 files changed

+62
-10
lines changed

2 files changed

+62
-10
lines changed

cmd/txtar-addmod/addmod.go

Lines changed: 43 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,22 @@ import (
3636
func 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)
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
unquote expect
2+
txtar-addmod - github.com/gobin-testrepos/simple-main
3+
cmp stdout expect
4+
! stderr .+
5+
-- expect --
6+
>-- .gomodproxy/github.com_gobin-testrepos_simple-main_v1.0.0/.mod --
7+
>module github.com/gobin-testrepos/simple-main
8+
>-- .gomodproxy/github.com_gobin-testrepos_simple-main_v1.0.0/.info --
9+
>{"Version":"v1.0.0","Time":"2018-10-22T18:45:39Z"}
10+
>-- .gomodproxy/github.com_gobin-testrepos_simple-main_v1.0.0/go.mod --
11+
>module github.com/gobin-testrepos/simple-main
12+
>-- .gomodproxy/github.com_gobin-testrepos_simple-main_v1.0.0/main.go --
13+
>package main
14+
>
15+
>import "fmt"
16+
>
17+
>func main() {
18+
> fmt.Println("I am a simple module-based main")
19+
>}

0 commit comments

Comments
 (0)