-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.go
57 lines (51 loc) · 1.3 KB
/
util.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 vfstemplate
import (
"io/ioutil"
"net/http"
"os"
)
// ReadDir reads the contents of the directory associated with file and
// returns a slice of FileInfo values in directory order.
func ReadDir(fs http.FileSystem, name string) ([]os.FileInfo, error) {
f, err := fs.Open(name)
if err != nil {
return nil, err
}
defer f.Close()
return f.Readdir(-1)
}
// Stat returns the FileInfo structure describing file.
func Stat(fs http.FileSystem, name string) (os.FileInfo, error) {
f, err := fs.Open(name)
if err != nil {
return nil, err
}
defer f.Close()
return f.Stat()
}
// ReadFile reads the file named by path from fs and returns the contents.
func ReadFile(fs http.FileSystem, path string) ([]byte, error) {
rc, err := fs.Open(path)
if err != nil {
return nil, err
}
defer rc.Close()
return ioutil.ReadAll(rc)
}
// ReadFileString reads the file named by path from fs and returns the contents.
func ReadFileString(fs http.FileSystem, path string) (string, error) {
buf, err := ReadFile(fs, path)
if err != nil {
return "", err
}
return string(buf), nil
}
// Exists reports whether the named file or directory exists in http.FileSystem.
func Exists(fs http.FileSystem, name string) bool {
if _, err := Stat(fs, name); err != nil {
if os.IsNotExist(err) {
return false
}
}
return true
}