Skip to content

Commit

Permalink
cmd/txtar-addmod: -all flag to include all files
Browse files Browse the repository at this point in the history
This can be useful if certain extra files are needed to pass some tests,
for example.

Added a simple test case too.

Fixes #28.
  • Loading branch information
mvdan committed Dec 2, 2018
1 parent 3da46a1 commit 3f553c3
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 6 deletions.
24 changes: 18 additions & 6 deletions cmd/txtar-addmod/addmod.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import (

func usage() {
fmt.Fprintf(os.Stderr, "usage: txtar-addmod dir path@version...\n")
flag.PrintDefaults()
os.Exit(2)
}

Expand All @@ -51,6 +52,8 @@ func main() {
os.Exit(main1())
}

var allFiles = flag.Bool("all", false, "include all source files")

func main1() int {
flag.Usage = usage
flag.Parse()
Expand Down Expand Up @@ -140,14 +143,23 @@ func main1() int {
if !info.Mode().IsRegular() {
return nil
}
// TODO: skip dirs like "testdata" or "_foo" unless -all
// is given?
name := info.Name()
if name == "go.mod" || strings.HasSuffix(name, ".go") {
data, err := ioutil.ReadFile(path)
if err != nil {
return err
}
a.Files = append(a.Files, txtar.File{Name: strings.TrimPrefix(path, dir+string(filepath.Separator)), Data: data})
switch {
case *allFiles:
case name == "go.mod":
case strings.HasSuffix(name, ".go"):
default:
// the name is not in the whitelist, and we're
// not including all files via -all
return nil
}
data, err := ioutil.ReadFile(path)
if err != nil {
return err
}
a.Files = append(a.Files, txtar.File{Name: strings.TrimPrefix(path, dir+string(filepath.Separator)), Data: data})
return nil
})
if err != nil {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,6 @@ import "fmt"
func main() {
fmt.Println("I am a simple module-based main")
}
-- foobar --
This is an extra file that can't be imported with the Go package, but may still
be used in a test.
4 changes: 4 additions & 0 deletions cmd/txtar-addmod/testdata/txtar-addmod-self.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,7 @@ txtar-addmod $WORK/out github.com/gobin-testrepos/simple-main
! stdout .+
! stderr .+
exists $WORK/out/github.com_gobin-testrepos_simple-main_v1.0.0.txt
! grep foobar $WORK/out/github.com_gobin-testrepos_simple-main_v1.0.0.txt

txtar-addmod -all $WORK/out github.com/gobin-testrepos/simple-main
grep '-- foobar --' $WORK/out/github.com_gobin-testrepos_simple-main_v1.0.0.txt

0 comments on commit 3f553c3

Please sign in to comment.