This repository has been archived by the owner on Aug 4, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
vfs.go
76 lines (66 loc) · 2.06 KB
/
vfs.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
// Copyright 2013 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// Package vfs defines types for abstract file system access and provides an
// implementation accessing the file system of the underlying OS.
package vfs // import "github.com/thomasf/vfs"
import (
"io"
"io/ioutil"
"os"
)
// The FileSystem interface specifies the methods godoc is using
// to access the file system for which it serves documentation.
type FileSystem interface {
Opener
Lstat(path string) (os.FileInfo, error)
Stat(path string) (os.FileInfo, error)
ReadDir(path string) ([]os.FileInfo, error)
String() string
}
// FileSystemFunc returns a FileSystem or an error if it's not configured
// correctly. Functions that returns FileSystemFuncs should verify that the
// underlying resources exists and if possible automatically fix bad
// configurations.
type FileSystemFunc func() (FileSystem, error)
// Wrapper which returns fs and no error
func Safe(fs FileSystem) FileSystemFunc {
return func() (FileSystem, error) {
return fs, nil
}
}
// MustSafe
func MustSafe(f FileSystemFunc) FileSystem {
fs, err := f()
if err != nil {
panic(err)
}
return fs
}
// OSPather contains the full path to files on vfs FileInfo instances which
// maps to an os.File path. Be careful with using OSPath results for
// directories when multiple filesystems are mounted to the same path in the
// namespace since the path returned by OSPath only leads to the first matching
// vfs.
type OSPather interface {
OSPath() string
}
// Opener is a minimal virtual filesystem that can only open regular files.
type Opener interface {
Open(name string) (ReadSeekCloser, error)
}
// A ReadSeekCloser can Read, Seek, and Close.
type ReadSeekCloser interface {
io.Reader
io.Seeker
io.Closer
}
// ReadFile reads the file named by path from fs and returns the contents.
func ReadFile(fs Opener, path string) ([]byte, error) {
rc, err := fs.Open(path)
if err != nil {
return nil, err
}
defer rc.Close()
return ioutil.ReadAll(rc)
}