
Signed-off-by: Izuru Yakumo <yakumo.izuru@chaotic.ninja> git-svn-id: file:///srv/svn/repo/aya/trunk@67 cec141ff-132a-4243-88a5-ce187bd62f94
63 lines
948 B
Go
63 lines
948 B
Go
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
|
|
}
|