1
0
mirror of https://github.com/Luzifer/duplicity-backup.git synced 2024-09-19 16:32:55 +00:00

Provide live output for duplicity

This commit is contained in:
Knut Ahlers 2016-05-23 13:19:25 +02:00
parent 4c65ecd5c7
commit 0b8f0d27c2
Signed by: luzifer
GPG Key ID: DC2729FDD34BE99E
2 changed files with 42 additions and 3 deletions

32
bufferedLineWriter.go Normal file
View File

@ -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
}

13
main.go
View File

@ -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 {