-
Notifications
You must be signed in to change notification settings - Fork 6
/
api.go
106 lines (90 loc) · 2.55 KB
/
api.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
101
102
103
104
105
106
package mobileapi
import (
"sort"
"github.com/renatoathaydes/go-hash/gohash_db"
)
// GroupIterator allows iterating over all groups in the database.
type GroupIterator struct {
state gohash_db.State
keys []string
currentIndex uint
}
// EntryIterator allows iterating over all LoginInfo entries in a group.
type EntryIterator struct {
Group string
entries []gohash_db.LoginInfo
currentIndex uint
}
// Entry is a wrapper around a LoginInfo instance.
// LoginInfo cannot be exposed directly due to the gomobile tool's limitations.
type Entry struct {
loginInfo gohash_db.LoginInfo
}
// Database a go-hash database.
type Database struct {
FileName string
state gohash_db.State
}
// Next returns the next EntryIterator, if any, or nil if none is available.
func (iter *GroupIterator) Next() *EntryIterator {
if int(iter.currentIndex) < len(iter.keys) {
group := iter.keys[iter.currentIndex]
iter.currentIndex++
info := iter.state[group]
return &EntryIterator{entries: info, Group: group}
}
return nil
}
// Next returns the next Entry, if any, or nil if none is available.
func (iter *EntryIterator) Next() *Entry {
if int(iter.currentIndex) < len(iter.entries) {
info := iter.entries[iter.currentIndex]
iter.currentIndex++
return &Entry{loginInfo: info}
}
return nil
}
// Name of this Entry.
func (entry *Entry) Name() string {
return entry.loginInfo.Name
}
// Username of this Entry.
func (entry *Entry) Username() string {
return entry.loginInfo.Username
}
// Description of this Entry.
func (entry *Entry) Description() string {
return entry.loginInfo.Description
}
// Password of this Entry.
func (entry *Entry) Password() string {
return entry.loginInfo.Password
}
// Url of this Entry.
func (entry *Entry) Url() string {
return entry.loginInfo.URL
}
// UpdatedAt the instance at which this entry was last updated.
// As epoch-milliseconds since 1970-01-01T00:00:00.
func (entry *Entry) UpdatedAt() int64 {
return entry.loginInfo.UpdatedAt.Unix()
}
// Iter returns a GroupIterator which can be used to iterate over the groups in this database.
func (db *Database) Iter() *GroupIterator {
keys := make([]string, len(db.state))
i := 0
for key := range db.state {
keys[i] = key
i++
}
sort.Strings(keys)
return &GroupIterator{keys: keys, state: db.state}
}
// ReadDatabase reads a go-hash database.
func ReadDatabase(filePath, password string) (*Database, error) {
state, err := gohash_db.ReadDatabase(filePath, password)
if err != nil {
return nil, err
}
return &Database{FileName: filePath, state: state}, nil
}