mirror of
https://github.com/restic/restic.git
synced 2025-12-15 18:22:28 +00:00
Merge pull request #2307 from gary-kim/fix-2306-password-retries
Allow multiple retries for interactive password input
This commit is contained in:
@@ -320,7 +320,7 @@ func ReadPassword(opts GlobalOptions, prompt string) (string, error) {
|
||||
}
|
||||
|
||||
if len(password) == 0 {
|
||||
return "", errors.Fatal("an empty password is not a password")
|
||||
return "", errors.New("an empty password is not a password")
|
||||
}
|
||||
|
||||
return password, nil
|
||||
@@ -366,14 +366,32 @@ func OpenRepository(opts GlobalOptions) (*repository.Repository, error) {
|
||||
|
||||
s := repository.New(be)
|
||||
|
||||
opts.password, err = ReadPassword(opts, "enter password for repository: ")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
passwordTriesLeft := 1
|
||||
if stdinIsTerminal() && opts.password == "" {
|
||||
passwordTriesLeft = 3
|
||||
}
|
||||
|
||||
err = s.SearchKey(opts.ctx, opts.password, maxKeys, opts.KeyHint)
|
||||
for ; passwordTriesLeft > 0; passwordTriesLeft-- {
|
||||
opts.password, err = ReadPassword(opts, "enter password for repository: ")
|
||||
if err != nil && passwordTriesLeft > 1 {
|
||||
opts.password = ""
|
||||
fmt.Printf("%s. Try again\n", err)
|
||||
}
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
|
||||
err = s.SearchKey(opts.ctx, opts.password, maxKeys, opts.KeyHint)
|
||||
if err != nil && passwordTriesLeft > 1 {
|
||||
opts.password = ""
|
||||
fmt.Printf("%s. Try again\n", err)
|
||||
}
|
||||
}
|
||||
if err != nil {
|
||||
return nil, err
|
||||
if errors.IsFatal(err) {
|
||||
return nil, err
|
||||
}
|
||||
return nil, errors.Fatalf("%s", err)
|
||||
}
|
||||
|
||||
if stdoutIsTerminal() && !opts.JSON {
|
||||
|
||||
Reference in New Issue
Block a user