diff --git a/pkg/store/filesystem/store.go b/pkg/store/filesystem/store.go index 7da8846ad2..40428e1705 100644 --- a/pkg/store/filesystem/store.go +++ b/pkg/store/filesystem/store.go @@ -3,6 +3,7 @@ package store import ( "encoding/json" + "fmt" "io/ioutil" "os" "path" @@ -17,8 +18,9 @@ import ( var ( // StoreName is the default name for the accounts store - StoreName string = "ocis-store" - managerName = "filesystem" + StoreName string = "ocis-store" + managerName = "filesystem" + emptyKeyError = "key cannot be empty" ) // StoreName is the default name for the store container @@ -49,12 +51,12 @@ func New(cfg *config.Config) account.Manager { } // List returns all the identities in the mountPath folder -func (s Store) List() []*proto.Record { +func (s Store) List() ([]*proto.Record, error) { records := []*proto.Record{} identities, err := ioutil.ReadDir(s.mountPath) if err != nil { s.Logger.Err(err).Msgf("error reading %v", s.mountPath) - return records + return nil, err } s.Logger.Info().Msg("listing identities") @@ -64,47 +66,47 @@ func (s Store) List() []*proto.Record { }) } - return records + return records, nil } // Read implements the store interface. This implementation only reads by id. -func (s Store) Read(key string) *proto.Record { +func (s Store) Read(key string) (*proto.Record, error) { contents, err := ioutil.ReadFile(path.Join(s.mountPath, key)) if err != nil { s.Logger.Err(err).Msgf("error reading contents of key %v: file not found", key) - return &proto.Record{} + return nil, err } record := proto.Record{} if err = json.Unmarshal(contents, &record); err != nil { s.Logger.Err(err).Msg("error unmarshaling record") - return &proto.Record{} + return nil, err } - return &record + return &record, nil } // Write implements the store interface -func (s Store) Write(rec *proto.Record) *proto.Record { +func (s Store) Write(rec *proto.Record) (*proto.Record, error) { path := filepath.Join(s.mountPath, rec.Key) if len(rec.Key) < 1 { s.Logger.Error().Msg("key cannot be empty") - return &proto.Record{} + return nil, fmt.Errorf(emptyKeyError) } contents, err := json.Marshal(rec) if err != nil { s.Logger.Err(err).Msg("record could not be marshaled") - return &proto.Record{} + return nil, err } if err := ioutil.WriteFile(path, contents, 0644); err != nil { - return &proto.Record{} + return nil, err } s.Logger.Info().Msgf("%v bytes written to %v", len(contents), path) - return rec + return rec, nil } func init() {