-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
98 lines (87 loc) · 2.05 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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
// find-duplicates finds duplicate files, concurrently.
package main
// FIXME operate on io/fs.FS
import (
"encoding/json"
"fmt"
"os"
"runtime/trace"
"github.com/spf13/pflag"
"github.com/twpayne/find-duplicates/internal/dupfind"
)
func run() error {
// Parse command line arguments.
keepGoing := pflag.BoolP("keep-going", "k", false, "keep going after errors")
threshold := pflag.IntP("threshold", "n", 2, "threshold")
output := pflag.StringP("output", "o", "", "output file")
printStatistics := pflag.BoolP("statistics", "s", false, "print statistics")
traceFile := pflag.String("trace", "", "trace file")
pflag.Parse()
var roots []string
if pflag.NArg() == 0 {
roots = []string{"."}
} else {
roots = pflag.Args()
}
// Create a trace file, if requested.
if *traceFile != "" {
traceFile, err := os.Create(*traceFile)
if err != nil {
return err
}
defer traceFile.Close()
if err := trace.Start(traceFile); err != nil {
return err
}
defer trace.Stop()
}
// Find duplicates.
options := []dupfind.Option{
dupfind.WithThreshold(*threshold),
dupfind.WithRoots(roots...),
}
if *keepGoing {
option := dupfind.WithErrorHandler(func(err error) error {
fmt.Fprintln(os.Stderr, err)
return nil
})
options = append(options, option)
}
dupFinder := dupfind.NewDupFinder(options...)
result, err := dupFinder.FindDuplicates()
if err != nil {
return err
}
// Write output file.
var outputFile *os.File
if *output == "" || *output == "-" {
outputFile = os.Stdout
} else {
file, err := os.Create(*output)
if err != nil {
return err
}
defer file.Close()
outputFile = file
}
encoder := json.NewEncoder(outputFile)
encoder.SetIndent("", " ")
if err := encoder.Encode(result); err != nil {
return err
}
// Print statistics.
if *printStatistics {
encoder := json.NewEncoder(os.Stderr)
encoder.SetIndent("", " ")
if err := encoder.Encode(dupFinder.Statistics()); err != nil {
return err
}
}
return nil
}
func main() {
if err := run(); err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
}