1
0
Fork 0
mirror of https://github.com/Luzifer/duplicity-backup.git synced 2024-11-09 15:40:06 +00:00
duplicity-backup/bufferedLineWriter.go
Knut Ahlers 47bf322f28
Lint: Handle several linter errors
Signed-off-by: Knut Ahlers <knut@ahlers.me>
2018-10-08 16:06:28 +02:00

34 lines
614 B
Go

package main
import "strings"
type messageChanWriter struct {
msgChan chan string
buffer []byte
}
func newMessageChanWriter(outputChannel chan string) *messageChanWriter {
return &messageChanWriter{
msgChan: outputChannel,
buffer: []byte{},
}
}
func (m *messageChanWriter) Write(p []byte) (int, error) {
var (
n = len(p)
err error
)
m.buffer = append(m.buffer, p...)
if strings.Contains(string(m.buffer), "\n") {
lines := strings.Split(string(m.buffer), "\n")
for _, l := range lines[:len(lines)-1] {
m.msgChan <- l
}
m.buffer = []byte(lines[len(lines)-1])
}
return n, err
}