66
|
1 package gcss
|
|
2
|
|
3 import (
|
|
4 "bufio"
|
|
5 "io"
|
|
6 "os"
|
|
7 )
|
|
8
|
|
9 // writeFlusher is the interface that groups the basic Write and Flush methods.
|
|
10 type writeFlusher interface {
|
|
11 io.Writer
|
|
12 Flush() error
|
|
13 }
|
|
14
|
|
15 var newBufWriter = func(w io.Writer) writeFlusher {
|
|
16 return bufio.NewWriter(w)
|
|
17 }
|
|
18
|
|
19 // write writes the input byte data to the CSS file.
|
|
20 func write(path string, bc <-chan []byte, berrc <-chan error) (<-chan struct{}, <-chan error) {
|
|
21 done := make(chan struct{})
|
|
22 errc := make(chan error)
|
|
23
|
|
24 go func() {
|
|
25 f, err := os.Create(path)
|
|
26
|
|
27 if err != nil {
|
|
28 errc <- err
|
|
29 return
|
|
30 }
|
|
31
|
|
32 defer f.Close()
|
|
33
|
|
34 w := newBufWriter(f)
|
|
35
|
|
36 for {
|
|
37 select {
|
|
38 case b, ok := <-bc:
|
|
39 if !ok {
|
|
40 if err := w.Flush(); err != nil {
|
|
41 errc <- err
|
|
42 return
|
|
43 }
|
|
44
|
|
45 done <- struct{}{}
|
|
46
|
|
47 return
|
|
48 }
|
|
49
|
|
50 if _, err := w.Write(b); err != nil {
|
|
51 errc <- err
|
|
52 return
|
|
53 }
|
|
54 case err := <-berrc:
|
|
55 errc <- err
|
|
56 return
|
|
57 }
|
|
58 }
|
|
59 }()
|
|
60
|
|
61 return done, errc
|
|
62 }
|