5 Commits

7 changed files with 36 additions and 23 deletions

View File

@@ -146,15 +146,16 @@ func (c *Collection) Items() (items []*Item, err error) {
paths = variant.Value().([]dbus.ObjectPath) paths = variant.Value().([]dbus.ObjectPath)
items = make([]*Item, len(paths)) items = make([]*Item, 0)
for idx, path := range paths { for _, path := range paths {
item = nil
if item, err = NewItem(c, path); err != nil { if item, err = NewItem(c, path); err != nil {
errs = append(errs, err) errs = append(errs, err)
err = nil err = nil
continue continue
} }
items[idx] = item items = append(items, item)
} }
err = NewErrors(err) err = NewErrors(err)
@@ -245,6 +246,7 @@ func (c *Collection) SearchItems(profile string) (items []*Item, err error) {
var paths []dbus.ObjectPath var paths []dbus.ObjectPath
var errs []error = make([]error, 0) var errs []error = make([]error, 0)
var attrs map[string]string = make(map[string]string, 0) var attrs map[string]string = make(map[string]string, 0)
var item *Item
attrs["profile"] = profile attrs["profile"] = profile
@@ -254,14 +256,16 @@ func (c *Collection) SearchItems(profile string) (items []*Item, err error) {
return return
} }
items = make([]*Item, len(paths)) items = make([]*Item, 0)
for idx, path := range paths { for _, path := range paths {
if items[idx], err = NewItem(c, path); err != nil { item = nil
if item, err = NewItem(c, path); err != nil {
errs = append(errs, err) errs = append(errs, err)
err = nil err = nil
continue continue
} }
items = append(items, item)
} }
err = NewErrors(err) err = NewErrors(err)

View File

@@ -148,6 +148,7 @@ func TestCollection_Label(t *testing.T) {
t.Fatalf("NewService failed: %v", err.Error()) t.Fatalf("NewService failed: %v", err.Error())
} }
t.Logf("Attempting to get label of collection: %v", defaultCollectionLabel)
if collection, err = svc.GetCollection(defaultCollectionLabel); err != nil { if collection, err = svc.GetCollection(defaultCollectionLabel); err != nil {
t.Errorf( t.Errorf(
"failed when fetching collection '%v': %v", "failed when fetching collection '%v': %v",

View File

@@ -1,7 +1,7 @@
package gosecret package gosecret
import ( import (
`github.com/godbus/dbus/v5` "github.com/godbus/dbus/v5"
) )
// Constants for use with gosecret. // Constants for use with gosecret.
@@ -30,6 +30,7 @@ const (
// Libsecret/SecretService special values. // Libsecret/SecretService special values.
var ( var (
// DbusRemoveAliasPath is used to remove an alias from a Collection and/or Item.
DbusRemoveAliasPath dbus.ObjectPath = dbus.ObjectPath("/") DbusRemoveAliasPath dbus.ObjectPath = dbus.ObjectPath("/")
) )

View File

@@ -118,7 +118,7 @@ func pathsFromPath(bus dbus.BusObject, path string) (paths []dbus.ObjectPath, er
/* /*
NameFromPath returns an actual name (as it appears in Dbus) from a dbus.ObjectPath. NameFromPath returns an actual name (as it appears in Dbus) from a dbus.ObjectPath.
Note that you can get any object's dbus.ObjectPath via <object.Dbus.Path(). Note that you can get any object's dbus.ObjectPath via <object>.Dbus.Path().
path is validated to ensure it is not an empty string. path is validated to ensure it is not an empty string.
*/ */
func NameFromPath(path dbus.ObjectPath) (name string, err error) { func NameFromPath(path dbus.ObjectPath) (name string, err error) {

View File

@@ -1,11 +1,11 @@
package gosecret package gosecret
import ( import (
`strconv` "strconv"
`strings` "strings"
`time` "time"
`github.com/godbus/dbus/v5` "github.com/godbus/dbus/v5"
) )
// NewItem returns a pointer to an Item based on Collection and a Dbus path. // NewItem returns a pointer to an Item based on Collection and a Dbus path.
@@ -158,6 +158,7 @@ func (i *Item) Label() (label string, err error) {
} }
label = variant.Value().(string) label = variant.Value().(string)
i.LabelName = label
return return
} }

View File

@@ -1,7 +1,7 @@
package gosecret package gosecret
import ( import (
`fmt` "fmt"
) )
/* /*
@@ -47,7 +47,7 @@ func (e *MultiError) Error() (errStr string) {
for idx, err := range e.Errors { for idx, err := range e.Errors {
if (idx + 1) < numErrs { if (idx + 1) < numErrs {
errStr += fmt.Sprintf(err.Error(), e.ErrorSep) errStr += fmt.Sprintf("%v%v", err.Error(), e.ErrorSep)
} else { } else {
errStr += err.Error() errStr += err.Error()
} }

View File

@@ -57,16 +57,17 @@ func (s *Service) Collections() (collections []*Collection, err error) {
paths = variant.Value().([]dbus.ObjectPath) paths = variant.Value().([]dbus.ObjectPath)
collections = make([]*Collection, len(paths)) collections = make([]*Collection, 0)
for idx, path := range paths { for _, path := range paths {
coll = nil
if coll, err = NewCollection(s, path); err != nil { if coll, err = NewCollection(s, path); err != nil {
// return // return
errs = append(errs, err) errs = append(errs, err)
err = nil err = nil
continue continue
} }
collections[idx] = coll collections = append(collections, coll)
} }
err = NewErrors(err) err = NewErrors(err)
@@ -364,6 +365,7 @@ func (s *Service) SearchItems(attributes map[string]string) (unlockedItems []*It
var ok bool var ok bool
var c *Collection var c *Collection
var cPath dbus.ObjectPath var cPath dbus.ObjectPath
var item *Item
var errs []error = make([]error, 0) var errs []error = make([]error, 0)
if attributes == nil || len(attributes) == 0 { if attributes == nil || len(attributes) == 0 {
@@ -375,8 +377,8 @@ func (s *Service) SearchItems(attributes map[string]string) (unlockedItems []*It
DbusServiceSearchItems, 0, attributes, DbusServiceSearchItems, 0, attributes,
).Store(&unlocked, &locked) ).Store(&unlocked, &locked)
lockedItems = make([]*Item, len(locked)) lockedItems = make([]*Item, 0)
unlockedItems = make([]*Item, len(unlocked)) unlockedItems = make([]*Item, 0)
if collectionObjs, err = s.Collections(); err != nil { if collectionObjs, err = s.Collections(); err != nil {
return return
@@ -389,8 +391,9 @@ func (s *Service) SearchItems(attributes map[string]string) (unlockedItems []*It
} }
// Locked items // Locked items
for idx, i := range locked { for _, i := range locked {
item = nil
cPath = dbus.ObjectPath(filepath.Dir(string(i))) cPath = dbus.ObjectPath(filepath.Dir(string(i)))
if c, ok = collections[cPath]; !ok { if c, ok = collections[cPath]; !ok {
@@ -400,18 +403,20 @@ func (s *Service) SearchItems(attributes map[string]string) (unlockedItems []*It
continue continue
} }
if lockedItems[idx], err = NewItem(c, i); err != nil { if item, err = NewItem(c, i); err != nil {
errs = append(errs, errors.New(fmt.Sprintf( errs = append(errs, errors.New(fmt.Sprintf(
"could not create Item for locked item %v", string(i), "could not create Item for locked item %v", string(i),
))) )))
err = nil err = nil
continue continue
} }
lockedItems = append(lockedItems, item)
} }
// Unlocked items // Unlocked items
for idx, i := range unlocked { for _, i := range unlocked {
item = nil
cPath = dbus.ObjectPath(filepath.Dir(string(i))) cPath = dbus.ObjectPath(filepath.Dir(string(i)))
if c, ok = collections[cPath]; !ok { if c, ok = collections[cPath]; !ok {
@@ -421,13 +426,14 @@ func (s *Service) SearchItems(attributes map[string]string) (unlockedItems []*It
continue continue
} }
if unlockedItems[idx], err = NewItem(c, i); err != nil { if item, err = NewItem(c, i); err != nil {
errs = append(errs, errors.New(fmt.Sprintf( errs = append(errs, errors.New(fmt.Sprintf(
"could not create Item for unlocked item %v", string(i), "could not create Item for unlocked item %v", string(i),
))) )))
err = nil err = nil
continue continue
} }
unlockedItems = append(unlockedItems, item)
} }
if errs != nil && len(errs) > 0 { if errs != nil && len(errs) > 0 {