mirror of
https://github.com/kopia/kopia.git
synced 2026-03-12 19:26:25 -04:00
* linter: upgraded to 1.33, disabled some linters * lint: fixed 'errorlint' errors This ensures that all error comparisons use errors.Is() or errors.As(). We will be wrapping more errors going forward so it's important that error checks are not strict everywhere. Verified that there are no exceptions for errorlint linter which guarantees that. * lint: fixed or suppressed wrapcheck errors * lint: nolintlint and misc cleanups Co-authored-by: Julio López <julio+gh@kasten.io>
89 lines
1.7 KiB
Go
89 lines
1.7 KiB
Go
package cli
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"strings"
|
|
|
|
"github.com/bgentry/speakeasy"
|
|
"github.com/pkg/errors"
|
|
|
|
"github.com/kopia/kopia/repo"
|
|
)
|
|
|
|
var password = app.Flag("password", "Repository password.").Envar("KOPIA_PASSWORD").Short('p').String()
|
|
|
|
func askForNewRepositoryPassword() (string, error) {
|
|
for {
|
|
p1, err := askPass("Enter password to create new repository: ")
|
|
if err != nil {
|
|
return "", errors.Wrap(err, "password entry")
|
|
}
|
|
|
|
p2, err := askPass("Re-enter password for verification: ")
|
|
if err != nil {
|
|
return "", errors.Wrap(err, "password verification")
|
|
}
|
|
|
|
if p1 != p2 {
|
|
fmt.Println("Passwords don't match!")
|
|
} else {
|
|
return p1, nil
|
|
}
|
|
}
|
|
}
|
|
|
|
func askForExistingRepositoryPassword() (string, error) {
|
|
p1, err := askPass("Enter password to open repository: ")
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
|
|
fmt.Println()
|
|
|
|
return p1, nil
|
|
}
|
|
|
|
var passwordFromToken string
|
|
|
|
func getPasswordFromFlags(ctx context.Context, isNew, allowPersistent bool) (string, error) {
|
|
if passwordFromToken != "" {
|
|
// password provided via token
|
|
return passwordFromToken, nil
|
|
}
|
|
|
|
if !isNew && allowPersistent {
|
|
pass, ok := repo.GetPersistedPassword(ctx, repositoryConfigFileName())
|
|
if ok {
|
|
return pass, nil
|
|
}
|
|
}
|
|
|
|
switch {
|
|
case *password != "":
|
|
return strings.TrimSpace(*password), nil
|
|
case isNew:
|
|
return askForNewRepositoryPassword()
|
|
default:
|
|
return askForExistingRepositoryPassword()
|
|
}
|
|
}
|
|
|
|
// askPass presents a given prompt and asks the user for password.
|
|
func askPass(prompt string) (string, error) {
|
|
for i := 0; i < 5; i++ {
|
|
p, err := speakeasy.Ask(prompt)
|
|
if err != nil {
|
|
return "", errors.Wrap(err, "password prompt error")
|
|
}
|
|
|
|
if p == "" {
|
|
continue
|
|
}
|
|
|
|
return p, nil
|
|
}
|
|
|
|
return "", errors.New("can't get password")
|
|
}
|