Skip to content

Commit 8969b75

Browse files
authored
cmd/txtar-savedir: add -a flag and rename to txtar-c (#62)
I could never remember the txtar-savedir name and `txtar-c` seems more reminiscent of the `tar` original. Also it's sometimes useful to be able to create txtar files containing dot files (particularly `.gomodproxy` directories) so allow that too.
1 parent a760d4f commit 8969b75

File tree

7 files changed

+77
-18
lines changed

7 files changed

+77
-18
lines changed

cmd/txtar-savedir/savedir.go renamed to cmd/txtar-c/savedir.go

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@
22
// Use of this source code is governed by a BSD-style
33
// license that can be found in the LICENSE file.
44

5-
// The txtar-savedir command archives a directory tree as a txtar archive printed to standard output.
5+
// The txtar-c command archives a directory tree as a txtar archive printed to standard output.
66
//
77
// Usage:
88
//
9-
// txtar-savedir /path/to/dir >saved.txt
9+
// txtar-c /path/to/dir >saved.txt
1010
//
1111
// See https://godoc.org/github.com/rogpeppe/go-internal/txtar for details of the format
1212
// and how to parse a txtar file.
@@ -30,11 +30,14 @@ import (
3030
var flag = stdflag.NewFlagSet(os.Args[0], stdflag.ContinueOnError)
3131

3232
func usage() {
33-
fmt.Fprintf(os.Stderr, "usage: savedir dir >saved.txt\n")
33+
fmt.Fprintf(os.Stderr, "usage: txtar-c dir >saved.txt\n")
3434
flag.PrintDefaults()
3535
}
3636

37-
var quoteFlag = flag.Bool("quote", false, "quote files that contain txtar file markers instead of failing")
37+
var (
38+
quoteFlag = flag.Bool("quote", false, "quote files that contain txtar file markers instead of failing")
39+
allFlag = flag.Bool("a", false, "include dot files too")
40+
)
3841

3942
func main() {
4043
os.Exit(main1())
@@ -50,7 +53,7 @@ func main1() int {
5053
return 2
5154
}
5255

53-
log.SetPrefix("txtar-savedir: ")
56+
log.SetPrefix("txtar-c: ")
5457
log.SetFlags(0)
5558

5659
dir := flag.Arg(0)
@@ -62,7 +65,7 @@ func main1() int {
6265
return nil
6366
}
6467
name := info.Name()
65-
if strings.HasPrefix(name, ".") {
68+
if strings.HasPrefix(name, ".") && !*allFlag {
6669
if info.IsDir() {
6770
return filepath.SkipDir
6871
}
@@ -76,7 +79,7 @@ func main1() int {
7679
log.Fatal(err)
7780
}
7881
if !utf8.Valid(data) {
79-
log.Printf("%s: ignoring invalid UTF-8 data", path)
82+
log.Printf("%s: ignoring file with invalid UTF-8 data", path)
8083
return nil
8184
}
8285
if len(data) > 0 && !bytes.HasSuffix(data, []byte("\n")) {
@@ -97,7 +100,7 @@ func main1() int {
97100
a.Comment = append(a.Comment, []byte("unquote "+filename+"\n")...)
98101
}
99102
a.Files = append(a.Files, txtar.File{
100-
Name: filename,
103+
Name: filepath.ToSlash(filename),
101104
Data: data,
102105
})
103106
return nil

cmd/txtar-savedir/script_test.go renamed to cmd/txtar-c/script_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import (
1313

1414
func TestMain(m *testing.M) {
1515
os.Exit(testscript.RunMain(m, map[string]func() int{
16-
"txtar-savedir": main1,
16+
"txtar-c": main1,
1717
}))
1818
}
1919

cmd/txtar-c/testdata/all.txt

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
unquote expect-all
2+
unquote expect-no-all
3+
4+
# Without the -a flag, it should ignore . files.
5+
txtar-c blah
6+
! stderr .+
7+
cmp stdout expect-no-all
8+
9+
# With the -a flag, it should include them.
10+
txtar-c -a blah
11+
! stderr .+
12+
cmp stdout expect-all
13+
14+
-- blah/.foo/foo --
15+
foo
16+
-- blah/.other --
17+
other
18+
-- blah/go.mod --
19+
module example.com/blah
20+
21+
-- blah/main.go --
22+
package main
23+
24+
import "fmt"
25+
26+
func main() {
27+
fmt.Println("Hello, world!")
28+
}
29+
-- expect-all --
30+
>-- .foo/foo --
31+
>foo
32+
>-- .other --
33+
>other
34+
>-- go.mod --
35+
>module example.com/blah
36+
>
37+
>-- main.go --
38+
>package main
39+
>
40+
>import "fmt"
41+
>
42+
>func main() {
43+
> fmt.Println("Hello, world!")
44+
>}
45+
-- expect-no-all --
46+
>-- go.mod --
47+
>module example.com/blah
48+
>
49+
>-- main.go --
50+
>package main
51+
>
52+
>import "fmt"
53+
>
54+
>func main() {
55+
> fmt.Println("Hello, world!")
56+
>}

cmd/txtar-savedir/testdata/needquote.txt renamed to cmd/txtar-c/testdata/needquote.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
unquote blah/withsep
22
unquote expect
3-
txtar-savedir blah
4-
stderr 'txtar-savedir: blah.withsep: ignoring file with txtar marker in'
3+
txtar-c blah
4+
stderr 'txtar-c: blah.withsep: ignoring file with txtar marker in'
55
cmp stdout expect
66

77
-- blah/withsep --

cmd/txtar-savedir/testdata/quote.txt renamed to cmd/txtar-c/testdata/quote.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
unquote blah/withsep
22
unquote expect
3-
txtar-savedir -quote blah
3+
txtar-c -quote blah
44
! stderr .+
55
cmp stdout expect
66

cmd/txtar-savedir/testdata/txtar-savedir-self.txt renamed to cmd/txtar-c/testdata/txtar-savedir-self.txt

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,5 @@
1-
# Because of https://github.com/rogpeppe/go-internal/issues/11 we cannot
2-
# define a txtar archive-generated file that is the golden output from txtar-savedir
3-
# So we can't actually test the output for now, instead we just rely on a simple
4-
# stdout check
51
unquote expect
6-
txtar-savedir blah
2+
txtar-c blah
73
! stderr .+
84
cmp stdout expect
95

@@ -18,6 +14,8 @@ import "fmt"
1814
func main() {
1915
fmt.Println("Hello, world!")
2016
}
17+
-- blah/subdir/x --
18+
x contents
2119
-- expect --
2220
>-- go.mod --
2321
>module example.com/blah
@@ -30,3 +28,5 @@ func main() {
3028
>func main() {
3129
> fmt.Println("Hello, world!")
3230
>}
31+
>-- subdir/x --
32+
>x contents

goproxytest/proxy.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ content of the module zip file. The path@vers prefix required of files in the
1616
zip file is added automatically by the proxy: the files in the archive have
1717
names without the prefix, like plain "go.mod", "x.go", and so on.
1818
19-
See ../cmd/txtar-addmod and ../cmd/txtar-savedir for tools generate txtar
19+
See ../cmd/txtar-addmod and ../cmd/txtar-c for tools generate txtar
2020
files, although it's fine to write them by hand.
2121
*/
2222
package goproxytest

0 commit comments

Comments
 (0)