-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
68 lines (58 loc) · 1.44 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
package main
import (
"fmt"
"io"
"os"
"sync"
"github.com/onlyhavecans/zipcheck/internal"
"github.com/onlyhavecans/zipcheck/internal/ziptools"
)
const (
maxGoroutines = 10
exitFail = 1
fileExtension = ".zip"
)
func main() {
if err := run(os.Args, os.Stdout, os.Stderr); err != nil {
_, _ = fmt.Fprintf(os.Stderr, "%v\n", err)
os.Exit(exitFail)
}
}
func run(args []string, stdout io.Writer, stderr io.Writer) error {
// get all directories passed as arguments and put them in a slice
directories := getDirectoriesFromArgs(args)
log(stdout, "Passed Directories:", directories)
// walk through all directories and check all zip files
for _, directory := range directories {
allFiles, err := internal.GetFilesRecursive(directory, fileExtension)
if err != nil {
log(stderr, err)
continue
}
log(stdout, "Found", len(allFiles), "zip files in:", directory)
// create a semaphore to limit the number of goroutines
semaphore := make(chan struct{}, maxGoroutines)
var wg sync.WaitGroup
for _, file := range allFiles {
wg.Add(1)
semaphore <- struct{}{}
go func(file string) {
defer wg.Done()
if ziptools.IsValidZip(file) {
log(stdout, ".")
} else {
log(stderr, "Invalid zip file:", file)
}
<-semaphore
}(file)
}
wg.Wait()
}
return nil
}
func log(to io.Writer, v ...interface{}) {
fmt.Fprintln(to, v...)
}
func getDirectoriesFromArgs(args []string) []string {
return args[1:]
}