forked from verless/verless
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfs.go
More file actions
67 lines (55 loc) · 1.39 KB
/
fs.go
File metadata and controls
67 lines (55 loc) · 1.39 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
// Package fs provides functions for filesystem operations.
package fs
import (
"os"
"path/filepath"
"strings"
)
var (
// MarkdownOnly is a filter that only lets pass Markdown files.
MarkdownOnly = func(file string) bool {
return filepath.Ext(file) == ".md"
}
// NoUnderscores is a predefined filter that doesn't let pass
// files starting with an underscore.
NoUnderscores = func(file string) bool {
filename := filepath.Base(file)
return !strings.HasPrefix(filename, "_")
}
// ErrStreaming is returned from StreamFiles.
ErrStreaming error = nil
)
// StreamFiles sends files in a given path that match the given
// filters through the files channel.
func StreamFiles(path string, files chan<- string, filters ...func(file string) bool) error {
if _, err := os.Stat(path); os.IsNotExist(err) {
close(files)
return nil
}
ErrStreaming = filepath.Walk(path, func(file string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if info.IsDir() {
return nil
}
for _, filter := range filters {
if !filter(file) {
return nil
}
}
files <- file
return nil
})
close(files)
return ErrStreaming
}
// MkdirAll creates one or more directories inside the given path.
func MkdirAll(path string, dirs ...string) error {
for _, dir := range dirs {
if err := os.MkdirAll(filepath.Join(path, dir), 0755); err != nil {
return err
}
}
return nil
}