-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathfs.go
71 lines (54 loc) · 1.65 KB
/
fs.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
package config
import (
"io/ioutil"
"os"
"github.com/mattn/go-zglob"
)
// FileSystem is an interface wrapping filesystem operations for
// sourcers that read information from disk. This is necessary in
// order to allow remote and in-memory filesystems that may be
// present in some application deployments. Third-party libraries
// such as spf13/afero that provide FS-like functionality can be
// shimmed into this interface.
type FileSystem interface {
// Exists determines if the given path exists.
Exists(path string) (bool, error)
// ListFiles returns the names of the files that are a direct
// child of the directory at the given path.
ListFiles(path string) ([]string, error)
// Glob returns the paths that the given pattern matches.
Glob(pattern string) ([]string, error)
// ReadFile returns the content of the file at the given path.
ReadFile(path string) ([]byte, error)
}
type realFileSystem struct{}
var _ FileSystem = &realFileSystem{}
func (fs *realFileSystem) Exists(path string) (bool, error) {
if _, err := os.Stat(path); err != nil {
if os.IsNotExist(err) {
err = nil
}
return false, err
}
return true, nil
}
func (fs *realFileSystem) ListFiles(path string) ([]string, error) {
entries, err := ioutil.ReadDir(path)
if err != nil {
return nil, err
}
fileEntries := []string{}
for _, entry := range entries {
if entry.IsDir() {
continue
}
fileEntries = append(fileEntries, entry.Name())
}
return fileEntries, nil
}
func (fs *realFileSystem) Glob(pattern string) ([]string, error) {
return zglob.Glob(pattern)
}
func (fs *realFileSystem) ReadFile(path string) ([]byte, error) {
return ioutil.ReadFile(path)
}