diff --git a/bufferedLineWriter.go b/bufferedLineWriter.go new file mode 100644 index 0000000..269e21e --- /dev/null +++ b/bufferedLineWriter.go @@ -0,0 +1,32 @@ +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) (n int, err error) { + n = len(p) + err = nil + + 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 +} diff --git a/main.go b/main.go index d8d82d0..5cabe13 100644 --- a/main.go +++ b/main.go @@ -1,7 +1,6 @@ package main import ( - "bytes" "fmt" "log" "os" @@ -162,14 +161,22 @@ func execute(config *configFile, argv []string) error { logf("[DBG] Command: %s %s", duplicityBinary, strings.Join(commandLine, " ")) } - output := bytes.NewBuffer([]byte{}) + msgChan := make(chan string, 10) + go func(c chan string) { + for l := range c { + logf(l) + } + }(msgChan) + + output := NewMessageChanWriter(msgChan) cmd := exec.Command(duplicityBinary, commandLine...) cmd.Stdout = output cmd.Stderr = output cmd.Env = envMapToList(env) err = cmd.Run() - logf("%s", output.String()) + close(msgChan) + if err != nil { logf("[ERR] Execution of duplicity command was unsuccessful! (exit-code was non-zero)") } else {