Skip to content

Commit fe78b04

Browse files
committed
Brand logo is hidden when --quiet is in use.
About text added to --help. Improved error feedback for directory args. Permission errors are returned for directories but not the sub-directories.
1 parent f88156d commit fe78b04

File tree

3 files changed

+44
-24
lines changed

3 files changed

+44
-24
lines changed

internal/misc/misc.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ type (
1515

1616
const Filename = "-zipcomment.txt"
1717

18-
// unique checks the destination path against an export map.
18+
// Unique checks the destination path against an export map.
1919
// The map contains a unique collection of previously used destination
2020
// paths, to avoid creating duplicate text filenames while using the
2121
// SaveName config.

main.go

+16-13
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ func main() { // nolint: funlen
6767
help(true)
6868
}
6969
flag.Parse()
70-
flags(ver, aliasV)
70+
flags(ver, aliasV, aliasQ)
7171
// parse aliases
7272
if *aliasR {
7373
c.NoWalk = true
@@ -100,31 +100,32 @@ func main() { // nolint: funlen
100100
}
101101
}
102102

103-
func flags(ver, aliasV *bool) {
103+
func flags(ver, aliasV, quiet *bool) {
104104
// convenience for when a help or version flag is passed as an argument
105105
for _, arg := range flag.Args() {
106+
showLogo := !*quiet
106107
switch strings.ToLower(arg) {
107108
case "-h", "-help", "--help":
108-
help(true)
109+
help(showLogo)
109110
os.Exit(0)
110111
case "-v", "-version", "--version":
111-
info()
112+
info(quiet)
112113
os.Exit(0)
113114
}
114115
}
115116
// print version information
116117
if *ver || *aliasV {
117-
info()
118+
info(quiet)
118119
os.Exit(0)
119120
}
120121
// print help if no arguments are given
121122
if len(flag.Args()) == 0 {
122123
if runtime.GOOS == winOS {
123-
color.Warn.Println("zipcmt requires at least one directory or drive letter to scan")
124+
fmt.Fprintln(os.Stderr, color.Warn.Sprint("zipcmt requires at least one directory or drive letter to scan"))
124125
} else {
125-
color.Warn.Println("zipcmt requires at least one directory to scan")
126+
fmt.Fprintln(os.Stderr, color.Warn.Sprint("zipcmt requires at least one directory to scan"))
126127
}
127-
fmt.Println()
128+
fmt.Fprintln(os.Stderr)
128129
help(false)
129130
os.Exit(0)
130131
}
@@ -175,10 +176,10 @@ func help(logo bool) {
175176
var f *flag.Flag
176177
if logo {
177178
fmt.Fprintln(os.Stderr, brand)
179+
fmt.Fprint(os.Stderr, " Zip Comment is the super-fast batch, zip file comment viewer, and extractor.\n"+
180+
" Using a modern PC with the zip files stored on a solid-state drive,\n"+
181+
" zipcmt handles many thousands of archives per second.\n\n")
178182
}
179-
fmt.Fprint(os.Stderr, " Zip Comment is the super-fast batch, zip file comment viewer, and extractor.\n"+
180-
" Using a modern PC with the zip files stored on a solid-state drive,\n"+
181-
" zipcmt handles many thousands of archives per second.\n\n")
182183
fmt.Fprintln(os.Stderr, "Usage:")
183184
if runtime.GOOS == winOS {
184185
helpWin()
@@ -229,9 +230,11 @@ func optimial(w *tabwriter.Writer) {
229230
}
230231

231232
// Info prints out the program information and version.
232-
func info() {
233+
func info(quiet *bool) {
233234
const copyright = "\u00A9"
234-
fmt.Println(brand)
235+
if !*quiet {
236+
fmt.Println(brand)
237+
}
235238
fmt.Printf("zipcmt v%s\n%s 2021-22 Ben Garrett, logo by sensenstahl\n", version, copyright)
236239
fmt.Printf("https://github.com/bengarrett/zipcmt\n\n")
237240
fmt.Printf("build: %s (%s)\n", commit, date)

pkg/zipcmt.go

+27-10
Original file line numberDiff line numberDiff line change
@@ -84,12 +84,13 @@ type (
8484
)
8585

8686
var (
87-
ErrFlag = errors.New("directories to scan must be listed after any option flags")
88-
ErrIsFile = errors.New("directory is a file")
89-
ErrMissing = errors.New("directory cannot be found")
90-
ErrPath = errors.New("directory path cannot be found or points to a file")
91-
ErrPerm = errors.New("directory access is blocked due to its permissions")
92-
ErrValid = errors.New("the operating system reports this directory is invalid")
87+
ErrFlag = errors.New("this option is used after a directory, it must be placed before any directories are listed")
88+
ErrDirExist = errors.New("directory does not exist")
89+
ErrIsFile = errors.New("directory is a file")
90+
ErrMissing = errors.New("directory cannot be found")
91+
ErrPath = errors.New("directory path cannot be found or points to a file")
92+
ErrPerm = errors.New("directory access is blocked due to its permissions")
93+
ErrValid = errors.New("the operating system reports this directory is invalid")
9394
)
9495

9596
// Read the named zip file and return the zip comment.
@@ -142,7 +143,11 @@ func (c *Config) WalkDir(root string) error { // nolint: cyclop,funlen,gocognit
142143
err := filepath.WalkDir(root, func(path string, d fs.DirEntry, err error) error {
143144
if err != nil {
144145
if errors.Is(err, fs.ErrPermission) {
145-
return nil
146+
// skip permission errors for subdirectories
147+
// but return an error if the root is inaccessible
148+
if root != path {
149+
return nil
150+
}
146151
}
147152
return err
148153
}
@@ -156,7 +161,8 @@ func (c *Config) WalkDir(root string) error { // nolint: cyclop,funlen,gocognit
156161
}
157162
c.Zips++
158163
if !c.test && !c.Print && !c.Quiet {
159-
fmt.Print("\r", color.Secondary.Sprint("Scanned "), color.Primary.Sprintf("%d zip archives", c.Zips))
164+
fmt.Print("\r", color.Secondary.Sprint("Scanned "),
165+
color.Primary.Sprintf("%d zip archives", c.Zips))
160166
}
161167
// read zip file comment
162168
cmmt, err := Read(path, c.Raw)
@@ -200,7 +206,8 @@ func (c *Config) WalkDir(root string) error { // nolint: cyclop,funlen,gocognit
200206
dat.name = c.exports.Unique(path, c.SaveName)
201207
c.names += len(dat.name)
202208
if c.save(dat) {
203-
c.WriteLog(fmt.Sprintf("SAVED: %s (%s) << %s", dat.name, humanize.Bytes(uint64(len(cmmt))), path))
209+
c.WriteLog(fmt.Sprintf("SAVED: %s (%s) << %s",
210+
dat.name, humanize.Bytes(uint64(len(cmmt))), path))
204211
c.saved++
205212
}
206213
}
@@ -213,8 +220,18 @@ func walkErrs(root string, err error) error {
213220
var pathError *os.PathError
214221
if errors.As(err, &pathError) {
215222
if root != "" && root[:1] == "-" {
216-
return fmt.Errorf("detected an options flag, %w: %s", ErrFlag, root)
223+
return fmt.Errorf("%w: %s", ErrFlag, root)
224+
}
225+
}
226+
if errors.Is(err, os.ErrNotExist) {
227+
return fmt.Errorf("%w: %s", ErrDirExist, root)
228+
}
229+
if errors.Is(err, fs.ErrPermission) {
230+
f, err := os.Stat(root)
231+
if err != nil {
232+
return fmt.Errorf("%w: %s", ErrPerm, root)
217233
}
234+
return fmt.Errorf("%w: %s, %s", ErrPerm, f.Mode(), root)
218235
}
219236
if err != nil {
220237
return fmt.Errorf("walk directory: %s, %w %T", root, err, err)

0 commit comments

Comments
 (0)