2
2
mirror of https://github.com/octoleo/restic.git synced 2024-11-27 07:16:40 +00:00

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 { func newScanProgress() *restic.Progress {
if !terminal.IsTerminal(int(os.Stdout.Fd())) { if disableProgress() {
return nil return nil
} }
@ -113,7 +113,7 @@ func newScanProgress() *restic.Progress {
} }
func newArchiveProgress(todo restic.Stat) *restic.Progress { func newArchiveProgress(todo restic.Stat) *restic.Progress {
if !terminal.IsTerminal(int(os.Stdout.Fd())) { if disableProgress() {
return nil return nil
} }
@ -194,7 +194,7 @@ func (cmd CmdBackup) Execute(args []string) error {
return fmt.Errorf("invalid id %q: %v", cmd.Parent, err) 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 // 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 { 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()) stat, err := restic.Scan(target, newScanProgress())
@ -252,12 +252,7 @@ func (cmd CmdBackup) Execute(args []string) error {
return err return err
} }
plen, err := s.PrefixLength(backend.Snapshot) verbosePrintf("snapshot %s saved\n", id.Str())
if err != nil {
return err
}
fmt.Printf("snapshot %s saved\n", id[:plen/2])
return nil return nil
} }

View File

@ -20,8 +20,9 @@ import (
var version = "compiled manually" var version = "compiled manually"
var opts struct { var opts struct {
Repo string `short:"r" long:"repo" description:"Repository directory to backup to/restore from"` 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"` 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 password string
} }
@ -47,6 +48,34 @@ func readPassword(prompt string) string {
return string(pw) 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{} type CmdInit struct{}
func (cmd CmdInit) Execute(args []string) error { func (cmd CmdInit) Execute(args []string) error {
@ -78,10 +107,11 @@ func (cmd CmdInit) Execute(args []string) error {
os.Exit(1) os.Exit(1)
} }
fmt.Printf("created restic backend %v at %s\n", s.Config.ID[:10], opts.Repo) verbosePrintf("created restic backend %v at %s\n", s.Config.ID[:10], opts.Repo)
verbosePrintf("\n")
fmt.Println("Please note that knowledge of your password is required to access the repository.") verbosePrintf("Please note that knowledge of your password is required to access\n")
fmt.Println("Losing your password means that your data is irrecoverably lost.") verbosePrintf("the repository. Losing your password means that your data is\n")
verbosePrintf("irrecoverably lost.\n")
return nil return nil
} }