From 9fac2ca832459801a730ad3cfcf24af2c23db22d Mon Sep 17 00:00:00 2001 From: Alexander Neumann Date: Sat, 21 Apr 2018 22:07:14 +0200 Subject: [PATCH] Add flags to set verbosity --- cmd/restic/global.go | 17 +++++++++++++---- cmd/restic/main.go | 15 +++++++++++++++ 2 files changed, 28 insertions(+), 4 deletions(-) diff --git a/cmd/restic/global.go b/cmd/restic/global.go index 0c3d805b2..627e4c8f9 100644 --- a/cmd/restic/global.go +++ b/cmd/restic/global.go @@ -43,6 +43,8 @@ type GlobalOptions struct { Repo string PasswordFile string Quiet bool + Verbose bool + Debug bool NoLock bool JSON bool CacheDir string @@ -59,6 +61,13 @@ type GlobalOptions struct { stdout io.Writer stderr io.Writer + // verbosity is set as follows: + // 0 means: don't print any messages except errors, this is used when --quiet is specified + // 1 is the default: print essential messages + // 2 means: print more messages, report minor things, this is used when --verbose is specified + // 3 means: print very detailed debug messages, this is used when --debug is specified + verbosity uint + Options []string extended options.Options @@ -81,6 +90,8 @@ func init() { f.StringVarP(&globalOptions.Repo, "repo", "r", os.Getenv("RESTIC_REPOSITORY"), "repository to backup to or restore from (default: $RESTIC_REPOSITORY)") f.StringVarP(&globalOptions.PasswordFile, "password-file", "p", os.Getenv("RESTIC_PASSWORD_FILE"), "read the repository password from a file (default: $RESTIC_PASSWORD_FILE)") f.BoolVarP(&globalOptions.Quiet, "quiet", "q", false, "do not output comprehensive progress report") + f.BoolVarP(&globalOptions.Verbose, "verbose", "v", false, "be verbose") + f.BoolVar(&globalOptions.Debug, "debug", false, "be very verbose") f.BoolVar(&globalOptions.NoLock, "no-lock", false, "do not lock the repo, this allows some operations on read-only repos") f.BoolVarP(&globalOptions.JSON, "json", "", false, "set output mode to JSON for commands that support it") f.StringVar(&globalOptions.CacheDir, "cache-dir", "", "set the cache directory") @@ -173,11 +184,9 @@ func Printf(format string, args ...interface{}) { // Verbosef calls Printf to write the message when the verbose flag is set. func Verbosef(format string, args ...interface{}) { - if globalOptions.Quiet { - return + if globalOptions.verbosity >= 1 { + Printf(format, args...) } - - Printf(format, args...) } // PrintProgress wraps fmt.Printf to handle the difference in writing progress diff --git a/cmd/restic/main.go b/cmd/restic/main.go index ca1067cda..c1a42bd90 100644 --- a/cmd/restic/main.go +++ b/cmd/restic/main.go @@ -30,6 +30,21 @@ directories in an encrypted repository stored on different backends. DisableAutoGenTag: true, PersistentPreRunE: func(c *cobra.Command, args []string) error { + // set verbosity + globalOptions.verbosity = 1 + if globalOptions.Quiet && (globalOptions.Verbose || globalOptions.Debug) { + return errors.Fatal("--quiet and --verbose or --debug cannot be specified at the same time") + } + + switch { + case globalOptions.Quiet: + globalOptions.verbosity = 0 + case globalOptions.Verbose: + globalOptions.verbosity = 2 + case globalOptions.Debug: + globalOptions.verbosity = 3 + } + // parse extended options opts, err := options.Parse(globalOptions.Options) if err != nil {