Skip to content

Commit dda2296

Browse files
committed
Implemented internal directories.
1 parent 3b90512 commit dda2296

11 files changed

+647
-625
lines changed

go.mod

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,6 @@ require (
1414
github.com/dlclark/regexp2 v1.4.0 // indirect
1515
github.com/mitchellh/go-homedir v1.1.0 // indirect
1616
github.com/xo/terminfo v0.0.0-20210125001918-ca9a967f8778 // indirect
17-
golang.org/x/sys v0.0.0-20220114195835-da31bd327af9 // indirect
17+
golang.org/x/sys v0.0.0-20220128215802-99c3d69c2c27 // indirect
1818
golang.org/x/text v0.3.7 // indirect
1919
)

go.sum

+2-2
Original file line numberDiff line numberDiff line change
@@ -477,8 +477,8 @@ golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c/go.mod h1:oPkhp1MJrh7nUepCBc
477477
golang.org/x/sys v0.0.0-20210806184541-e5e7981a1069/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
478478
golang.org/x/sys v0.0.0-20210823070655-63515b42dcdf/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
479479
golang.org/x/sys v0.0.0-20211025201205-69cdffdb9359/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
480-
golang.org/x/sys v0.0.0-20220114195835-da31bd327af9 h1:XfKQ4OlFl8okEOr5UvAqFRVj8pY/4yfcXrddB8qAbU0=
481-
golang.org/x/sys v0.0.0-20220114195835-da31bd327af9/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
480+
golang.org/x/sys v0.0.0-20220128215802-99c3d69c2c27 h1:XDXtA5hveEEV8JB2l7nhMTp3t3cHp9ZpwcdjqyEWLlo=
481+
golang.org/x/sys v0.0.0-20220128215802-99c3d69c2c27/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
482482
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
483483
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
484484
golang.org/x/text v0.0.0-20170915032832-14c0d48ead0c/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=

internal/misc/misc.go

+89
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
package misc
2+
3+
import (
4+
"fmt"
5+
"os"
6+
"path/filepath"
7+
"runtime"
8+
"strconv"
9+
"strings"
10+
)
11+
12+
type (
13+
Export map[string]bool
14+
)
15+
16+
const Filename = "-zipcomment.txt"
17+
18+
// unique checks the destination path against an export map.
19+
// The map contains a unique collection of previously used destination
20+
// paths, to avoid creating duplicate text filenames while using the
21+
// SaveName config.
22+
func (e Export) Unique(zipPath, dest string) string {
23+
base := filepath.Base(zipPath)
24+
name := strings.TrimSuffix(base, filepath.Ext(base)) + Filename
25+
if runtime.GOOS == "windows" {
26+
name = strings.ToLower(name)
27+
}
28+
path := filepath.Join(dest, name)
29+
if f := e.Find(path); f != path {
30+
path = f
31+
}
32+
e[path] = true
33+
return path
34+
}
35+
36+
// Find searches for the name in the export map.
37+
// If no matches exist, the name is unique and returned.
38+
// Otherwise find attempts to append a `_1` suffix to the
39+
// name. If the name already has this suffix, the digit
40+
// is incrementally increased until a unique name is returned.
41+
func (e Export) Find(name string) string {
42+
if !e[name] {
43+
return name
44+
}
45+
i, new := 0, ""
46+
for {
47+
i++
48+
ext := filepath.Ext(name)
49+
base := strings.TrimSuffix(name, ext)
50+
a := strings.Split(base, "_")
51+
n, err := strconv.Atoi(a[len(a)-1])
52+
if err == nil {
53+
i = n
54+
base = strings.Join(a[0:len(a)-1], "_")
55+
}
56+
suf := fmt.Sprintf("_%d", i+1)
57+
new = fmt.Sprintf("%s%s%s", base, suf, ext)
58+
if !e[new] {
59+
return new
60+
}
61+
if i > 9999 {
62+
break
63+
}
64+
}
65+
return ""
66+
}
67+
68+
// ExportName returns a text file file path for the Export config.
69+
func ExportName(path string) string {
70+
if path == "" {
71+
return ""
72+
}
73+
return strings.TrimSuffix(path, filepath.Ext(path)) + Filename
74+
}
75+
76+
// Self returns the path for the zipcmt executable.
77+
func Self() (string, error) {
78+
exe, err := os.Executable()
79+
if err != nil {
80+
return "", fmt.Errorf("self error: %w", err)
81+
}
82+
return exe, nil
83+
}
84+
85+
// Valid checks that the named file is a known zip archive.
86+
func Valid(name string) bool {
87+
const z = ".zip"
88+
return filepath.Ext(strings.ToLower(name)) == z
89+
}

