-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
133 lines (116 loc) · 3.97 KB
/
main.go
File metadata and controls
133 lines (116 loc) · 3.97 KB
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
package main
import (
"flag"
"fmt"
"os"
"runtime/pprof"
"time"
"godedupe/report"
)
const (
name string = "godedupe"
version string = "1.5.2"
)
const defaultExcludeFile string = "~/.godedupe-exclude"
type targetDirectories []string
func (i *targetDirectories) String() string {
return ""
}
func (i *targetDirectories) Set(value string) error {
*i = append(*i, value)
return nil
}
// Options for start the program
type Options struct {
report.Opts
cpuprofile string
targetDirs targetDirectories
pattern string
excludeFrom string
maxDepth int
showCurrentValues bool
excludeEmptyFiles bool
excludeHidden bool
enableRecursion bool
followSymlinks bool
quiet bool
}
var (
opt Options
)
// Init the options to run the program
func init() {
flag.StringVar(&opt.cpuprofile, "cpuprofile", "", "Enable profiling")
flag.Var(&opt.targetDirs, "t", "Target directories where the program search for duplicated files")
flag.StringVar(&opt.JsonFile, "json", "", "Export the list of duplicated files to the given json file")
flag.StringVar(&opt.pattern, "pattern", "", "Only find duplicates if the given pattern match")
flag.StringVar(&opt.excludeFrom, "exclude-from", defaultExcludeFile, "Exclude all patterns from the given file")
flag.IntVar(&opt.maxDepth, "d", -1, "Max recursion depth, -1 = no limit. 1 = current directory")
flag.BoolVar(&opt.excludeEmptyFiles, "z", true, "Exclude the zero length files")
flag.BoolVar(&opt.excludeHidden, "e", true, "Exclude the hidden files")
flag.BoolVar(&opt.showCurrentValues, "debug", false,
"Show the current values of the program options")
flag.BoolVar(&opt.enableRecursion, "r", true, "Follow subdirectories (recursion)")
flag.BoolVar(&opt.followSymlinks, "s", false, "Follow symlinks")
flag.BoolVar(&opt.ShowSummary, "m", false, "Show a summary")
flag.BoolVar(&opt.quiet, "q", false, "Don't show progress info")
flag.BoolVar(&opt.ShowNotification, "show-notification", false,
"Show a desktop notification when the program finish")
flag.BoolVar(&opt.SameLine, "1", false, "Show each set of duplicated files in one line."+
"It implies -q (quiet) and ignores -m (show summary)")
flag.Parse()
}
// Header show the program name and current version
func header() {
fmt.Println("------------------------")
fmt.Printf("%s - version %s\n", name, version)
fmt.Println("------------------------")
}
// ShowDebugInfo print all the current option values
func showDebugInfo() {
if opt.showCurrentValues {
header()
fmt.Println()
fmt.Println("------------------------")
fmt.Println("Current option values")
fmt.Println("------------------------")
fmt.Println("Target directory :", opt.targetDirs)
fmt.Println("Exclude zero length files :", opt.excludeEmptyFiles)
fmt.Println("Exclude hidden files :", opt.excludeHidden)
fmt.Println("Ignore symlinks :", opt.followSymlinks)
fmt.Println("Recursive search :", opt.enableRecursion)
fmt.Println("Show a summary :", opt.ShowSummary)
fmt.Println("Quiet :", opt.quiet)
fmt.Println("Show notification :", opt.ShowNotification)
fmt.Println("Pattern :", opt.pattern)
fmt.Println("Exclude from :", opt.excludeFrom)
fmt.Println("Max depth :", opt.maxDepth)
fmt.Println("Json file :", opt.JsonFile)
fmt.Println("Profile output :", opt.cpuprofile)
fmt.Println("Same line :", opt.SameLine)
fmt.Println("------------------------")
}
}
func trackTime(now time.Time) {
fmt.Printf("[+] Program terminated in %v\n", time.Since(now))
}
func executeCPUProfile(profile string) {
f, err := os.Create(profile)
if err != nil {
panic(err)
}
pprof.StartCPUProfile(f)
}
func main() {
if opt.SameLine {
opt.quiet = true
}
showDebugInfo()
if opt.cpuprofile != "" {
executeCPUProfile(opt.cpuprofile)
defer pprof.StopCPUProfile()
}
st := time.Now()
start()
trackTime(st)
}