-
Notifications
You must be signed in to change notification settings - Fork 39
/
write.go
62 lines (49 loc) · 948 Bytes
/
write.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
package gcss
import (
"bufio"
"io"
"os"
)
// writeFlusher is the interface that groups the basic Write and Flush methods.
type writeFlusher interface {
io.Writer
Flush() error
}
var newBufWriter = func(w io.Writer) writeFlusher {
return bufio.NewWriter(w)
}
// write writes the input byte data to the CSS file.
func write(path string, bc <-chan []byte, berrc <-chan error) (<-chan struct{}, <-chan error) {
done := make(chan struct{})
errc := make(chan error)
go func() {
f, err := os.Create(path)
if err != nil {
errc <- err
return
}
defer f.Close()
w := newBufWriter(f)
for {
select {
case b, ok := <-bc:
if !ok {
if err := w.Flush(); err != nil {
errc <- err
return
}
done <- struct{}{}
return
}
if _, err := w.Write(b); err != nil {
errc <- err
return
}
case err := <-berrc:
errc <- err
return
}
}
}()
return done, errc
}