Mercurial > yakumo_izuru > aya
diff vendor/github.com/yosssi/gcss/write.go @ 66:787b5ee0289d draft
Use vendored modules
Signed-off-by: Izuru Yakumo <yakumo.izuru@chaotic.ninja>
author | yakumo.izuru |
---|---|
date | Sun, 23 Jul 2023 13:18:53 +0000 |
parents | |
children |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/vendor/github.com/yosssi/gcss/write.go Sun Jul 23 13:18:53 2023 +0000 @@ -0,0 +1,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 +}