mirror of https://github.com/restic/restic.git
move globalOptions initialization into method
This commit is contained in:
parent
d378a171c8
commit
120bd08c0d
|
@ -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{
|
var globalOptions = GlobalOptions{
|
||||||
stdout: os.Stdout,
|
stdout: os.Stdout,
|
||||||
stderr: os.Stderr,
|
stderr: os.Stderr,
|
||||||
|
@ -255,7 +288,7 @@ func Warnf(format string, args ...interface{}) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// resolvePassword determines the password to be used for opening the repository.
|
// 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 != "" {
|
if opts.PasswordFile != "" && opts.PasswordCommand != "" {
|
||||||
return "", errors.Fatalf("Password file and command are mutually exclusive options")
|
return "", errors.Fatalf("Password file and command are mutually exclusive options")
|
||||||
}
|
}
|
||||||
|
|
|
@ -17,7 +17,6 @@ import (
|
||||||
"github.com/restic/restic/internal/debug"
|
"github.com/restic/restic/internal/debug"
|
||||||
"github.com/restic/restic/internal/errors"
|
"github.com/restic/restic/internal/errors"
|
||||||
"github.com/restic/restic/internal/feature"
|
"github.com/restic/restic/internal/feature"
|
||||||
"github.com/restic/restic/internal/options"
|
|
||||||
"github.com/restic/restic/internal/repository"
|
"github.com/restic/restic/internal/repository"
|
||||||
"github.com/restic/restic/internal/restic"
|
"github.com/restic/restic/internal/restic"
|
||||||
)
|
)
|
||||||
|
@ -47,38 +46,7 @@ The full documentation can be found at https://restic.readthedocs.io/ .
|
||||||
DisableAutoGenTag: true,
|
DisableAutoGenTag: true,
|
||||||
|
|
||||||
PersistentPreRunE: func(c *cobra.Command, _ []string) error {
|
PersistentPreRunE: func(c *cobra.Command, _ []string) error {
|
||||||
// set verbosity, default is one
|
return globalOptions.PreRun(needsPassword(c.Name()))
|
||||||
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
|
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -110,7 +110,7 @@ func fillSecondaryGlobalOpts(ctx context.Context, opts secondaryRepoOptions, gop
|
||||||
if opts.password != "" {
|
if opts.password != "" {
|
||||||
dstGopts.password = opts.password
|
dstGopts.password = opts.password
|
||||||
} else {
|
} else {
|
||||||
dstGopts.password, err = resolvePassword(dstGopts, pwdEnv)
|
dstGopts.password, err = resolvePassword(&dstGopts, pwdEnv)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return GlobalOptions{}, false, err
|
return GlobalOptions{}, false, err
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue