/
copy.go
71 lines (57 loc) · 1.2 KB
/
copy.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
package copy
// github.com/yosssi/gcss binding for slurp.
// No Configuration required.
import (
"bytes"
"strings"
"sync"
"github.com/omeid/slurp"
)
// File represents a file.
type File struct {
path string
dir string
data []byte
}
// NewFile creates and returns a file.
func NewFile(path, dir string, data []byte) *File {
return &File{
path: path,
dir: dir,
data: data,
}
}
// Copy the file content
func Copy(c *slurp.C, keepath bool) slurp.Stage {
return func(in <-chan slurp.File, out chan<- slurp.File) {
fs := []*File{}
var wg sync.WaitGroup
defer wg.Wait() //Wait before all templates are executed.
for file := range in {
buf := new(bytes.Buffer)
_, err := buf.ReadFrom(file.Reader)
file.Close()
if err != nil {
c.Error(err)
continue
}
s, err := file.Stat()
if err != nil {
c.Error(err)
break
}
name := s.Name()
file.Dir = ""
if keepath {
name = strings.Join(strings.Split(file.Path, "/")[1:], "/")
file.Dir = strings.Split(file.Dir, "/")[0]
}
f := NewFile(name, file.Dir, buf.Bytes())
fs = append(fs, f)
file.Path = name
file.Reader = buf
file.FileInfo.SetSize(int64(buf.Len()))
out <- file
}
}
}