From c6a2c52b94da7b131884292fd1728f4d4aff50d2 Mon Sep 17 00:00:00 2001 From: cupcakearmy Date: Wed, 27 Apr 2022 00:12:33 +0200 Subject: [PATCH] stream the output --- internal/backend.go | 13 +++---------- internal/bins/bins.go | 3 +-- internal/location.go | 18 ++---------------- internal/utils.go | 30 ++++++++++++++++++++++++++---- 4 files changed, 32 insertions(+), 32 deletions(-) diff --git a/internal/backend.go b/internal/backend.go index 745b800..40bb9d6 100644 --- a/internal/backend.go +++ b/internal/backend.go @@ -9,7 +9,6 @@ import ( "strings" "github.com/cupcakearmy/autorestic/internal/colors" - "github.com/cupcakearmy/autorestic/internal/flags" ) type BackendRest struct { @@ -120,18 +119,15 @@ func (b Backend) validate() error { if err != nil { return err } - options := ExecuteOptions{Envs: env} + options := ExecuteOptions{Envs: env, Silent: true} // Check if already initialized - _, _, err = ExecuteResticCommand(options, "snapshots") + _, _, err = ExecuteResticCommand(options, "check") if err == nil { return nil } else { // If not initialize colors.Body.Printf("Initializing backend \"%s\"...\n", b.name) - _, out, err := ExecuteResticCommand(options, "init") - if flags.VERBOSE { - colors.Faint.Println(out) - } + _, _, err := ExecuteResticCommand(options, "init") return err } } @@ -147,9 +143,6 @@ func (b Backend) Exec(args []string) error { colors.Error.Println(out) return err } - if flags.VERBOSE { - colors.Faint.Println(out) - } return nil } diff --git a/internal/bins/bins.go b/internal/bins/bins.go index 104efcb..291e201 100644 --- a/internal/bins/bins.go +++ b/internal/bins/bins.go @@ -128,10 +128,9 @@ func InstallRestic() error { } func upgradeRestic() error { - _, out, err := internal.ExecuteCommand(internal.ExecuteOptions{ + _, _, err := internal.ExecuteCommand(internal.ExecuteOptions{ Command: "restic", }, "self-update") - colors.Faint.Println(out) return err } diff --git a/internal/location.go b/internal/location.go index b715105..0bfb853 100644 --- a/internal/location.go +++ b/internal/location.go @@ -146,9 +146,6 @@ func (l Location) ExecuteHooks(commands []string, options ExecuteOptions) error colors.Error.Println(out) return err } - if flags.VERBOSE { - colors.Faint.Println(out) - } } colors.Body.Println("") return nil @@ -284,24 +281,16 @@ func (l Location) Backup(cron bool, specificBackend string) []error { for k, v := range env2 { env[k+"2"] = v } - _, out, err := ExecuteResticCommand(ExecuteOptions{ + _, _, err := ExecuteResticCommand(ExecuteOptions{ Envs: env, }, "copy", md.SnapshotID) - if flags.VERBOSE { - colors.Faint.Println(out) - } - if err != nil { errors = append(errors, err) } } } } - - if flags.VERBOSE { - colors.Faint.Println(out) - } } // After hooks @@ -353,10 +342,7 @@ func (l Location) Forget(prune bool, dry bool) error { cmd = append(cmd, "--dry-run") } cmd = append(cmd, combineOptions("forget", l, backend)...) - _, out, err := ExecuteResticCommand(options, cmd...) - if flags.VERBOSE { - colors.Faint.Println(out) - } + _, _, err = ExecuteResticCommand(options, cmd...) if err != nil { return err } diff --git a/internal/utils.go b/internal/utils.go index 42389f1..91d312e 100644 --- a/internal/utils.go +++ b/internal/utils.go @@ -9,6 +9,7 @@ import ( "github.com/cupcakearmy/autorestic/internal/colors" "github.com/cupcakearmy/autorestic/internal/flags" + "github.com/fatih/color" ) var RESTIC_BIN string @@ -26,6 +27,18 @@ type ExecuteOptions struct { Command string Envs map[string]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) { @@ -43,15 +56,24 @@ func ExecuteCommand(options ExecuteOptions, args ...string) (int, string, error) var out 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 err := cmd.Run() if err != nil { + code := -1 if exitError, ok := err.(*exec.ExitError); ok { - return exitError.ExitCode(), error.String(), err - } else { - return -1, error.String(), err + code = exitError.ExitCode() } + return code, error.String(), err } return 0, out.String(), nil }