diff --git a/pkg/account/accounts.go b/pkg/account/accounts.go index 4fa0fb09b7..df74897a00 100644 --- a/pkg/account/accounts.go +++ b/pkg/account/accounts.go @@ -1,6 +1,9 @@ package account -import "github.com/owncloud/ocis-accounts/pkg/config" +import ( + "github.com/owncloud/ocis-accounts/pkg/config" + "github.com/owncloud/ocis-accounts/pkg/proto/v0" +) var ( // Registry uses the strategy pattern as a registry @@ -16,11 +19,11 @@ type RegisterFunc func(*config.Config) Manager // Manager is an accounts service interface type Manager interface { // Read a record - Read(key string) *Record + Read(key string) *proto.Record // Write a record - Write(*Record) *Record + Write(*proto.Record) *proto.Record // List all records - List() []*Record + List() []*proto.Record } // Record is an entry in the account storage diff --git a/pkg/service/v0/service.go b/pkg/service/v0/service.go index 0a432c1e2b..2ae0f8a0de 100644 --- a/pkg/service/v0/service.go +++ b/pkg/service/v0/service.go @@ -2,7 +2,6 @@ package service import ( "context" - "encoding/json" "github.com/golang/protobuf/ptypes/empty" "github.com/owncloud/ocis-accounts/pkg/account" @@ -36,45 +35,18 @@ type Service struct { // Set implements the SettingsServiceHandler interface // This implementation replaces the existent data with the requested. It does not calculate diff func (s Service) Set(c context.Context, req *proto.Record, res *proto.Record) error { - settingsJSON, err := json.Marshal(req.Payload) - if err != nil { - return err - } - - s.Manager.Write(&account.Record{ - Key: req.Key, - Value: settingsJSON, - }) - + s.Manager.Write(req) return nil } // Get implements the SettingsServiceHandler interface func (s Service) Get(c context.Context, req *proto.Query, res *proto.Record) error { - contents := s.Manager.Read(req.Key) - - r := &proto.Payload{} - json.Unmarshal(contents.Value, r) - res.Payload = r - + res.Payload = s.Manager.Read(req.Key).Payload return nil } // List implements the SettingsServiceHandler interface func (s Service) List(ctx context.Context, in *empty.Empty, res *proto.Records) error { - // r := &proto.Records{} - // contents, err := registry.Store.List() - // if err != nil { - // return err - // } - - // for _, v := range contents { - // r.Records = append(r.Records, &proto.Record{ - // Key: v.Key, - // }) - // } - - // res.Records = r.Records - + // res = s.Manager.List() return nil } diff --git a/pkg/store/filesystem/store.go b/pkg/store/filesystem/store.go index a4a5a09d41..7da8846ad2 100644 --- a/pkg/store/filesystem/store.go +++ b/pkg/store/filesystem/store.go @@ -2,13 +2,16 @@ package store import ( + "encoding/json" "io/ioutil" "os" "path" "path/filepath" + // gproto "github.com/golang/protobuf/proto" "github.com/owncloud/ocis-accounts/pkg/account" "github.com/owncloud/ocis-accounts/pkg/config" + "github.com/owncloud/ocis-accounts/pkg/proto/v0" olog "github.com/owncloud/ocis-pkg/log" ) @@ -46,8 +49,8 @@ func New(cfg *config.Config) account.Manager { } // List returns all the identities in the mountPath folder -func (s Store) List() []*account.Record { - records := []*account.Record{} +func (s Store) List() []*proto.Record { + records := []*proto.Record{} identities, err := ioutil.ReadDir(s.mountPath) if err != nil { s.Logger.Err(err).Msgf("error reading %v", s.mountPath) @@ -56,7 +59,7 @@ func (s Store) List() []*account.Record { s.Logger.Info().Msg("listing identities") for _, v := range identities { - records = append(records, &account.Record{ + records = append(records, &proto.Record{ Key: v.Name(), }) } @@ -65,33 +68,42 @@ func (s Store) List() []*account.Record { } // Read implements the store interface. This implementation only reads by id. -func (s Store) Read(key string) *account.Record { +func (s Store) Read(key string) *proto.Record { 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 &account.Record{} + return &proto.Record{} } - return &account.Record{ - Key: key, - Value: contents, + record := proto.Record{} + if err = json.Unmarshal(contents, &record); err != nil { + s.Logger.Err(err).Msg("error unmarshaling record") + return &proto.Record{} } + + return &record } // Write implements the store interface -func (s Store) Write(rec *account.Record) *account.Record { +func (s Store) Write(rec *proto.Record) *proto.Record { path := filepath.Join(s.mountPath, rec.Key) if len(rec.Key) < 1 { s.Logger.Error().Msg("key cannot be empty") - return &account.Record{} + return &proto.Record{} } - if err := ioutil.WriteFile(path, rec.Value, 0644); err != nil { - return &account.Record{} + contents, err := json.Marshal(rec) + if err != nil { + s.Logger.Err(err).Msg("record could not be marshaled") + return &proto.Record{} } - s.Logger.Info().Msgf("%v bytes written to %v", len(rec.Value), path) + if err := ioutil.WriteFile(path, contents, 0644); err != nil { + return &proto.Record{} + } + + s.Logger.Info().Msgf("%v bytes written to %v", len(contents), path) return rec }