mirror of
https://github.com/cupcakearmy/autorestic.git
synced 2026-04-02 20:05:23 +00:00
stream the output (#186)
This commit is contained in:
@@ -9,7 +9,6 @@ import (
|
|||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/cupcakearmy/autorestic/internal/colors"
|
"github.com/cupcakearmy/autorestic/internal/colors"
|
||||||
"github.com/cupcakearmy/autorestic/internal/flags"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
type BackendRest struct {
|
type BackendRest struct {
|
||||||
@@ -120,18 +119,15 @@ func (b Backend) validate() error {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
options := ExecuteOptions{Envs: env}
|
options := ExecuteOptions{Envs: env, Silent: true}
|
||||||
// Check if already initialized
|
// Check if already initialized
|
||||||
_, _, err = ExecuteResticCommand(options, "snapshots")
|
_, _, err = ExecuteResticCommand(options, "check")
|
||||||
if err == nil {
|
if err == nil {
|
||||||
return nil
|
return nil
|
||||||
} else {
|
} else {
|
||||||
// If not initialize
|
// If not initialize
|
||||||
colors.Body.Printf("Initializing backend \"%s\"...\n", b.name)
|
colors.Body.Printf("Initializing backend \"%s\"...\n", b.name)
|
||||||
_, out, err := ExecuteResticCommand(options, "init")
|
_, _, err := ExecuteResticCommand(options, "init")
|
||||||
if flags.VERBOSE {
|
|
||||||
colors.Faint.Println(out)
|
|
||||||
}
|
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -147,9 +143,6 @@ func (b Backend) Exec(args []string) error {
|
|||||||
colors.Error.Println(out)
|
colors.Error.Println(out)
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
if flags.VERBOSE {
|
|
||||||
colors.Faint.Println(out)
|
|
||||||
}
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -128,10 +128,9 @@ func InstallRestic() error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func upgradeRestic() error {
|
func upgradeRestic() error {
|
||||||
_, out, err := internal.ExecuteCommand(internal.ExecuteOptions{
|
_, _, err := internal.ExecuteCommand(internal.ExecuteOptions{
|
||||||
Command: "restic",
|
Command: "restic",
|
||||||
}, "self-update")
|
}, "self-update")
|
||||||
colors.Faint.Println(out)
|
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -146,9 +146,6 @@ func (l Location) ExecuteHooks(commands []string, options ExecuteOptions) error
|
|||||||
colors.Error.Println(out)
|
colors.Error.Println(out)
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
if flags.VERBOSE {
|
|
||||||
colors.Faint.Println(out)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
colors.Body.Println("")
|
colors.Body.Println("")
|
||||||
return nil
|
return nil
|
||||||
@@ -284,24 +281,16 @@ func (l Location) Backup(cron bool, specificBackend string) []error {
|
|||||||
for k, v := range env2 {
|
for k, v := range env2 {
|
||||||
env[k+"2"] = v
|
env[k+"2"] = v
|
||||||
}
|
}
|
||||||
_, out, err := ExecuteResticCommand(ExecuteOptions{
|
_, _, err := ExecuteResticCommand(ExecuteOptions{
|
||||||
Envs: env,
|
Envs: env,
|
||||||
}, "copy", md.SnapshotID)
|
}, "copy", md.SnapshotID)
|
||||||
|
|
||||||
if flags.VERBOSE {
|
|
||||||
colors.Faint.Println(out)
|
|
||||||
}
|
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
errors = append(errors, err)
|
errors = append(errors, err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if flags.VERBOSE {
|
|
||||||
colors.Faint.Println(out)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// After hooks
|
// After hooks
|
||||||
@@ -353,10 +342,7 @@ func (l Location) Forget(prune bool, dry bool) error {
|
|||||||
cmd = append(cmd, "--dry-run")
|
cmd = append(cmd, "--dry-run")
|
||||||
}
|
}
|
||||||
cmd = append(cmd, combineOptions("forget", l, backend)...)
|
cmd = append(cmd, combineOptions("forget", l, backend)...)
|
||||||
_, out, err := ExecuteResticCommand(options, cmd...)
|
_, _, err = ExecuteResticCommand(options, cmd...)
|
||||||
if flags.VERBOSE {
|
|
||||||
colors.Faint.Println(out)
|
|
||||||
}
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -9,6 +9,7 @@ import (
|
|||||||
|
|
||||||
"github.com/cupcakearmy/autorestic/internal/colors"
|
"github.com/cupcakearmy/autorestic/internal/colors"
|
||||||
"github.com/cupcakearmy/autorestic/internal/flags"
|
"github.com/cupcakearmy/autorestic/internal/flags"
|
||||||
|
"github.com/fatih/color"
|
||||||
)
|
)
|
||||||
|
|
||||||
var RESTIC_BIN string
|
var RESTIC_BIN string
|
||||||
@@ -26,6 +27,18 @@ type ExecuteOptions struct {
|
|||||||
Command string
|
Command string
|
||||||
Envs map[string]string
|
Envs map[string]string
|
||||||
Dir string
|
Dir string
|
||||||
|
Silent bool
|
||||||
|
}
|
||||||
|
|
||||||
|
type ColoredWriter struct {
|
||||||
|
target io.Writer
|
||||||
|
color *color.Color
|
||||||
|
}
|
||||||
|
|
||||||
|
func (w ColoredWriter) Write(p []byte) (n int, err error) {
|
||||||
|
colored := []byte(w.color.Sprint(string(p)))
|
||||||
|
w.target.Write(colored)
|
||||||
|
return len(p), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func ExecuteCommand(options ExecuteOptions, args ...string) (int, string, error) {
|
func ExecuteCommand(options ExecuteOptions, args ...string) (int, string, error) {
|
||||||
@@ -43,15 +56,24 @@ func ExecuteCommand(options ExecuteOptions, args ...string) (int, string, error)
|
|||||||
|
|
||||||
var out bytes.Buffer
|
var out bytes.Buffer
|
||||||
var error bytes.Buffer
|
var error bytes.Buffer
|
||||||
cmd.Stdout = &out
|
if flags.VERBOSE && !options.Silent {
|
||||||
|
var colored ColoredWriter = ColoredWriter{
|
||||||
|
target: os.Stdout,
|
||||||
|
color: colors.Faint,
|
||||||
|
}
|
||||||
|
mw := io.MultiWriter(colored, &out)
|
||||||
|
cmd.Stdout = mw
|
||||||
|
} else {
|
||||||
|
cmd.Stdout = &out
|
||||||
|
}
|
||||||
cmd.Stderr = &error
|
cmd.Stderr = &error
|
||||||
err := cmd.Run()
|
err := cmd.Run()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
code := -1
|
||||||
if exitError, ok := err.(*exec.ExitError); ok {
|
if exitError, ok := err.(*exec.ExitError); ok {
|
||||||
return exitError.ExitCode(), error.String(), err
|
code = exitError.ExitCode()
|
||||||
} else {
|
|
||||||
return -1, error.String(), err
|
|
||||||
}
|
}
|
||||||
|
return code, error.String(), err
|
||||||
}
|
}
|
||||||
return 0, out.String(), nil
|
return 0, out.String(), nil
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user