-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
96 lines (81 loc) · 2.01 KB
/
Copy pathmain.go
File metadata and controls
96 lines (81 loc) · 2.01 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
package main
import (
"flag"
"fmt"
"io"
"log"
"os"
"path/filepath"
)
type config struct {
ext string // extension to filter out
size int64 // min file size
list bool // list files
del bool // delete files
wLog io.Writer // log destination writer
archive string // archive directory
}
func main() {
// Parsing command line flags
root := flag.String("root", ".", "Root directory to start")
logFile := flag.String("log", "", "Log deletes to this file")
// Action options
list := flag.Bool("list", false, "List files only")
archive := flag.String("archive", "", "Path to directory where files should be archived")
del := flag.Bool("del", false, "Delete files")
ext := flag.String("ext", "", "File extension to filter out")
size := flag.Int64("size", 0, "Minimum file size")
flag.Parse()
var (
f = os.Stdout
err error
)
if *logFile != "" {
f, err = os.OpenFile(*logFile, os.O_APPEND|os.O_CREATE|os.O_RDWR, 0644)
if err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
defer f.Close()
}
c := config{
ext: *ext,
size: *size,
list: *list,
del: *del,
wLog: f,
archive: *archive,
}
if err := run(*root, os.Stdout, c); err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
}
func run(root string, out io.Writer, cfg config) error {
delLogger := log.New(cfg.wLog, "DELETED FILE: ", log.LstdFlags)
return filepath.Walk(root,
func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if filterOut(path, cfg.ext, cfg.size, info) {
return nil
}
// If list was explicitly set, don't do anything else
if cfg.list {
return listFile(path, out)
}
// Archive files and continue if successful
if cfg.archive != "" {
if err := archiveFile(cfg.archive, root, path); err != nil {
return err
}
}
// Delete files
if cfg.del {
return delFile(path, delLogger)
}
// List is the default option if nithing else was set
return listFile(path, out)
})
}