diff --git a/cmd/restic/cmd_backup.go b/cmd/restic/cmd_backup.go index 08a5ff46d..96e4f01eb 100644 --- a/cmd/restic/cmd_backup.go +++ b/cmd/restic/cmd_backup.go @@ -97,7 +97,7 @@ func (cmd CmdBackup) Usage() string { } func newScanProgress() *restic.Progress { - if !terminal.IsTerminal(int(os.Stdout.Fd())) { + if disableProgress() { return nil } @@ -113,7 +113,7 @@ func newScanProgress() *restic.Progress { } func newArchiveProgress(todo restic.Stat) *restic.Progress { - if !terminal.IsTerminal(int(os.Stdout.Fd())) { + if disableProgress() { return nil } @@ -194,7 +194,7 @@ func (cmd CmdBackup) Execute(args []string) error { return fmt.Errorf("invalid id %q: %v", cmd.Parent, err) } - fmt.Printf("found parent snapshot %v\n", parentSnapshotID) + verbosePrintf("found parent snapshot %v\n", parentSnapshotID.Str()) } // Find last snapshot to set it as parent, if not already set @@ -226,11 +226,11 @@ func (cmd CmdBackup) Execute(args []string) error { } if parentSnapshotID != nil { - fmt.Printf("using parent snapshot %v\n", parentSnapshotID) + verbosePrintf("using parent snapshot %v\n", parentSnapshotID) } } - fmt.Printf("scan %v\n", target) + verbosePrintf("scan %v\n", target) stat, err := restic.Scan(target, newScanProgress()) @@ -252,12 +252,7 @@ func (cmd CmdBackup) Execute(args []string) error { return err } - plen, err := s.PrefixLength(backend.Snapshot) - if err != nil { - return err - } - - fmt.Printf("snapshot %s saved\n", id[:plen/2]) + verbosePrintf("snapshot %s saved\n", id.Str()) return nil } diff --git a/cmd/restic/main.go b/cmd/restic/main.go index 5dbf9eb13..b34755c84 100644 --- a/cmd/restic/main.go +++ b/cmd/restic/main.go @@ -20,8 +20,9 @@ import ( var version = "compiled manually" var opts struct { - Repo string `short:"r" long:"repo" description:"Repository directory to backup to/restore from"` - CacheDir string ` long:"cache-dir" description:"Directory to use as a local cache"` + Repo string `short:"r" long:"repo" description:"Repository directory to backup to/restore from"` + CacheDir string ` long:"cache-dir" description:"Directory to use as a local cache"` + Quiet bool `short:"q" long:"quiet" default:"false" description:"Do not output comprehensive progress report"` password string } @@ -47,6 +48,34 @@ func readPassword(prompt string) string { return string(pw) } +func disableProgress() bool { + if opts.Quiet { + return true + } + + if !terminal.IsTerminal(int(os.Stdout.Fd())) { + return true + } + + return false +} + +func silenceRequested() bool { + if opts.Quiet { + return true + } + + return false +} + +func verbosePrintf(format string, args ...interface{}) { + if silenceRequested() { + return + } + + fmt.Printf(format, args...) +} + type CmdInit struct{} func (cmd CmdInit) Execute(args []string) error { @@ -78,10 +107,11 @@ func (cmd CmdInit) Execute(args []string) error { os.Exit(1) } - fmt.Printf("created restic backend %v at %s\n", s.Config.ID[:10], opts.Repo) - - fmt.Println("Please note that knowledge of your password is required to access the repository.") - fmt.Println("Losing your password means that your data is irrecoverably lost.") + verbosePrintf("created restic backend %v at %s\n", s.Config.ID[:10], opts.Repo) + verbosePrintf("\n") + verbosePrintf("Please note that knowledge of your password is required to access\n") + verbosePrintf("the repository. Losing your password means that your data is\n") + verbosePrintf("irrecoverably lost.\n") return nil }