From 182655bc883b85586e389c3d7ff4f519fbdbba57 Mon Sep 17 00:00:00 2001 From: Michael Eischer Date: Sat, 4 Apr 2020 19:41:24 +0200 Subject: [PATCH] Replace fmt.Printf/Println/Fprintf with wrapper functions cmd/restic/globals.go already provides Printf, Println and Warnf wrapper which get their output streams from the globalOptions object. This allows for stream replacements when testing. --- cmd/restic/cleanup.go | 7 ++----- cmd/restic/cmd_cat.go | 15 ++++++--------- cmd/restic/cmd_check.go | 11 +++++------ cmd/restic/cmd_debug.go | 8 ++++---- cmd/restic/cmd_list.go | 4 +--- cmd/restic/lock.go | 4 +--- 6 files changed, 19 insertions(+), 30 deletions(-) diff --git a/cmd/restic/cleanup.go b/cmd/restic/cleanup.go index 2f3c15683..738ec590b 100644 --- a/cmd/restic/cleanup.go +++ b/cmd/restic/cleanup.go @@ -1,7 +1,6 @@ package main import ( - "fmt" "os" "os/signal" "sync" @@ -17,8 +16,6 @@ var cleanupHandlers struct { ch chan os.Signal } -var stderr = os.Stderr - func init() { cleanupHandlers.ch = make(chan os.Signal, 1) go CleanupHandler(cleanupHandlers.ch) @@ -51,7 +48,7 @@ func RunCleanupHandlers() { for _, f := range cleanupHandlers.list { err := f() if err != nil { - fmt.Fprintf(stderr, "error in cleanup handler: %v\n", err) + Warnf("error in cleanup handler: %v\n", err) } } cleanupHandlers.list = nil @@ -61,7 +58,7 @@ func RunCleanupHandlers() { func CleanupHandler(c <-chan os.Signal) { for s := range c { debug.Log("signal %v received, cleaning up", s) - fmt.Fprintf(stderr, "%ssignal %v received, cleaning up\n", ClearLine(), s) + Warnf("%ssignal %v received, cleaning up\n", ClearLine(), s) code := 0 diff --git a/cmd/restic/cmd_cat.go b/cmd/restic/cmd_cat.go index 8d2535585..01cb8d278 100644 --- a/cmd/restic/cmd_cat.go +++ b/cmd/restic/cmd_cat.go @@ -2,7 +2,6 @@ package main import ( "encoding/json" - "fmt" "os" "github.com/spf13/cobra" @@ -76,7 +75,7 @@ func runCat(gopts GlobalOptions, args []string) error { return err } - fmt.Println(string(buf)) + Println(string(buf)) return nil case "index": buf, err := repo.LoadAndDecrypt(gopts.ctx, nil, restic.IndexFile, id) @@ -99,8 +98,7 @@ func runCat(gopts GlobalOptions, args []string) error { return err } - fmt.Println(string(buf)) - + Println(string(buf)) return nil case "key": h := restic.Handle{Type: restic.KeyFile, Name: id.String()} @@ -120,7 +118,7 @@ func runCat(gopts GlobalOptions, args []string) error { return err } - fmt.Println(string(buf)) + Println(string(buf)) return nil case "masterkey": buf, err := json.MarshalIndent(repo.Key(), "", " ") @@ -128,7 +126,7 @@ func runCat(gopts GlobalOptions, args []string) error { return err } - fmt.Println(string(buf)) + Println(string(buf)) return nil case "lock": lock, err := restic.LoadLock(gopts.ctx, repo, id) @@ -141,8 +139,7 @@ func runCat(gopts GlobalOptions, args []string) error { return err } - fmt.Println(string(buf)) - + Println(string(buf)) return nil } @@ -162,7 +159,7 @@ func runCat(gopts GlobalOptions, args []string) error { hash := restic.Hash(buf) if !hash.Equal(id) { - fmt.Fprintf(stderr, "Warning: hash of data does not match ID, want\n %v\ngot:\n %v\n", id.String(), hash.String()) + Warnf("Warning: hash of data does not match ID, want\n %v\ngot:\n %v\n", id.String(), hash.String()) } _, err = os.Stdout.Write(buf) diff --git a/cmd/restic/cmd_check.go b/cmd/restic/cmd_check.go index 34fd61ce1..35729af6d 100644 --- a/cmd/restic/cmd_check.go +++ b/cmd/restic/cmd_check.go @@ -3,7 +3,6 @@ package main import ( "fmt" "io/ioutil" - "os" "strconv" "strings" "time" @@ -235,7 +234,7 @@ func runCheck(opts CheckOptions, gopts GlobalOptions, args []string) error { continue } errorsFound = true - fmt.Fprintf(os.Stderr, "%v\n", err) + Warnf("%v\n", err) } if orphanedPacks > 0 { @@ -249,12 +248,12 @@ func runCheck(opts CheckOptions, gopts GlobalOptions, args []string) error { for err := range errChan { errorsFound = true if e, ok := err.(checker.TreeError); ok { - fmt.Fprintf(os.Stderr, "error for tree %v:\n", e.ID.Str()) + Warnf("error for tree %v:\n", e.ID.Str()) for _, treeErr := range e.Errors { - fmt.Fprintf(os.Stderr, " %v\n", treeErr) + Warnf(" %v\n", treeErr) } } else { - fmt.Fprintf(os.Stderr, "error: %v\n", err) + Warnf("error: %v\n", err) } } @@ -289,7 +288,7 @@ func runCheck(opts CheckOptions, gopts GlobalOptions, args []string) error { for err := range errChan { errorsFound = true - fmt.Fprintf(os.Stderr, "%v\n", err) + Warnf("%v\n", err) } } diff --git a/cmd/restic/cmd_debug.go b/cmd/restic/cmd_debug.go index bbba32cc3..6552a0ddb 100644 --- a/cmd/restic/cmd_debug.go +++ b/cmd/restic/cmd_debug.go @@ -89,7 +89,7 @@ func printPacks(repo *repository.Repository, wr io.Writer) error { blobs, err := pack.List(repo.Key(), restic.ReaderAt(repo.Backend(), h), size) if err != nil { - fmt.Fprintf(globalOptions.stderr, "error for pack %v: %v\n", id.Str(), err) + Warnf("error for pack %v: %v\n", id.Str(), err) return nil } @@ -112,7 +112,7 @@ func printPacks(repo *repository.Repository, wr io.Writer) error { func dumpIndexes(repo restic.Repository, wr io.Writer) error { return repo.List(context.TODO(), restic.IndexFile, func(id restic.ID, size int64) error { - fmt.Printf("index_id: %v\n", id) + Printf("index_id: %v\n", id) idx, err := repository.LoadIndex(context.TODO(), repo, id) if err != nil { @@ -151,13 +151,13 @@ func runDebugDump(gopts GlobalOptions, args []string) error { case "packs": return printPacks(repo, gopts.stdout) case "all": - fmt.Printf("snapshots:\n") + Printf("snapshots:\n") err := debugPrintSnapshots(repo, gopts.stdout) if err != nil { return err } - fmt.Printf("\nindexes:\n") + Printf("\nindexes:\n") err = dumpIndexes(repo, gopts.stdout) if err != nil { return err diff --git a/cmd/restic/cmd_list.go b/cmd/restic/cmd_list.go index 56b9b9f86..990ca6b9e 100644 --- a/cmd/restic/cmd_list.go +++ b/cmd/restic/cmd_list.go @@ -1,8 +1,6 @@ package main import ( - "fmt" - "github.com/restic/restic/internal/errors" "github.com/restic/restic/internal/index" "github.com/restic/restic/internal/restic" @@ -69,7 +67,7 @@ func runList(cmd *cobra.Command, opts GlobalOptions, args []string) error { for _, pack := range idx.Packs { for _, entry := range pack.Entries { - fmt.Printf("%v %v\n", entry.Type, entry.ID) + Printf("%v %v\n", entry.Type, entry.ID) } } diff --git a/cmd/restic/lock.go b/cmd/restic/lock.go index d267bc54e..2b4ac94d5 100644 --- a/cmd/restic/lock.go +++ b/cmd/restic/lock.go @@ -2,8 +2,6 @@ package main import ( "context" - "fmt" - "os" "sync" "time" @@ -79,7 +77,7 @@ func refreshLocks(wg *sync.WaitGroup, done <-chan struct{}) { for _, lock := range globalLocks.locks { err := lock.Refresh(context.TODO()) if err != nil { - fmt.Fprintf(os.Stderr, "unable to refresh lock: %v\n", err) + Warnf("unable to refresh lock: %v\n", err) } } globalLocks.Unlock()