2
2
mirror of https://github.com/octoleo/restic.git synced 2024-11-22 12:55:18 +00:00

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.
This commit is contained in:
Michael Eischer 2020-04-04 19:41:24 +02:00
parent 74bc7141c1
commit 182655bc88
6 changed files with 19 additions and 30 deletions

View File

@ -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

View File

@ -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)

View File

@ -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)
}
}

View File

@ -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

View File

@ -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)
}
}

View File

@ -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()