Add flags to set verbosity

This commit is contained in:
Alexander Neumann 2018-04-21 22:07:14 +02:00
parent a5c0cf2324
commit 9fac2ca832
2 changed files with 28 additions and 4 deletions

View File

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

View File

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