-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
114 lines (91 loc) · 2.07 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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
package main
import (
"flag"
"fmt"
"io"
"io/fs"
"os"
"path/filepath"
)
type App struct {
config *Config
out io.Writer
}
type Config struct {
// Extension of files want to filter
ext *string
// Minimum file size in bytes
min_size *int64
// Maximum file size in bytes
max_size *int64
// Verbose mode
verbose *bool
// List mode
list *bool
// Root path to start
root *string
}
func main() {
app := &App{
config: &Config{
ext: StringPtr(""),
min_size: Int64Ptr(0),
max_size: Int64Ptr(0),
verbose: BoolPtr(false),
list: BoolPtr(false),
root: StringPtr("."),
},
out: os.Stdout,
}
app.Parse()
if err := app.run(); err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
}
func (app *App) Parse() {
// Parse command line flags
flag.Int64Var(app.config.min_size, "min", 0, "Minimum file size")
flag.Int64Var(app.config.max_size, "max", int64(100000000000), "Maximum file size")
flag.BoolVar(app.config.list, "list", false, "List mode")
flag.BoolVar(app.config.verbose, "v", false, "Verbose mode")
flag.StringVar(app.config.ext, "ext", "", "Extension of files")
flag.StringVar(app.config.root, "root", ".", "Root path to start walk")
flag.Parse()
}
func StringPtr(str string) *string {
return &str
}
func BoolPtr(b bool) *bool {
return &b
}
func Int64Ptr(number int64) *int64 {
return &number
}
func (app *App) run() error {
return filepath.Walk(*app.config.root, func(path string, info fs.FileInfo, err error) error {
if err != nil {
return nil
}
if filterOut(path, app.config.ext, app.config.min_size, app.config.max_size, info) {
return nil
}
if *app.config.list {
return listFile(path, app.out)
}
return listFile(path, app.out)
})
}
func filterOut(path string, ext *string, minSize *int64, maxSize *int64, info fs.FileInfo) bool {
if info.IsDir() || info.Size() >= *maxSize || info.Size() <= *minSize {
return true
}
if *ext != "" && filepath.Ext(path) != *ext {
return true
}
return false
}
func listFile(path string, out io.Writer) error {
_, err := fmt.Fprintln(out, path)
return err
}