-
Notifications
You must be signed in to change notification settings - Fork 4
/
helpers.go
71 lines (58 loc) · 1.31 KB
/
helpers.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
package main
import (
"fmt"
"syscall"
"github.com/tobischo/gokeepasslib/v3"
"golang.org/x/term"
)
func readString(text string) (string, error) {
fmt.Print(text)
var input string
_, err := fmt.Scanln(&input)
return input, err
}
func readPassword(text string) (string, error) {
fmt.Print(text)
pw, err := term.ReadPassword(syscall.Stdin)
if err != nil {
return "", fmt.Errorf("Failed to read password: '%w'", err)
}
fmt.Println()
return string(pw), nil
}
func readPasswordWithConfirmation() (string, error) {
var (
password string
passwordRepeated string
err error
)
password, err = readPassword("Enter password: ")
if err != nil {
return "", err
}
passwordRepeated, err = readPassword("Repeat password: ")
if err != nil {
return "", err
}
if password != passwordRepeated {
return "", errPasswordMismatch
}
return password, nil
}
func pickCredentialMode(password string) (*gokeepasslib.DBCredentials, error) {
switch {
case usePassword && keyFile != "":
return gokeepasslib.NewPasswordAndKeyCredentials(
password, keyFile,
)
case usePassword:
credentials := gokeepasslib.NewPasswordCredentials(
password,
)
return credentials, nil
case keyFile != "":
return gokeepasslib.NewKeyCredentials(keyFile)
default:
return nil, errCredentialsMissing
}
}