-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
87 lines (77 loc) · 1.66 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
package main
import (
"flag"
"fmt"
"io"
"io/fs"
"log"
"os"
"path/filepath"
"strings"
)
type config struct {
ext []string
size int64
list bool
del bool
archive string
log io.Writer
}
var (
f = os.Stdout
err error
)
func main() {
root := flag.String("root", ".", "Root directory to start")
list := flag.Bool("list", false, "List files only")
exts := flag.String("ext", "", "File extensions to filter out, comma separated.")
logFile := flag.String("log", "", "Log deleted files to this file")
size := flag.Int64("size", 0, "Minimum file size")
del := flag.Bool("del", false, "Delete matched files")
archiveDir := flag.String("archive", "", "Archive directory")
flag.Parse()
if *logFile != "" {
f, err = os.OpenFile(*logFile, os.O_CREATE|os.O_APPEND|os.O_RDWR, 0644)
if err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
defer f.Close()
}
c := config{
ext: strings.Split((*exts), ","),
list: *list,
size: *size,
del: *del,
log: f,
archive: *archiveDir,
}
_ = c
if err := run(*root, os.Stdout, c); err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
}
func run(root string, out io.Writer, c config) error {
delLogger := log.New(c.log, "Deleted file", log.LstdFlags)
return filepath.Walk(root, func(path string, info fs.FileInfo, err error) error {
if err != nil {
return err
}
if filterOut(path, c.size, c.ext, info) {
return nil
}
if c.list {
return listFile(path, out)
}
if c.archive != "" {
if err := archiveFile(c.archive, root, path); err != nil {
return err
}
}
if c.del {
return delFile(path, delLogger)
}
return listFile(path, out)
})
}