-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathfile.go
79 lines (67 loc) · 1.72 KB
/
file.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
package useless
import (
"bufio"
"os"
"path/filepath"
)
// GetFileList compiles a list of files that match any of the extensions in the
// FileExtensions map and are less than or equal to the specified file size.
func GetFileList(dirname string, size int64) (files []string, err error) {
walkFn := func(path string, info os.FileInfo, err error) error {
// Can't walk here.
if err != nil {
// Continue walking elsewhere.
return nil
}
// Check if this is a regular file otherwise skip it.
if !info.Mode().IsRegular() {
return nil
}
// Check if this has a size equal or less than to the specified
// file size.
if !(info.Size() <= size) {
return nil
}
// Check if this has a file extension that is in the
// FileExtensions map.
if !FileExtensions[filepath.Ext(info.Name())] {
return nil
}
files = append(files, path)
return nil
}
err = filepath.Walk(dirname, walkFn)
return
}
// WriteFileList writes a list of files to the specified directory.
func WriteFileList(filename string, files []string) (err error) {
f, err := os.OpenFile(filename, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0644)
if err != nil {
return
}
defer f.Close()
for _, filepath := range files {
_, err = f.WriteString(filepath + "\n")
if err != nil {
return
}
}
return
}
// ReadFileList reads a list of files contained in a file within the users
// home directory.
func ReadFileList(filename string) (files []string, err error) {
file, err := os.Open(filename)
if err != nil {
return
}
defer file.Close()
scanner := bufio.NewScanner(file)
for scanner.Scan() {
// Append each line of the file list to the files string
// slice.
files = append(files, scanner.Text())
}
err = scanner.Err()
return
}