-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdatabase.go
69 lines (59 loc) · 1.73 KB
/
database.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
package main
import (
"regexp"
"strings"
"github.com/tobischo/gokeepasslib/v3"
w "github.com/tobischo/gokeepasslib/v3/wrappers"
)
func mkValue(key string, value string) gokeepasslib.ValueData {
return gokeepasslib.ValueData{Key: key, Value: gokeepasslib.V{Content: value}}
}
func mkProtectedValue(key string, value string) gokeepasslib.ValueData {
return gokeepasslib.ValueData{
Key: key,
Value: gokeepasslib.V{Content: value, Protected: w.NewBoolWrapper(true)},
}
}
func newDatabase() *gokeepasslib.Database {
rootGroup := gokeepasslib.NewGroup()
rootGroup.Name = "Database"
emailGroup := gokeepasslib.NewGroup()
emailGroup.Name = "Email"
internetGroup := gokeepasslib.NewGroup()
internetGroup.Name = "Internet"
rootGroup.Groups = append(rootGroup.Groups, emailGroup, internetGroup)
db := gokeepasslib.NewDatabase()
db.Content.Root = &gokeepasslib.RootData{
Groups: []gokeepasslib.Group{rootGroup},
}
return db
}
func getEntryContent(entry gokeepasslib.Entry, key string) string {
for i := range entry.Values {
if strings.EqualFold(entry.Values[i].Key, key) {
return entry.Values[i].Value.Content
}
}
return ""
}
func findEntry(root workingGroup, search string) []string {
var results []string
group := root.Group()
regex := regexp.MustCompile("(?i)" + search)
for _, entry := range group.Entries {
for _, value := range entry.Values {
if regex.MatchString(value.Value.Content) {
results = append(results, strings.Join([]string{root.String(), entry.GetTitle()}, "/"))
break
}
}
}
for _, subGroup := range group.Groups {
subWorkingGroup, err := travel(root, subGroup.Name)
if err != nil {
panic(err)
}
results = append(results, findEntry(subWorkingGroup, search)...)
}
return results
}