-
Notifications
You must be signed in to change notification settings - Fork 4
/
add.go
85 lines (70 loc) · 1.76 KB
/
add.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
package main
import (
"strings"
"github.com/tobischo/gokeepasslib/v3"
"github.com/tobischo/gokeepasslib/v3/wrappers"
"github.com/spf13/cobra"
)
func addCmd(_ *cobra.Command, args []string) error {
selectors := strings.Split(strings.Join(args, " "), "/")
group, err := readGroup(selectors, &db.Content.Root.Groups[0])
if err != nil {
return err
}
if groupFlag {
groupName, err := readString("Group Name: ")
if err != nil {
return err
}
for _, g := range group.Groups {
if g.Name == groupName {
return errGroupNameNotUnique
}
}
newGroup := gokeepasslib.NewGroup()
newGroup.Name = groupName
group.Groups = append(group.Groups, newGroup)
changed = true
} else {
entryTitle, err := readString("Entry Title: ")
if err != nil {
return err
}
entryUserName, err := readString("Entry UserName: ")
if err != nil {
return err
}
entryURL, err := readString("Entry URL: ")
if err != nil {
return err
}
for _, e := range group.Entries {
if e.GetTitle() == entryTitle {
return errEntryTitleNotUnique
}
}
var password string
password, err = readPasswordWithConfirmation()
if err != nil {
return err
}
values := []gokeepasslib.ValueData{
{Key: "Notes", Value: gokeepasslib.V{Content: "Notes"}},
{
Key: "Password",
Value: gokeepasslib.V{
Content: password,
Protected: wrappers.NewBoolWrapper(true),
},
},
{Key: "Title", Value: gokeepasslib.V{Content: entryTitle}},
{Key: "URL", Value: gokeepasslib.V{Content: entryURL}},
{Key: "UserName", Value: gokeepasslib.V{Content: entryUserName}},
}
newEntry := gokeepasslib.NewEntry()
newEntry.Values = append(newEntry.Values, values...)
group.Entries = append(group.Entries, newEntry)
changed = true
}
return nil
}