internal/misc/misc_test.go

+94
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
package misc_test
2+
3+
import (
4+
"testing"
5+
6+
"github.com/bengarrett/zipcmt/internal/misc"
7+
)
8+
9+
func TestExportName(t *testing.T) {
10+
tests := []struct {
11+
name string
12+
path string
13+
want string
14+
}{
15+
{"empty", "", ""},
16+
{"name", "myfile.zip", "myfile-zipcomment.txt"},
17+
{"windows", "C:\\Users\\retro\\myfile.zip", "C:\\Users\\retro\\myfile-zipcomment.txt"},
18+
{"*nix", "/home/retro/myfile.zip", "/home/retro/myfile-zipcomment.txt"},
19+
}
20+
for _, tt := range tests {
21+
t.Run(tt.name, func(t *testing.T) {
22+
if got := misc.ExportName(tt.path); got != tt.want {
23+
t.Errorf("ExportName() = %v, want %v", got, tt.want)
24+
}
25+
})
26+
}
27+
}
28+
29+
func TestExportFind(t *testing.T) {
30+
files := misc.Export{
31+
"file.txt": true,
32+
"file_1.txt": true,
33+
"file_2.txt": true,
34+
"file_3.txt": true,
35+
}
36+
tests := []struct {
37+
name string
38+
e misc.Export
39+
fname string
40+
want string
41+
}{
42+
{"none", files, "", ""},
43+
{"unique", files, "somefile.txt", "somefile.txt"},
44+
{"conflict", files, "file.txt", "file_4.txt"},
45+
{"conflict 3", files, "file_3.txt", "file_4.txt"},
46+
{"conflict 4", files, "file_4.txt", "file_4.txt"},
47+
{"underscores", files, "file_000_1.txt", "file_000_1.txt"},
48+
}
49+
for _, tt := range tests {
50+
t.Run(tt.name, func(t *testing.T) {
51+
if got := tt.e.Find(tt.fname); got != tt.want {
52+
t.Errorf("ExportFind() = %v, want %v", got, tt.want)
53+
}
54+
})
55+
}
56+
}
57+
58+
func TestSelf(t *testing.T) {
59+
tests := []struct {
60+
name string
61+
wantErr bool
62+
}{
63+
{"expected", false},
64+
}
65+
for _, tt := range tests {
66+
t.Run(tt.name, func(t *testing.T) {
67+
_, err := misc.Self()
68+
if (err != nil) != tt.wantErr {
69+
t.Errorf("Self() error = %v, wantErr %v", err, tt.wantErr)
70+
return
71+
}
72+
})
73+
}
74+
}
75+
76+
func TestValid(t *testing.T) {
77+
tests := []struct {
78+
name string
79+
fname string
80+
want bool
81+
}{
82+
{"empty", "", false},
83+
{"dir", "/somedir/", false},
84+
{"file", "/somedir/somefile.txt", false},
85+
{"zip", "somedir/somefile.zip", true},
86+
}
87+
for _, tt := range tests {
88+
t.Run(tt.name, func(t *testing.T) {
89+
if got := misc.Valid(tt.fname); got != tt.want {
90+
t.Errorf("Valid() = %v, want %v", got, tt.want)
91+
}
92+
})
93+
}
94+
}

