From a43733d552f386e9ede5cb2a9643814afab9d6f3 Mon Sep 17 00:00:00 2001 From: Alexander Neumann Date: Sun, 21 Jun 2015 13:25:26 +0200 Subject: [PATCH] Introduce `Verbosef` --- cmd/restic/cmd_backup.go | 8 ++++---- cmd/restic/cmd_init.go | 10 +++++----- cmd/restic/cmd_list.go | 11 ++--------- cmd/restic/cmd_restore.go | 2 +- cmd/restic/global.go | 12 ++++++++---- 5 files changed, 20 insertions(+), 23 deletions(-) diff --git a/cmd/restic/cmd_backup.go b/cmd/restic/cmd_backup.go index 86bde9dbb..088b3afab 100644 --- a/cmd/restic/cmd_backup.go +++ b/cmd/restic/cmd_backup.go @@ -234,7 +234,7 @@ func (cmd CmdBackup) Execute(args []string) error { return fmt.Errorf("invalid id %q: %v", cmd.Parent, err) } - cmd.global.Printf("found parent snapshot %v\n", parentSnapshotID.Str()) + cmd.global.Verbosef("found parent snapshot %v\n", parentSnapshotID.Str()) } // Find last snapshot to set it as parent, if not already set @@ -245,11 +245,11 @@ func (cmd CmdBackup) Execute(args []string) error { } if parentSnapshotID != nil { - cmd.global.Printf("using parent snapshot %v\n", parentSnapshotID) + cmd.global.Verbosef("using parent snapshot %v\n", parentSnapshotID) } } - cmd.global.Printf("scan %v\n", target) + cmd.global.Verbosef("scan %v\n", target) stat, err := restic.Scan(target, cmd.newScanProgress()) @@ -271,7 +271,7 @@ func (cmd CmdBackup) Execute(args []string) error { return err } - cmd.global.Printf("snapshot %s saved\n", id.Str()) + cmd.global.Verbosef("snapshot %s saved\n", id.Str()) return nil } diff --git a/cmd/restic/cmd_init.go b/cmd/restic/cmd_init.go index 1e75ee5f9..e4333b06e 100644 --- a/cmd/restic/cmd_init.go +++ b/cmd/restic/cmd_init.go @@ -32,11 +32,11 @@ func (cmd CmdInit) Execute(args []string) error { cmd.global.Exitf(1, "creating key in backend at %s failed: %v\n", cmd.global.Repo, err) } - cmd.global.Printf("created restic backend %v at %s\n", s.Config.ID[:10], cmd.global.Repo) - cmd.global.Printf("\n") - cmd.global.Printf("Please note that knowledge of your password is required to access\n") - cmd.global.Printf("the repository. Losing your password means that your data is\n") - cmd.global.Printf("irrecoverably lost.\n") + cmd.global.Verbosef("created restic backend %v at %s\n", s.Config.ID[:10], cmd.global.Repo) + cmd.global.Verbosef("\n") + cmd.global.Verbosef("Please note that knowledge of your password is required to access\n") + cmd.global.Verbosef("the repository. Losing your password means that your data is\n") + cmd.global.Verbosef("irrecoverably lost.\n") return nil } diff --git a/cmd/restic/cmd_list.go b/cmd/restic/cmd_list.go index 4d5cf0ab1..bd01e6eda 100644 --- a/cmd/restic/cmd_list.go +++ b/cmd/restic/cmd_list.go @@ -3,14 +3,11 @@ package main import ( "errors" "fmt" - "io" - "os" "github.com/restic/restic/backend" ) type CmdList struct { - w io.Writer global *GlobalOptions } @@ -29,10 +26,6 @@ func (cmd CmdList) Usage() string { } func (cmd CmdList) Execute(args []string) error { - if cmd.w == nil { - cmd.w = os.Stdout - } - if len(args) != 1 { return fmt.Errorf("type not specified, Usage: %s", cmd.Usage()) } @@ -51,7 +44,7 @@ func (cmd CmdList) Execute(args []string) error { } for blob := range s.Index().Each(nil) { - fmt.Fprintln(cmd.w, blob.ID) + cmd.global.Printf("%s\n", blob.ID) } return nil @@ -70,7 +63,7 @@ func (cmd CmdList) Execute(args []string) error { } for id := range s.List(t, nil) { - fmt.Fprintf(cmd.w, "%s\n", id) + cmd.global.Printf("%s\n", id) } return nil diff --git a/cmd/restic/cmd_restore.go b/cmd/restic/cmd_restore.go index 183798932..ef3fb577a 100644 --- a/cmd/restic/cmd_restore.go +++ b/cmd/restic/cmd_restore.go @@ -83,7 +83,7 @@ func (cmd CmdRestore) Execute(args []string) error { } } - cmd.global.Printf("restoring %s to %s\n", res.Snapshot(), target) + cmd.global.Verbosef("restoring %s to %s\n", res.Snapshot(), target) err = res.RestoreTo(target) if err != nil { diff --git a/cmd/restic/global.go b/cmd/restic/global.go index 2f7ffe27e..7c411a85c 100644 --- a/cmd/restic/global.go +++ b/cmd/restic/global.go @@ -30,10 +30,6 @@ var globalOpts = GlobalOptions{stdout: os.Stdout} var parser = flags.NewParser(&globalOpts, flags.Default) func (o GlobalOptions) Printf(format string, args ...interface{}) { - if o.Quiet { - return - } - _, err := fmt.Fprintf(o.stdout, format, args...) if err != nil { fmt.Fprintf(os.Stderr, "unable to write to stdout: %v\n", err) @@ -41,6 +37,14 @@ func (o GlobalOptions) Printf(format string, args ...interface{}) { } } +func (o GlobalOptions) Verbosef(format string, args ...interface{}) { + if o.Quiet { + return + } + + o.Printf(format, args...) +} + func (o GlobalOptions) ShowProgress() bool { if o.Quiet { return false