/
files.go
57 lines (46 loc) · 1.11 KB
/
files.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
package compactedaol
import (
"fmt"
"os"
"path/filepath"
"sort"
"strings"
"time"
)
type filePathInfo struct {
info os.FileInfo
filepath string
}
type byPath []filePathInfo
func (s byPath) Len() int {
return len(s)
}
func (s byPath) Less(i, j int) bool {
return s[i].filepath < s[j].filepath
}
func (s byPath) Swap(i, j int) {
s[i], s[j] = s[j], s[i]
}
// listFilesWithSuffix returns all files matching the suffix in descending
// order sorted by filepath
func listFilesWithSuffix(basepath, suffix string, desc bool) ([]filePathInfo, error) {
files := []filePathInfo{}
err := filepath.Walk(basepath, func(path string, info os.FileInfo, err error) error {
if filepath.Ext(path) == suffix {
files = append(files, filePathInfo{info, path})
}
return nil
})
if desc {
sort.Sort(sort.Reverse(byPath(files)))
} else {
sort.Sort(byPath(files))
}
return files, err
}
func genFilename(suffix string) string {
return fmt.Sprintf("%d%s", time.Now().UTC().UnixNano(), suffix)
}
func changeSuffix(filepath, oldSuffix, newSuffix string) string {
return strings.Replace(filepath, oldSuffix, newSuffix, 1)
}