From 120bd08c0d3c6431ab20d5962917703ca6492a4c Mon Sep 17 00:00:00 2001 From: Michael Eischer Date: Fri, 7 Feb 2025 19:14:55 +0100 Subject: [PATCH] move globalOptions initialization into method --- cmd/restic/global.go | 35 ++++++++++++++++++++++++++++++++++- cmd/restic/main.go | 34 +--------------------------------- cmd/restic/secondary_repo.go | 2 +- 3 files changed, 36 insertions(+), 35 deletions(-) diff --git a/cmd/restic/global.go b/cmd/restic/global.go index 0be456d29..066d0acef 100644 --- a/cmd/restic/global.go +++ b/cmd/restic/global.go @@ -147,6 +147,39 @@ func (opts *GlobalOptions) AddFlags(f *pflag.FlagSet) { } } +func (opts *GlobalOptions) PreRun(needsPassword bool) error { + // set verbosity, default is one + opts.verbosity = 1 + if opts.Quiet && opts.Verbose > 0 { + return errors.Fatal("--quiet and --verbose cannot be specified at the same time") + } + + switch { + case opts.Verbose >= 2: + opts.verbosity = 3 + case opts.Verbose > 0: + opts.verbosity = 2 + case opts.Quiet: + opts.verbosity = 0 + } + + // parse extended options + extendedOpts, err := options.Parse(opts.Options) + if err != nil { + return err + } + opts.extended = extendedOpts + if !needsPassword { + return nil + } + pwd, err := resolvePassword(opts, "RESTIC_PASSWORD") + if err != nil { + return errors.Fatal(fmt.Sprintf("Resolving password failed: %v\n", err)) + } + opts.password = pwd + return nil +} + var globalOptions = GlobalOptions{ stdout: os.Stdout, stderr: os.Stderr, @@ -255,7 +288,7 @@ func Warnf(format string, args ...interface{}) { } // resolvePassword determines the password to be used for opening the repository. -func resolvePassword(opts GlobalOptions, envStr string) (string, error) { +func resolvePassword(opts *GlobalOptions, envStr string) (string, error) { if opts.PasswordFile != "" && opts.PasswordCommand != "" { return "", errors.Fatalf("Password file and command are mutually exclusive options") } diff --git a/cmd/restic/main.go b/cmd/restic/main.go index 72a726b0e..80da200e3 100644 --- a/cmd/restic/main.go +++ b/cmd/restic/main.go @@ -17,7 +17,6 @@ import ( "github.com/restic/restic/internal/debug" "github.com/restic/restic/internal/errors" "github.com/restic/restic/internal/feature" - "github.com/restic/restic/internal/options" "github.com/restic/restic/internal/repository" "github.com/restic/restic/internal/restic" ) @@ -47,38 +46,7 @@ The full documentation can be found at https://restic.readthedocs.io/ . DisableAutoGenTag: true, PersistentPreRunE: func(c *cobra.Command, _ []string) error { - // set verbosity, default is one - globalOptions.verbosity = 1 - if globalOptions.Quiet && globalOptions.Verbose > 0 { - return errors.Fatal("--quiet and --verbose cannot be specified at the same time") - } - - switch { - case globalOptions.Verbose >= 2: - globalOptions.verbosity = 3 - case globalOptions.Verbose > 0: - globalOptions.verbosity = 2 - case globalOptions.Quiet: - globalOptions.verbosity = 0 - } - - // parse extended options - opts, err := options.Parse(globalOptions.Options) - if err != nil { - return err - } - globalOptions.extended = opts - if !needsPassword(c.Name()) { - return nil - } - pwd, err := resolvePassword(globalOptions, "RESTIC_PASSWORD") - if err != nil { - fmt.Fprintf(os.Stderr, "Resolving password failed: %v\n", err) - Exit(1) - } - globalOptions.password = pwd - - return nil + return globalOptions.PreRun(needsPassword(c.Name())) }, } diff --git a/cmd/restic/secondary_repo.go b/cmd/restic/secondary_repo.go index 44621afa1..66fd4b03b 100644 --- a/cmd/restic/secondary_repo.go +++ b/cmd/restic/secondary_repo.go @@ -110,7 +110,7 @@ func fillSecondaryGlobalOpts(ctx context.Context, opts secondaryRepoOptions, gop if opts.password != "" { dstGopts.password = opts.password } else { - dstGopts.password, err = resolvePassword(dstGopts, pwdEnv) + dstGopts.password, err = resolvePassword(&dstGopts, pwdEnv) if err != nil { return GlobalOptions{}, false, err }