-
Notifications
You must be signed in to change notification settings - Fork 4
/
entries.go
100 lines (78 loc) · 2 KB
/
entries.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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
package main
import (
"fmt"
"strconv"
"strings"
"github.com/tobischo/gokeepasslib/v3"
"github.com/tobischo/gokeepasslib/v3/wrappers"
)
func markAsAccessed(entry *gokeepasslib.Entry) {
access := wrappers.Now()
entry.Times.LastAccessTime = &access
changed = true
}
func listEntries(g *gokeepasslib.Group) []string {
var entries = make([]string, 0)
for _, entry := range g.Entries {
entries = append(entries, entry.GetTitle())
}
for _, group := range g.Groups {
subEntries := listEntries(&group) //#nosec G601
for i, val := range subEntries {
subEntries[i] = fmt.Sprintf("%s/%s", group.Name, val)
}
entries = append(entries, subEntries...)
}
return entries
}
func readEntry(selection string, g *gokeepasslib.Group) (*gokeepasslib.Entry, error) {
for i, entry := range g.Entries {
if entry.GetTitle() == selection {
return &g.Entries[i], nil
}
}
selectors := strings.Split(selection, "/")
if len(selectors) == 1 {
for i, entry := range g.Entries {
if entry.GetTitle() == selectors[0] {
return &g.Entries[i], nil
}
}
} else {
for i, group := range g.Groups {
if group.Name == selectors[0] {
return readEntry(strings.Join(selectors[1:], "/"), &g.Groups[i])
}
}
}
entries := searchEntries(selectors, g)
if len(entries) < 1 {
return nil, errNoEntryFound
}
if len(entries) == 1 {
return readEntry(entries[0], g)
}
for i, entry := range entries {
fmt.Printf("%3d %s\n", i, entry)
}
selection, err := readString("Selection: ")
if err != nil {
return nil, err
}
index, err := strconv.Atoi(selection)
if err != nil {
return nil, err
}
return readEntry(entries[index], g)
}
func searchEntries(selectors []string, g *gokeepasslib.Group) []string {
entries := listEntries(g)
selector := strings.ToLower(strings.Join(selectors, "/"))
var selectedEntries = make([]string, 0)
for _, entry := range entries {
if strings.Contains(strings.ToLower(entry), selector) {
selectedEntries = append(selectedEntries, entry)
}
}
return selectedEntries
}