Skip to content

Commit

Permalink
Switch to a map-based storage for passwords
Browse files Browse the repository at this point in the history
  • Loading branch information
lcmaqueda committed Nov 14, 2016
1 parent bbfd4da commit 3e76375
Showing 1 changed file with 20 additions and 21 deletions.
41 changes: 20 additions & 21 deletions pass.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,15 @@ import (
"path/filepath"
"strings"

"sort"

"github.com/proglottis/gpgme"
"github.com/rjeczalik/notify"
)

// PasswordStore keeps track of all the passwords
type PasswordStore struct {
passwords []Password
passwords map[string]string
Prefix string
subscribers []Subscriber
}
Expand Down Expand Up @@ -73,19 +75,28 @@ func NewPasswordStore() *PasswordStore {
log.Fatal(err)
}
ps.Prefix = path
ps.passwords = make(map[string]string)
ps.indexAll()
ps.watch()
return ps
}

type byName []Password

func (a byName) Len() int { return len(a) }
func (a byName) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
func (a byName) Less(i, j int) bool { return a[i].Name < a[j].Name }

// Query the PasswordStore
func (ps *PasswordStore) Query(q string) []Password {
var hits []Password
for _, p := range ps.passwords {
if match(q, p.Name) {
hits = append(hits, p)
for pwPath, pwName := range ps.passwords {
if match(q, pwName) {
hits = append(hits, Password{Name: pwName, Path: pwPath})
}
}

sort.Sort(byName(hits))
return hits
}

Expand Down Expand Up @@ -131,8 +142,7 @@ func generateName(path, prefix string) string {

func (ps *PasswordStore) indexFile(path string, info os.FileInfo, err error) error {
if strings.HasSuffix(path, ".gpg") {
name := generateName(path, ps.Prefix)
ps.add(Password{Name: name, Path: path})
ps.add(path)
}
return nil
}
Expand All @@ -142,7 +152,6 @@ func (ps *PasswordStore) index(path string) {
}

func (ps *PasswordStore) indexAll() {
ps.clearAll()
ps.index(ps.Prefix)
}

Expand All @@ -168,30 +177,20 @@ func (ps *PasswordStore) updateIndex(eventInfo notify.EventInfo) {
ps.remove(eventInfo.Path())
ps.publishUpdate("Entry removed")
case notify.Rename:
// EventInfo contains old path, but we don't know the new one. Update all
ps.remove(eventInfo.Path())
ps.indexAll()
ps.publishUpdate("Index updated")
case notify.Write:
// Path and Name haven ot changed, ignore.
}
}

func (ps *PasswordStore) clearAll() {
ps.passwords = nil
}

func (ps *PasswordStore) add(p Password) {
ps.passwords = append(ps.passwords, p)
func (ps *PasswordStore) add(path string) {
ps.passwords[path] = generateName(path, ps.Prefix)
}

func (ps *PasswordStore) remove(path string) {
for i, p := range ps.passwords {
if p.Path == path {
ps.passwords[i] = ps.passwords[len(ps.passwords)-1]
ps.passwords = ps.passwords[:len(ps.passwords)-1]
return
}
}
delete(ps.passwords, path)
}

func findPasswordStore() (string, error) {
Expand Down

0 comments on commit 3e76375

Please sign in to comment.