forked from ghetzel/friendscript
/
environment_io.go
93 lines (81 loc) · 2.72 KB
/
environment_io.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
package friendscript
import (
"bytes"
"fmt"
"io"
"io/ioutil"
"os"
"github.com/PerformLine/friendscript/utils"
"github.com/PerformLine/go-stockutil/typeutil"
)
// Registers a new handler function that will be used for turning paths into writable streams.
func (self *Environment) RegisterPathWriter(handler utils.PathWriterFunc) {
self.pathWriters = append([]utils.PathWriterFunc{
handler,
}, self.pathWriters...)
}
// Registers a new handler function that will be used for turning paths into readable streams.
func (self *Environment) RegisterPathReader(handler utils.PathReaderFunc) {
self.pathReaders = append([]utils.PathReaderFunc{
handler,
}, self.pathReaders...)
}
// Takes a path string and consults all registered PathWriterFuncs. The first one to claim it can handle
// the path will be responsible for returning a possibly-rewritten path string and an io.Writer that will
// accept the data being written.
func (self *Environment) GetWriterForPath(path string) (string, io.Writer, error) {
for _, handler := range self.pathWriters {
if p, w, err := handler(path); err == nil {
// non-nil io.Writer + nil error = a handled request
if w != nil {
return p, w, nil
}
} else {
return ``, nil, err
}
}
return ``, nil, nil
}
// Takes a path string and consults all registered PathReaderFuncs. The first one to claim it can handle
// the path will be responsible for returning an io.ReadCloser that represents the stream of data being
// sought.
func (self *Environment) GetReaderForPath(path string) (io.ReadCloser, error) {
for _, handler := range self.pathReaders {
if r, err := handler(path); err == nil {
// non-nil RC + nil error = a handled request
if r != nil {
return r, nil
}
} else {
return nil, err
}
}
return os.Open(path)
}
// Open a readable destination file for reading. If fileOrReader is a string, it will be treated
// as a path and will be sent to GetReaderForPath(). If it is an io.Reader, it will be returned
// without reading from it.
func (self *Environment) Open(fileOrReader interface{}) (io.ReadCloser, error) {
var rc io.ReadCloser
if b, ok := fileOrReader.([]byte); ok {
rc = ioutil.NopCloser(bytes.NewBuffer(b))
} else if i, ok := fileOrReader.([]interface{}); ok {
rc = ioutil.NopCloser(bytes.NewBuffer(
typeutil.Bytes(i),
))
} else if filename, ok := fileOrReader.(string); ok {
if file, err := self.GetReaderForPath(filename); err == nil {
rc = file
} else {
return nil, err
}
} else if r, ok := fileOrReader.(io.Reader); ok {
rc = ioutil.NopCloser(r)
} else {
return nil, fmt.Errorf("argument must be a string or stream, got: %T", fileOrReader)
}
if rc == nil {
return nil, fmt.Errorf("no readable data available")
}
return rc, nil
}