Refactor terminal recognition, add --quiet parameter

This commit is contained in:
Alexander Neumann 2015-05-09 22:06:08 +02:00
parent f5537e7a0e
commit fd80499954
2 changed files with 42 additions and 17 deletions

View File

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

View File

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