-
Notifications
You must be signed in to change notification settings - Fork 1
/
options.go
61 lines (53 loc) · 1.23 KB
/
options.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
package staticfs
import (
"errors"
"fmt"
"io/fs"
"os"
)
type PathTranslator func(real string) (external string, accept bool, err error)
type options struct {
Index map[string]string
Translators []PathTranslator
FileSystem fs.FS
}
type Option func(*options) error
func WithPath(external, real string) Option {
return func(o *options) error {
current, ok := o.Index[external]
if ok {
return fmt.Errorf("request path %q already points to %q", external, current)
}
o.Index[external] = real
return nil
}
}
func WithPathTranslators(ts ...PathTranslator) Option {
return func(o *options) error {
if len(ts) == 0 {
return errors.New("empty path translator list")
}
for i, translator := range ts {
if translator == nil {
return fmt.Errorf("path translator list entry #%d is <nil>", i)
}
}
o.Translators = append(o.Translators, ts...)
return nil
}
}
func WithFileSystem(fs fs.FS) Option {
return func(o *options) (err error) {
if fs == nil {
return errors.New("unable to use a <nil> file system")
}
if o.FileSystem != nil {
return errors.New("file system is already set")
}
o.FileSystem = fs
return nil
}
}
func WithDirectory(p string) Option {
return WithFileSystem(os.DirFS(p))
}