-
Notifications
You must be signed in to change notification settings - Fork 4
/
groups.go
78 lines (59 loc) · 1.48 KB
/
groups.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
package main
import (
"fmt"
"strconv"
"strings"
"github.com/tobischo/gokeepasslib/v3"
)
func listGroups(g *gokeepasslib.Group) []string {
var groups = make([]string, 0)
groups = append(groups, g.Name)
for _, group := range g.Groups {
subGroups := listGroups(&group) //#nosec G601
for i, val := range subGroups {
subGroups[i] = fmt.Sprintf("%s/%s", g.Name, val)
}
groups = append(groups, subGroups...)
}
return groups
}
func readGroup(selectors []string, g *gokeepasslib.Group) (*gokeepasslib.Group, error) {
if len(selectors) == 1 {
if g.Name == selectors[0] {
return g, nil
}
} else {
for i, group := range g.Groups {
if group.Name == selectors[1] {
return readGroup(selectors[1:], &g.Groups[i])
}
}
}
groups := searchGroups(selectors, g)
if len(groups) < 1 {
return nil, errNoGroupFound
}
for i, group := range groups {
fmt.Printf("%3d %s\n", i, group)
}
selection, err := readString("Selection: ")
if err != nil {
return nil, err
}
index, err := strconv.Atoi(selection)
if err != nil {
return nil, err
}
return readGroup(strings.Split(groups[index], "/"), g)
}
func searchGroups(selectors []string, g *gokeepasslib.Group) []string {
groups := listGroups(g)
selector := strings.ToLower(strings.Join(selectors, "/"))
var selectedGroups = make([]string, 0)
for _, group := range groups {
if strings.Contains(strings.ToLower(group), selector) {
selectedGroups = append(selectedGroups, group)
}
}
return selectedGroups
}