forked from gobuffalo/packr
/
file.go
54 lines (48 loc) · 907 Bytes
/
file.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
package parser
import (
"bytes"
"io"
"io/ioutil"
"path/filepath"
)
// File that is to be parsed
type File struct {
io.Reader
Path string
AbsPath string
}
// Name of the file "app.go"
func (f File) Name() string {
return f.Path
}
// String returns the contents of the reader
func (f *File) String() string {
src, _ := ioutil.ReadAll(f)
f.Reader = bytes.NewReader(src)
return string(src)
}
func (s *File) Write(p []byte) (int, error) {
bb := &bytes.Buffer{}
i, err := bb.Write(p)
s.Reader = bb
return i, err
}
// NewFile takes the name of the file you want to
// write to and a reader to reader from
func NewFile(path string, r io.Reader) *File {
if r == nil {
r = &bytes.Buffer{}
}
if seek, ok := r.(io.Seeker); ok {
seek.Seek(0, 0)
}
abs := path
if !filepath.IsAbs(path) {
abs, _ = filepath.Abs(path)
}
return &File{
Reader: r,
Path: path,
AbsPath: abs,
}
}