mirror of
https://github.com/octoleo/restic.git
synced 2024-11-22 21:05:10 +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:
parent
74bc7141c1
commit
182655bc88
@ -1,7 +1,6 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
|
||||||
"os"
|
"os"
|
||||||
"os/signal"
|
"os/signal"
|
||||||
"sync"
|
"sync"
|
||||||
@ -17,8 +16,6 @@ var cleanupHandlers struct {
|
|||||||
ch chan os.Signal
|
ch chan os.Signal
|
||||||
}
|
}
|
||||||
|
|
||||||
var stderr = os.Stderr
|
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
cleanupHandlers.ch = make(chan os.Signal, 1)
|
cleanupHandlers.ch = make(chan os.Signal, 1)
|
||||||
go CleanupHandler(cleanupHandlers.ch)
|
go CleanupHandler(cleanupHandlers.ch)
|
||||||
@ -51,7 +48,7 @@ func RunCleanupHandlers() {
|
|||||||
for _, f := range cleanupHandlers.list {
|
for _, f := range cleanupHandlers.list {
|
||||||
err := f()
|
err := f()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Fprintf(stderr, "error in cleanup handler: %v\n", err)
|
Warnf("error in cleanup handler: %v\n", err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
cleanupHandlers.list = nil
|
cleanupHandlers.list = nil
|
||||||
@ -61,7 +58,7 @@ func RunCleanupHandlers() {
|
|||||||
func CleanupHandler(c <-chan os.Signal) {
|
func CleanupHandler(c <-chan os.Signal) {
|
||||||
for s := range c {
|
for s := range c {
|
||||||
debug.Log("signal %v received, cleaning up", s)
|
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
|
code := 0
|
||||||
|
|
||||||
|
@ -2,7 +2,6 @@ package main
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
|
||||||
"os"
|
"os"
|
||||||
|
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
@ -76,7 +75,7 @@ func runCat(gopts GlobalOptions, args []string) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
fmt.Println(string(buf))
|
Println(string(buf))
|
||||||
return nil
|
return nil
|
||||||
case "index":
|
case "index":
|
||||||
buf, err := repo.LoadAndDecrypt(gopts.ctx, nil, restic.IndexFile, id)
|
buf, err := repo.LoadAndDecrypt(gopts.ctx, nil, restic.IndexFile, id)
|
||||||
@ -99,8 +98,7 @@ func runCat(gopts GlobalOptions, args []string) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
fmt.Println(string(buf))
|
Println(string(buf))
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
case "key":
|
case "key":
|
||||||
h := restic.Handle{Type: restic.KeyFile, Name: id.String()}
|
h := restic.Handle{Type: restic.KeyFile, Name: id.String()}
|
||||||
@ -120,7 +118,7 @@ func runCat(gopts GlobalOptions, args []string) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
fmt.Println(string(buf))
|
Println(string(buf))
|
||||||
return nil
|
return nil
|
||||||
case "masterkey":
|
case "masterkey":
|
||||||
buf, err := json.MarshalIndent(repo.Key(), "", " ")
|
buf, err := json.MarshalIndent(repo.Key(), "", " ")
|
||||||
@ -128,7 +126,7 @@ func runCat(gopts GlobalOptions, args []string) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
fmt.Println(string(buf))
|
Println(string(buf))
|
||||||
return nil
|
return nil
|
||||||
case "lock":
|
case "lock":
|
||||||
lock, err := restic.LoadLock(gopts.ctx, repo, id)
|
lock, err := restic.LoadLock(gopts.ctx, repo, id)
|
||||||
@ -141,8 +139,7 @@ func runCat(gopts GlobalOptions, args []string) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
fmt.Println(string(buf))
|
Println(string(buf))
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -162,7 +159,7 @@ func runCat(gopts GlobalOptions, args []string) error {
|
|||||||
|
|
||||||
hash := restic.Hash(buf)
|
hash := restic.Hash(buf)
|
||||||
if !hash.Equal(id) {
|
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)
|
_, err = os.Stdout.Write(buf)
|
||||||
|
@ -3,7 +3,6 @@ package main
|
|||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"os"
|
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
@ -235,7 +234,7 @@ func runCheck(opts CheckOptions, gopts GlobalOptions, args []string) error {
|
|||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
errorsFound = true
|
errorsFound = true
|
||||||
fmt.Fprintf(os.Stderr, "%v\n", err)
|
Warnf("%v\n", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
if orphanedPacks > 0 {
|
if orphanedPacks > 0 {
|
||||||
@ -249,12 +248,12 @@ func runCheck(opts CheckOptions, gopts GlobalOptions, args []string) error {
|
|||||||
for err := range errChan {
|
for err := range errChan {
|
||||||
errorsFound = true
|
errorsFound = true
|
||||||
if e, ok := err.(checker.TreeError); ok {
|
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 {
|
for _, treeErr := range e.Errors {
|
||||||
fmt.Fprintf(os.Stderr, " %v\n", treeErr)
|
Warnf(" %v\n", treeErr)
|
||||||
}
|
}
|
||||||
} else {
|
} 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 {
|
for err := range errChan {
|
||||||
errorsFound = true
|
errorsFound = true
|
||||||
fmt.Fprintf(os.Stderr, "%v\n", err)
|
Warnf("%v\n", err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -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)
|
blobs, err := pack.List(repo.Key(), restic.ReaderAt(repo.Backend(), h), size)
|
||||||
if err != nil {
|
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
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -112,7 +112,7 @@ func printPacks(repo *repository.Repository, wr io.Writer) error {
|
|||||||
|
|
||||||
func dumpIndexes(repo restic.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 {
|
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)
|
idx, err := repository.LoadIndex(context.TODO(), repo, id)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -151,13 +151,13 @@ func runDebugDump(gopts GlobalOptions, args []string) error {
|
|||||||
case "packs":
|
case "packs":
|
||||||
return printPacks(repo, gopts.stdout)
|
return printPacks(repo, gopts.stdout)
|
||||||
case "all":
|
case "all":
|
||||||
fmt.Printf("snapshots:\n")
|
Printf("snapshots:\n")
|
||||||
err := debugPrintSnapshots(repo, gopts.stdout)
|
err := debugPrintSnapshots(repo, gopts.stdout)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
fmt.Printf("\nindexes:\n")
|
Printf("\nindexes:\n")
|
||||||
err = dumpIndexes(repo, gopts.stdout)
|
err = dumpIndexes(repo, gopts.stdout)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
|
@ -1,8 +1,6 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
|
||||||
|
|
||||||
"github.com/restic/restic/internal/errors"
|
"github.com/restic/restic/internal/errors"
|
||||||
"github.com/restic/restic/internal/index"
|
"github.com/restic/restic/internal/index"
|
||||||
"github.com/restic/restic/internal/restic"
|
"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 _, pack := range idx.Packs {
|
||||||
for _, entry := range pack.Entries {
|
for _, entry := range pack.Entries {
|
||||||
fmt.Printf("%v %v\n", entry.Type, entry.ID)
|
Printf("%v %v\n", entry.Type, entry.ID)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -2,8 +2,6 @@ package main
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
|
||||||
"os"
|
|
||||||
"sync"
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
@ -79,7 +77,7 @@ func refreshLocks(wg *sync.WaitGroup, done <-chan struct{}) {
|
|||||||
for _, lock := range globalLocks.locks {
|
for _, lock := range globalLocks.locks {
|
||||||
err := lock.Refresh(context.TODO())
|
err := lock.Refresh(context.TODO())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Fprintf(os.Stderr, "unable to refresh lock: %v\n", err)
|
Warnf("unable to refresh lock: %v\n", err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
globalLocks.Unlock()
|
globalLocks.Unlock()
|
||||||
|
Loading…
Reference in New Issue
Block a user