lib/example_posix_test.go

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1+
//go:build !windows
12
// +build !windows
3+
24
// © Ben Garrett https://github.com/bengarrett/zipcmt
35

46
package zipcmt
@@ -8,11 +10,11 @@ import (
810
"log"
911
)
1012

11-
func ExampleConfig_clean() {
13+
func ExampleConfig_Clean() {
1214
c := Config{
1315
SaveName: "..//test///.",
1416
}
15-
if err := c.clean(); err != nil {
17+
if err := c.Clean(); err != nil {
1618
log.Fatalln(err)
1719
}
1820
fmt.Print(c.SaveName)

lib/example_test.go

+46-46
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,46 @@
1-
// © Ben Garrett https://github.com/bengarrett/zipcmt
2-
3-
package zipcmt
4-
5-
import (
6-
"fmt"
7-
"log"
8-
9-
"github.com/gookit/color"
10-
)
11-
12-
func init() {
13-
color.Enable = false
14-
}
15-
16-
func ExampleRead() {
17-
s, err := Read("../test/test-with-comment.zip", false)
18-
if err != nil {
19-
log.Fatalln(err)
20-
}
21-
fmt.Print(s)
22-
// Output:
23-
//This is an example test comment for zipcmmt.
24-
//
25-
}
26-
27-
func ExampleConfig_Status() {
28-
c := Config{}
29-
c.SetTest()
30-
if err := c.WalkDir("../test"); err != nil {
31-
log.Panicln(err)
32-
}
33-
fmt.Print(c.Status())
34-
35-
c = Config{
36-
Dupes: true,
37-
}
38-
c.SetTest()
39-
if err := c.WalkDir("../test"); err != nil {
40-
log.Panicln(err)
41-
}
42-
fmt.Print(c.Status())
43-
// Output:
44-
// Scanned 4 zip archives and found 1 unique comment
45-
// Scanned 4 zip archives and found 2 comments
46-
}
1+
// © Ben Garrett https://github.com/bengarrett/zipcmt
2+
3+
package zipcmt
4+
5+
import (
6+
"fmt"
7+
"log"
8+
9+
"github.com/gookit/color"
10+
)
11+
12+
func init() {
13+
color.Enable = false
14+
}
15+
16+
func ExampleRead() {
17+
s, err := Read("../test/test-with-comment.zip", false)
18+
if err != nil {
19+
log.Fatalln(err)
20+
}
21+
fmt.Print(s)
22+
// Output:
23+
//This is an example test comment for zipcmmt.
24+
//
25+
}
26+
27+
func ExampleConfig_Status() {
28+
c := Config{}
29+
c.SetTest()
30+
if err := c.WalkDir("../test"); err != nil {
31+
log.Panicln(err)
32+
}
33+
fmt.Print(c.Status())
34+
35+
c = Config{
36+
Dupes: true,
37+
}
38+
c.SetTest()
39+
if err := c.WalkDir("../test"); err != nil {
40+
log.Panicln(err)
41+
}
42+
fmt.Print(c.Status())
43+
// Output:
44+
// Scanned 4 zip archives and found 1 unique comment
45+
// Scanned 4 zip archives and found 2 comments
46+
}

lib/log.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ func (c *Config) WriteLog(s string) {
5555
if st.Size() == 0 {
5656
c.logHeader(logger)
5757
}
58-
l := fmt.Sprintf("zip#: %07d; cmmt#: %07d; ", c.zips, c.cmmts)
58+
l := fmt.Sprintf("zip#: %07d; cmmt#: %07d; ", c.Zips, c.Cmmts)
5959
if !c.Dupes {
6060
l += fmt.Sprintf("hashes: %s; ", humanize.Bytes(uint64(len(c.hashes)*32)))
6161
}

0 commit comments

Comments
 (0)