mirror of
https://github.com/Luzifer/duplicity-backup.git
synced 2024-11-09 23:50:08 +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
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"log"
|
"log"
|
||||||
"os"
|
"os"
|
||||||
|
@ -162,14 +161,22 @@ func execute(config *configFile, argv []string) error {
|
||||||
logf("[DBG] Command: %s %s", duplicityBinary, strings.Join(commandLine, " "))
|
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 := exec.Command(duplicityBinary, commandLine...)
|
||||||
cmd.Stdout = output
|
cmd.Stdout = output
|
||||||
cmd.Stderr = output
|
cmd.Stderr = output
|
||||||
cmd.Env = envMapToList(env)
|
cmd.Env = envMapToList(env)
|
||||||
err = cmd.Run()
|
err = cmd.Run()
|
||||||
|
|
||||||
logf("%s", output.String())
|
close(msgChan)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logf("[ERR] Execution of duplicity command was unsuccessful! (exit-code was non-zero)")
|
logf("[ERR] Execution of duplicity command was unsuccessful! (exit-code was non-zero)")
|
||||||
} else {
|
} else {
|
||||||
|
|
Loading…
Reference in a new issue