-
Notifications
You must be signed in to change notification settings - Fork 4
/
prerun.go
58 lines (45 loc) · 1019 Bytes
/
prerun.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
package main
import (
"fmt"
"os"
"github.com/tobischo/gokeepasslib/v3"
"github.com/spf13/cobra"
)
func paramSetupCmd(_ *cobra.Command, _ []string) {
if filePath == "" {
filePath = os.Getenv("KP2FILE")
}
if keyFile == "" {
usePassword = true
}
}
func loadDatabaseCmd(_ *cobra.Command, _ []string) error {
var (
password string
err error
)
if usePassword {
password, err = readPassword("Enter password: ")
if err != nil {
return err
}
}
credentials, err := pickCredentialMode(password)
if err != nil {
return fmt.Errorf("Failed to setup credentials: '%w'", err)
}
db = gokeepasslib.NewDatabase()
db.Credentials = credentials
file, err := os.Open(filePath)
if err != nil {
return fmt.Errorf("Failed to open Keepass2 file %s: '%w'", filePath, err)
}
err = gokeepasslib.NewDecoder(file).Decode(db)
if err != nil {
return fmt.Errorf("Failed to decode Keepass2 file: %w", err)
}
if err := db.UnlockProtectedEntries(); err != nil {
return err
}
return nil
}