mirror of
https://github.com/Luzifer/duplicity-backup.git
synced 2024-11-09 15:40:06 +00:00
Provide live output for duplicity
This commit is contained in:
parent
4c65ecd5c7
commit
0b8f0d27c2
2 changed files with 42 additions and 3 deletions
32
bufferedLineWriter.go
Normal file
32
bufferedLineWriter.go
Normal 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
13
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 {
|
||||
|
|
Loading…
Reference in a new issue