-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfilesfinder.go
164 lines (141 loc) · 3.41 KB
/
filesfinder.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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
package filesfinder
import (
"flag"
"fmt"
"io/fs"
"os"
"time"
)
type finder struct {
fsys fs.FS
currentTime time.Time
}
const (
minsinsecs = 60 * time.Second
hrstosecs = 60 * minsinsecs
)
type Option func(f *finder) error
// New provides the construction of finder instance.
// first argument is fs.FS type
// second is the list of time options letting the user specify the time
// as a combination of years, months, days, hours and minutes
func New(fsys fs.FS, opts ...Option) (finder, error) {
f := finder{
fsys: fsys,
currentTime: time.Now(),
}
for _, opt := range opts {
if err := opt(&f); err != nil {
return finder{}, err
}
}
return f, nil
}
func WithYears(years int) Option {
return func(f *finder) error {
f.currentTime = f.currentTime.AddDate(-years, 0, 0)
return nil
}
}
func WithMonths(months int) Option {
return func(f *finder) error {
f.currentTime = f.currentTime.AddDate(0, -months, 0)
return nil
}
}
func WithDays(days int) Option {
return func(f *finder) error {
f.currentTime = f.currentTime.AddDate(0, 0, -days)
return nil
}
}
func WithHours(hours int) Option {
return func(f *finder) error {
f.currentTime = f.currentTime.Add(time.Duration(hours) * -hrstosecs)
return nil
}
}
func WithMinutes(minutes int) Option {
return func(f *finder) error {
f.currentTime = f.currentTime.Add(time.Duration(minutes) * -minsinsecs)
return nil
}
}
type Result struct {
Path string
ModTime time.Time
Error error
}
type Results []Result
// Finds files older than the time options specified
// in local file system.
func (f finder) OlderThan() (Results, error) {
var results Results
walkerr := fs.WalkDir(f.fsys, ".", func(path string, d fs.DirEntry, err error) error {
if err != nil {
results = append(results, Result{Path: path, Error: err})
return fs.SkipDir
}
if d.IsDir() {
return nil
}
finfo, err := d.Info()
if err != nil { // handle other way. add to struct
results = append(results, Result{Path: path, Error: err})
return fs.SkipDir
}
if finfo.ModTime().Before(f.currentTime) {
results = append(results, Result{Path: path, ModTime: finfo.ModTime()})
}
return nil
})
return results, walkerr
}
func makeFinder(inputArgs []string) (finder, error) {
if inputArgs == nil || len(inputArgs) == 0 {
return finder{}, fmt.Errorf("No input provided")
}
fset := flag.NewFlagSet(os.Args[0], flag.ContinueOnError)
years := fset.Int("y", 0, "Years option.")
months := fset.Int("mo", 0, "Months option.")
days := fset.Int("d", 0, "Days option.")
hours := fset.Int("h", 0, "Hours option.")
minutes := fset.Int("mi", 0, "Minutes option.")
fset.SetOutput(os.Stdout)
err := fset.Parse(inputArgs)
if err != nil {
return finder{}, err
}
args := fset.Args()
if len(args) < 1 {
return finder{}, fmt.Errorf("no folder path provided to search under")
}
fs, err := New(os.DirFS(args[0]),
WithYears(*years),
WithMonths(*months),
WithDays(*days),
WithHours(*hours),
WithMinutes(*minutes))
return fs, err
}
func RunCLI() {
finder, err := makeFinder(os.Args[1:])
if err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
results, err := finder.OlderThan()
if err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
for _, res := range results {
fmt.Printf("%s %s\n", res.Path, res.ModTime)
}
fmt.Println("Erros:")
for _, res := range results {
if res.Error != nil {
fmt.Printf("%s\n", res.Error)
}
}
}