This repository has been archived by the owner on Nov 8, 2017. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathfilesystem.go
75 lines (59 loc) · 2.27 KB
/
filesystem.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
package bake
import (
"errors"
)
// ErrUnregisteredFileSystem is returned when a file system has not been registered.
var ErrUnregisteredFileSystem = errors.New("unregistered file system")
// FileSystem represents a way to mount the build filesystem.
type FileSystem interface {
Open() error
Close() error
// The underlying path being served.
Path() string
// Creates a new root path for the file system where changes can be tracked.
CreateRoot() FileSystemRoot
}
// FileSystemRoot represents a copy of the file system root.
// It can be used for tracking reads & writes.
type FileSystemRoot interface {
Path() string
Readset() map[string]struct{}
Writeset() map[string]struct{}
}
// lookup of file system constructors by type.
var newFileSystemFns = make(map[string]NewFileSystemFunc)
// NewFileSystem returns a new instance of FileSystem for a given type.
func NewFileSystem(typ string, opt FileSystemOptions) (FileSystem, error) {
fn := newFileSystemFns[typ]
if fn == nil {
return nil, ErrUnregisteredFileSystem
}
return fn(opt)
}
// NewFileSystemFunc is a function for creating a new file system.
type NewFileSystemFunc func(FileSystemOptions) (FileSystem, error)
// RegisterFileSystem registers a filesystem constructor by type.
func RegisterFileSystem(typ string, fn NewFileSystemFunc) {
if _, ok := newFileSystemFns[typ]; ok {
panic("file system already registered: " + typ)
}
newFileSystemFns[typ] = fn
}
// FileSystemOptions represents a list of options passed to a file sytem on creation.
type FileSystemOptions struct {
// Underlying path to serve
Path string
// Directory to mount to.
MountPath string
}
// nopFileSystem is a file system that does nothing.
type nopFileSystem struct{}
func (*nopFileSystem) Open() error { return nil }
func (*nopFileSystem) Close() error { return nil }
func (*nopFileSystem) Path() string { return "" }
func (*nopFileSystem) CreateRoot() FileSystemRoot { return &nopFileSystemRoot{} }
// nopFileSystemRoot is a file system root that does nothing.
type nopFileSystemRoot struct{}
func (*nopFileSystemRoot) Path() string { return "" }
func (*nopFileSystemRoot) Readset() map[string]struct{} { return nil }
func (*nopFileSystemRoot) Writeset() map[string]struct{} { return nil }