From 4b595b4b071ce28e240a425d046fd587b53e6cb7 Mon Sep 17 00:00:00 2001 From: "A.Unger" Date: Mon, 3 Feb 2020 19:10:17 +0100 Subject: [PATCH 01/14] config barebones --- pkg/config/config.go | 12 ++++++++++++ 1 file changed, 12 insertions(+) create mode 100644 pkg/config/config.go diff --git a/pkg/config/config.go b/pkg/config/config.go new file mode 100644 index 000000000..bf7229720 --- /dev/null +++ b/pkg/config/config.go @@ -0,0 +1,12 @@ +// Package config should be moved to internal +package config + +// Config captures ocis-accounts configuration parameters +type Config struct { + MountPath string +} + +// New returns a new config +func New() *Config { + return &Config{} +} From eef2de244fb7a5e95543a821a0e88fa37c9e4934 Mon Sep 17 00:00:00 2001 From: "A.Unger" Date: Mon, 3 Feb 2020 19:47:05 +0100 Subject: [PATCH 02/14] first draft of an accounts interface --- pkg/accounts/accounts.go | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 pkg/accounts/accounts.go diff --git a/pkg/accounts/accounts.go b/pkg/accounts/accounts.go new file mode 100644 index 000000000..f49b787fa --- /dev/null +++ b/pkg/accounts/accounts.go @@ -0,0 +1,17 @@ +package accounts + +// Account is an accounts service interface +type Account interface { + // Read a record + Read(key string) (*Record, error) + // Write a record + Write(Record) Record + // List all records + List() []*Record +} + +// Record is an entry in the account storage +type Record struct { + Key string + Value []byte +} From bdc66633ddfa76a77fbee6b689deacde9e3c9e80 Mon Sep 17 00:00:00 2001 From: "A.Unger" Date: Mon, 3 Feb 2020 20:05:22 +0100 Subject: [PATCH 03/14] rename -> account --- pkg/{accounts => account}/accounts.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) rename pkg/{accounts => account}/accounts.go (94%) diff --git a/pkg/accounts/accounts.go b/pkg/account/accounts.go similarity index 94% rename from pkg/accounts/accounts.go rename to pkg/account/accounts.go index f49b787fa..2760e145a 100644 --- a/pkg/accounts/accounts.go +++ b/pkg/account/accounts.go @@ -1,4 +1,4 @@ -package accounts +package account // Account is an accounts service interface type Account interface { From 35d9deb38a4bca9cc75ed0c42c9ee70b1465b44c Mon Sep 17 00:00:00 2001 From: "A.Unger" Date: Mon, 3 Feb 2020 22:54:33 +0100 Subject: [PATCH 04/14] custom interface; strategy pattern; service configuration; refactor --- go.mod | 1 + go.sum | 1 + pkg/account/accounts.go | 21 +++++++++-- pkg/command/root.go | 3 +- pkg/command/server.go | 1 - pkg/micro/grpc/grpc.go | 7 ++-- pkg/registry/registry.go | 21 ----------- pkg/service/v0/service.go | 69 ++++++++++++++++++----------------- pkg/store/filesystem/store.go | 64 +++++++++++++++----------------- pkg/store/registry.go | 5 +++ 10 files changed, 94 insertions(+), 99 deletions(-) delete mode 100644 pkg/registry/registry.go create mode 100644 pkg/store/registry.go diff --git a/go.mod b/go.mod index 617335c05..5f96a22f1 100644 --- a/go.mod +++ b/go.mod @@ -3,6 +3,7 @@ module github.com/owncloud/ocis-accounts go 1.13 require ( + github.com/coreos/etcd v3.3.18+incompatible github.com/golang/protobuf v1.3.2 github.com/google/uuid v1.1.1 github.com/micro/cli v0.2.0 diff --git a/go.sum b/go.sum index f8748c4b5..0d49beb03 100644 --- a/go.sum +++ b/go.sum @@ -381,6 +381,7 @@ github.com/jessevdk/go-flags v1.4.0/go.mod h1:4FA24M0QyGHXBuZZK/XkWh8h0e1EYbRYJS github.com/jmespath/go-jmespath v0.0.0-20160202185014-0b12d6b521d8/go.mod h1:Nht3zPeWKUH0NzdCt2Blrr5ys8VGpn0CEB0cQHVjt7k= github.com/jmespath/go-jmespath v0.0.0-20180206201540-c2b33e8439af/go.mod h1:Nht3zPeWKUH0NzdCt2Blrr5ys8VGpn0CEB0cQHVjt7k= github.com/joho/godotenv v1.3.0/go.mod h1:7hK45KPybAkOC6peb+G5yklZfMxEjkZhHbwpqxOKXbg= +github.com/jonboulle/clockwork v0.1.0 h1:VKV+ZcuP6l3yW9doeqz6ziZGgcynBVQO+obU0+0hcPo= github.com/jonboulle/clockwork v0.1.0/go.mod h1:Ii8DK3G1RaLaWxj9trq07+26W01tbo22gdxWY5EU2bo= github.com/joncalhoun/qson v0.0.0-20170526102502-8a9cab3a62b1/go.mod h1:DFXrEwSRX0p/aSvxE21319menCBFeQO0jXpRj7LEZUA= github.com/json-iterator/go v0.0.0-20180612202835-f2b4162afba3/go.mod h1:+SdeFBvtyEkXs7REEP0seUULqWtbJapLOCVDaaPEHmU= diff --git a/pkg/account/accounts.go b/pkg/account/accounts.go index 2760e145a..4fa0fb09b 100644 --- a/pkg/account/accounts.go +++ b/pkg/account/accounts.go @@ -1,11 +1,24 @@ package account -// Account is an accounts service interface -type Account interface { +import "github.com/owncloud/ocis-accounts/pkg/config" + +var ( + // Registry uses the strategy pattern as a registry + Registry = map[string]RegisterFunc{} + + // DefaultManager defines the default accounts manager + DefaultManager = "filesystem" +) + +// RegisterFunc stores store constructors +type RegisterFunc func(*config.Config) Manager + +// Manager is an accounts service interface +type Manager interface { // Read a record - Read(key string) (*Record, error) + Read(key string) *Record // Write a record - Write(Record) Record + Write(*Record) *Record // List all records List() []*Record } diff --git a/pkg/command/root.go b/pkg/command/root.go index 972cdd2cb..1dc3777ab 100644 --- a/pkg/command/root.go +++ b/pkg/command/root.go @@ -4,7 +4,8 @@ import ( "os" "github.com/micro/cli" - _ "github.com/owncloud/ocis-accounts/pkg/registry" + // init store manager + _ "github.com/owncloud/ocis-accounts/pkg/store" "github.com/owncloud/ocis-hello/pkg/version" ) diff --git a/pkg/command/server.go b/pkg/command/server.go index 9ed90f398..6bec060c4 100644 --- a/pkg/command/server.go +++ b/pkg/command/server.go @@ -25,7 +25,6 @@ func Server() cli.Command { gr.Add(func() error { return service.Run() }, func(_ error) { - fmt.Println("shutting down grpc server") cancel() }) diff --git a/pkg/micro/grpc/grpc.go b/pkg/micro/grpc/grpc.go index 20e9b41a2..874b69e2d 100644 --- a/pkg/micro/grpc/grpc.go +++ b/pkg/micro/grpc/grpc.go @@ -1,15 +1,15 @@ package grpc -// package grpc uses `ocis-pkg` to start a go-micro service import ( "context" + "github.com/owncloud/ocis-accounts/pkg/config" "github.com/owncloud/ocis-accounts/pkg/proto/v0" svc "github.com/owncloud/ocis-accounts/pkg/service/v0" "github.com/owncloud/ocis-pkg/service/grpc" ) -// NewService initializes a new go-micro service ready to run +// NewService creates a grpc service func NewService(c context.Context) grpc.Service { service := grpc.NewService( grpc.Name("accounts"), @@ -18,8 +18,7 @@ func NewService(c context.Context) grpc.Service { grpc.Context(c), ) - // add a handler to the service - hdlr := svc.New() + hdlr := svc.New(config.New()) proto.RegisterSettingsServiceHandler(service.Server(), hdlr) service.Init() diff --git a/pkg/registry/registry.go b/pkg/registry/registry.go deleted file mode 100644 index 1dae9ba8f..000000000 --- a/pkg/registry/registry.go +++ /dev/null @@ -1,21 +0,0 @@ -// Package registry provides accessors to runtime services -package registry - -import ( - "sync" - - mstore "github.com/micro/go-micro/v2/store" - store "github.com/owncloud/ocis-accounts/pkg/store/filesystem" -) - -var ( - once *sync.Once = &sync.Once{} - // Store is a micro store implementation - Store mstore.Store -) - -func init() { - once.Do(func() { - Store = store.New() - }) -} diff --git a/pkg/service/v0/service.go b/pkg/service/v0/service.go index 79b4bb448..c0a7a0edd 100644 --- a/pkg/service/v0/service.go +++ b/pkg/service/v0/service.go @@ -3,23 +3,30 @@ package service import ( "context" "encoding/json" + "fmt" "github.com/golang/protobuf/ptypes/empty" - mstore "github.com/micro/go-micro/v2/store" + "github.com/owncloud/ocis-accounts/pkg/account" + "github.com/owncloud/ocis-accounts/pkg/config" "github.com/owncloud/ocis-accounts/pkg/proto/v0" - - "github.com/owncloud/ocis-accounts/pkg/registry" ) // New returns a new instance of Service -func New() Service { - return Service{} +func New(cfg *config.Config) Service { + fmt.Printf("config type: %T", account.Registry["filesystem"]) + return Service{ + Config: cfg, + Manager: account.Registry["filesystem"](cfg), // TODO read this from config + } } -// Service implements the SettingsServiceHandler interface generated on accounts.pb.micro.go -type Service struct{} +// Service implements the SettingsServiceHandler interface +type Service struct { + Config *config.Config + Manager account.Manager +} -// Set implements the SettingsServiceHandler interface generated on accounts.pb.micro.go +// 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) @@ -27,45 +34,41 @@ func (s Service) Set(c context.Context, req *proto.Record, res *proto.Record) er return err } - record := mstore.Record{ + s.Manager.Write(&account.Record{ Key: req.Key, Value: settingsJSON, - } + }) - return registry.Store.Write(&record) + return nil } -// Get implements the SettingsServiceHandler interface generated on accounts.pb.micro.go +// Get implements the SettingsServiceHandler interface func (s Service) Get(c context.Context, req *proto.Query, res *proto.Record) error { - contents, err := registry.Store.Read(req.Key) - if err != nil { - return err - } + // contents, err := registry.Store.Read(req.Key) + contents := s.Manager.Read(req.Key) - if len(contents) > 0 { - r := &proto.Payload{} - json.Unmarshal(contents[0].Value, r) - res.Payload = r - } + r := &proto.Payload{} + json.Unmarshal(contents.Value, r) + res.Payload = r return nil } -// List implements the SettingsServiceHandler interface generated on accounts.pb.micro.go +// 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 - } + // 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, - }) - } + // for _, v := range contents { + // r.Records = append(r.Records, &proto.Record{ + // Key: v.Key, + // }) + // } - res.Records = r.Records + // res.Records = r.Records return nil } diff --git a/pkg/store/filesystem/store.go b/pkg/store/filesystem/store.go index 2b2691494..d27d4d2ef 100644 --- a/pkg/store/filesystem/store.go +++ b/pkg/store/filesystem/store.go @@ -2,18 +2,25 @@ package store import ( - "fmt" "io/ioutil" "os" "path" "path/filepath" - mstore "github.com/micro/go-micro/v2/store" + "github.com/owncloud/ocis-accounts/pkg/account" + "github.com/owncloud/ocis-accounts/pkg/config" olog "github.com/owncloud/ocis-pkg/log" ) +var ( + // StoreName is the default name for the accounts store + StoreName string = "ocis-store" + + // managerName + managerName = "filesystem" +) + // StoreName is the default name for the store container -var StoreName string = "ocis-store" // Store interacts with the filesystem to manage account information type Store struct { @@ -21,8 +28,8 @@ type Store struct { Logger olog.Logger } -// New returns a new stor. TODO add mountPath as a flag. Accept a *config argument -func New() *Store { +// New creates a new store +func New(cfg *config.Config) account.Manager { s := Store{ Logger: olog.NewLogger(), } @@ -43,68 +50,55 @@ func New() *Store { return &s } -// Init implements the store interface -func (s Store) Init(...mstore.Option) error { - return nil -} - // List returns all the identities in the mountPath folder -func (s Store) List() ([]*mstore.Record, error) { - records := []*mstore.Record{} +func (s Store) List() []*account.Record { + records := []*account.Record{} identities, err := ioutil.ReadDir(s.mountPath) if err != nil { s.Logger.Err(err).Msgf("error reading %v", s.mountPath) + return records } s.Logger.Info().Msg("listing identities") for _, v := range identities { - records = append(records, &mstore.Record{ + records = append(records, &account.Record{ Key: v.Name(), }) } - return records, nil + return records } // Read implements the store interface. This implementation only reads by id. -func (s Store) Read(key string, opts ...mstore.ReadOption) ([]*mstore.Record, error) { +func (s Store) Read(key string) *account.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 []*mstore.Record{}, err + return &account.Record{} } - return []*mstore.Record{ - &mstore.Record{ - Key: key, - Value: contents, - }, - }, nil + return &account.Record{ + Key: key, + Value: contents, + } } // Write implements the store interface -func (s Store) Write(rec *mstore.Record) error { +func (s Store) Write(rec *account.Record) *account.Record { path := filepath.Join(s.mountPath, rec.Key) if len(rec.Key) < 1 { s.Logger.Error().Msg("key cannot be empty") - return fmt.Errorf("%v", "key is empty") + return &account.Record{} } if err := ioutil.WriteFile(path, rec.Value, 0644); err != nil { - return err + return &account.Record{} } s.Logger.Info().Msgf("%v bytes written to %v", len(rec.Value), path) - return nil + return rec } - -// Delete implements the store interface -func (s Store) Delete(key string) error { - return nil -} - -// String implements the store interface, and the stringer interface -func (s Store) String() string { - return "store" +func init() { + account.Registry[managerName] = New } diff --git a/pkg/store/registry.go b/pkg/store/registry.go new file mode 100644 index 000000000..e4a32c66d --- /dev/null +++ b/pkg/store/registry.go @@ -0,0 +1,5 @@ +package store + +import ( + _ "github.com/owncloud/ocis-accounts/pkg/store/filesystem" +) From 7431297a1c9ebc511b788c25997672d5c271362a Mon Sep 17 00:00:00 2001 From: "A.Unger" Date: Tue, 4 Feb 2020 10:09:38 +0100 Subject: [PATCH 05/14] use micro cli v2 --- go.mod | 1 + pkg/command/root.go | 6 +++--- pkg/command/server.go | 6 +++--- pkg/service/v0/service.go | 2 +- 4 files changed, 8 insertions(+), 7 deletions(-) diff --git a/go.mod b/go.mod index 5f96a22f1..8a2c149c2 100644 --- a/go.mod +++ b/go.mod @@ -7,6 +7,7 @@ require ( github.com/golang/protobuf v1.3.2 github.com/google/uuid v1.1.1 github.com/micro/cli v0.2.0 + github.com/micro/cli/v2 v2.1.1 github.com/micro/go-micro v1.18.0 github.com/micro/go-micro/v2 v2.0.0 github.com/oklog/run v1.1.0 diff --git a/pkg/command/root.go b/pkg/command/root.go index 1dc3777ab..01f884660 100644 --- a/pkg/command/root.go +++ b/pkg/command/root.go @@ -3,7 +3,7 @@ package command import ( "os" - "github.com/micro/cli" + "github.com/micro/cli/v2" // init store manager _ "github.com/owncloud/ocis-accounts/pkg/store" "github.com/owncloud/ocis-hello/pkg/version" @@ -16,14 +16,14 @@ func Execute() error { Version: version.String, Usage: "Example service for Reva/oCIS", - Authors: []cli.Author{ + Authors: []*cli.Author{ { Name: "ownCloud GmbH", Email: "support@owncloud.com", }, }, - Commands: []cli.Command{ + Commands: []*cli.Command{ Server(), }, } diff --git a/pkg/command/server.go b/pkg/command/server.go index 6bec060c4..a0a867f7f 100644 --- a/pkg/command/server.go +++ b/pkg/command/server.go @@ -5,14 +5,14 @@ import ( "fmt" "syscall" - "github.com/micro/cli" + "github.com/micro/cli/v2" "github.com/oklog/run" "github.com/owncloud/ocis-accounts/pkg/micro/grpc" ) // Server is the entry point for the server command. -func Server() cli.Command { - return cli.Command{ +func Server() *cli.Command { + return &cli.Command{ Name: "server", Usage: "Start accounts service", Action: func(c *cli.Context) error { diff --git a/pkg/service/v0/service.go b/pkg/service/v0/service.go index c0a7a0edd..4a7c8fc2c 100644 --- a/pkg/service/v0/service.go +++ b/pkg/service/v0/service.go @@ -16,7 +16,7 @@ func New(cfg *config.Config) Service { fmt.Printf("config type: %T", account.Registry["filesystem"]) return Service{ Config: cfg, - Manager: account.Registry["filesystem"](cfg), // TODO read this from config + Manager: account.Registry["filesystem"](cfg), // TODO read manager from config } } From 2ff87de71f9c3eac75a9f627af4058eac8655e79 Mon Sep 17 00:00:00 2001 From: "A.Unger" Date: Tue, 4 Feb 2020 10:16:12 +0100 Subject: [PATCH 06/14] toss config struct around --- pkg/command/root.go | 6 ++++-- pkg/command/server.go | 3 ++- pkg/micro/grpc/grpc.go | 1 + 3 files changed, 7 insertions(+), 3 deletions(-) diff --git a/pkg/command/root.go b/pkg/command/root.go index 01f884660..66369097c 100644 --- a/pkg/command/root.go +++ b/pkg/command/root.go @@ -4,9 +4,11 @@ import ( "os" "github.com/micro/cli/v2" + "github.com/owncloud/ocis-accounts/pkg/config" + "github.com/owncloud/ocis-hello/pkg/version" + // init store manager _ "github.com/owncloud/ocis-accounts/pkg/store" - "github.com/owncloud/ocis-hello/pkg/version" ) // Execute is the entry point for the ocis-accounts command. @@ -24,7 +26,7 @@ func Execute() error { }, Commands: []*cli.Command{ - Server(), + Server(config.New()), }, } diff --git a/pkg/command/server.go b/pkg/command/server.go index a0a867f7f..b997157eb 100644 --- a/pkg/command/server.go +++ b/pkg/command/server.go @@ -7,11 +7,12 @@ import ( "github.com/micro/cli/v2" "github.com/oklog/run" + "github.com/owncloud/ocis-accounts/pkg/config" "github.com/owncloud/ocis-accounts/pkg/micro/grpc" ) // Server is the entry point for the server command. -func Server() *cli.Command { +func Server(cfg *config.Config) *cli.Command { return &cli.Command{ Name: "server", Usage: "Start accounts service", diff --git a/pkg/micro/grpc/grpc.go b/pkg/micro/grpc/grpc.go index 874b69e2d..5676a56a9 100644 --- a/pkg/micro/grpc/grpc.go +++ b/pkg/micro/grpc/grpc.go @@ -12,6 +12,7 @@ import ( // NewService creates a grpc service func NewService(c context.Context) grpc.Service { service := grpc.NewService( + // TODO options come from configuration grpc.Name("accounts"), grpc.Namespace("com.owncloud"), grpc.Address("localhost:9999"), From 79aaba63fcc1d725294f8b7df2022d19f57247ee Mon Sep 17 00:00:00 2001 From: "A.Unger" Date: Tue, 4 Feb 2020 11:54:45 +0100 Subject: [PATCH 07/14] added flags to server command --- pkg/command/server.go | 24 +++++++++++++++++++++++- pkg/service/v0/service.go | 1 - 2 files changed, 23 insertions(+), 2 deletions(-) diff --git a/pkg/command/server.go b/pkg/command/server.go index b997157eb..d27b9d01d 100644 --- a/pkg/command/server.go +++ b/pkg/command/server.go @@ -3,6 +3,8 @@ package command import ( "context" "fmt" + "os" + "path/filepath" "syscall" "github.com/micro/cli/v2" @@ -13,15 +15,35 @@ import ( // Server is the entry point for the server command. func Server(cfg *config.Config) *cli.Command { + baseDir, _ := filepath.Abs(filepath.Dir(os.Args[0])) + return &cli.Command{ Name: "server", Usage: "Start accounts service", + Flags: []cli.Flag{ + &cli.StringFlag{ + Name: "manager", + DefaultText: "filesystem", + Usage: "store controller driver. eg: filesystem", + Value: "filesystem", + EnvVars: []string{"OCIS_ACCOUNTS_MANAGER"}, + Destination: &cfg.Manager, + }, + &cli.StringFlag{ + Name: "mount-path", + DefaultText: "binary default running location", + Usage: "where to mount the ocis accounts store", + Value: baseDir, + EnvVars: []string{"OCIS_ACCOUNTS_MOUNT_PATH"}, + Destination: &cfg.MountPath, + }, + }, Action: func(c *cli.Context) error { gr := run.Group{} ctx, cancel := context.WithCancel(context.Background()) defer cancel() - service := grpc.NewService(ctx) + service := grpc.NewService(ctx, cfg) gr.Add(func() error { return service.Run() diff --git a/pkg/service/v0/service.go b/pkg/service/v0/service.go index 4a7c8fc2c..f6789bce1 100644 --- a/pkg/service/v0/service.go +++ b/pkg/service/v0/service.go @@ -44,7 +44,6 @@ func (s Service) Set(c context.Context, req *proto.Record, res *proto.Record) er // Get implements the SettingsServiceHandler interface func (s Service) Get(c context.Context, req *proto.Query, res *proto.Record) error { - // contents, err := registry.Store.Read(req.Key) contents := s.Manager.Read(req.Key) r := &proto.Payload{} From a43512834ec0726f68ffc7f799ec21f2c54555ab Mon Sep 17 00:00:00 2001 From: "A.Unger" Date: Tue, 4 Feb 2020 11:55:04 +0100 Subject: [PATCH 08/14] manager configurable --- pkg/config/config.go | 1 + 1 file changed, 1 insertion(+) diff --git a/pkg/config/config.go b/pkg/config/config.go index bf7229720..68577874d 100644 --- a/pkg/config/config.go +++ b/pkg/config/config.go @@ -4,6 +4,7 @@ package config // Config captures ocis-accounts configuration parameters type Config struct { MountPath string + Manager string } // New returns a new config From cb3516a706344d7e9bb5fe016fb6715d5e14f3cc Mon Sep 17 00:00:00 2001 From: "A.Unger" Date: Tue, 4 Feb 2020 11:55:25 +0100 Subject: [PATCH 09/14] injected config down the tree --- pkg/micro/grpc/grpc.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pkg/micro/grpc/grpc.go b/pkg/micro/grpc/grpc.go index 5676a56a9..be538834c 100644 --- a/pkg/micro/grpc/grpc.go +++ b/pkg/micro/grpc/grpc.go @@ -10,7 +10,7 @@ import ( ) // NewService creates a grpc service -func NewService(c context.Context) grpc.Service { +func NewService(c context.Context, cfg *config.Config) grpc.Service { service := grpc.NewService( // TODO options come from configuration grpc.Name("accounts"), @@ -19,7 +19,7 @@ func NewService(c context.Context) grpc.Service { grpc.Context(c), ) - hdlr := svc.New(config.New()) + hdlr := svc.New(cfg) proto.RegisterSettingsServiceHandler(service.Server(), hdlr) service.Init() From 32c5bb871c916b2fca978fd5abe69e6ecfd1dad9 Mon Sep 17 00:00:00 2001 From: "A.Unger" Date: Tue, 4 Feb 2020 11:55:48 +0100 Subject: [PATCH 10/14] read manager from the config definition --- pkg/service/v0/service.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/service/v0/service.go b/pkg/service/v0/service.go index f6789bce1..f71fc6849 100644 --- a/pkg/service/v0/service.go +++ b/pkg/service/v0/service.go @@ -16,7 +16,7 @@ func New(cfg *config.Config) Service { fmt.Printf("config type: %T", account.Registry["filesystem"]) return Service{ Config: cfg, - Manager: account.Registry["filesystem"](cfg), // TODO read manager from config + Manager: account.Registry[cfg.Manager](cfg), } } From dbd98ac203d32ceffca4d8334e4e5a97f8d75fb2 Mon Sep 17 00:00:00 2001 From: "A.Unger" Date: Tue, 4 Feb 2020 11:55:57 +0100 Subject: [PATCH 11/14] refactor the store --- pkg/store/filesystem/store.go | 13 +++++-------- 1 file changed, 5 insertions(+), 8 deletions(-) diff --git a/pkg/store/filesystem/store.go b/pkg/store/filesystem/store.go index d27d4d2ef..9c98d9c8b 100644 --- a/pkg/store/filesystem/store.go +++ b/pkg/store/filesystem/store.go @@ -34,16 +34,13 @@ func New(cfg *config.Config) account.Manager { Logger: olog.NewLogger(), } - // default to the current working directory if not configured - dir, err := filepath.Abs(filepath.Dir(os.Args[0])) - if err != nil { - s.Logger.Err(err).Msg("initializing accounts store") - } - - dest := filepath.Join(dir, StoreName) + dest := filepath.Join(cfg.MountPath, StoreName) if _, err := os.Stat(dest); err != nil { s.Logger.Info().Msgf("creating container on %v", dest) - os.Mkdir(dest, 0700) + err := os.MkdirAll(dest, 0700) + if err != nil { + s.Logger.Err(err).Msgf("providing container on %v", dest) + } } s.mountPath = dest From ac20460e7c5c2479d9304834113f474f943a6af7 Mon Sep 17 00:00:00 2001 From: "A.Unger" Date: Tue, 4 Feb 2020 11:56:42 +0100 Subject: [PATCH 12/14] leftover print --- pkg/service/v0/service.go | 2 -- 1 file changed, 2 deletions(-) diff --git a/pkg/service/v0/service.go b/pkg/service/v0/service.go index f71fc6849..2b1fd6190 100644 --- a/pkg/service/v0/service.go +++ b/pkg/service/v0/service.go @@ -3,7 +3,6 @@ package service import ( "context" "encoding/json" - "fmt" "github.com/golang/protobuf/ptypes/empty" "github.com/owncloud/ocis-accounts/pkg/account" @@ -13,7 +12,6 @@ import ( // New returns a new instance of Service func New(cfg *config.Config) Service { - fmt.Printf("config type: %T", account.Registry["filesystem"]) return Service{ Config: cfg, Manager: account.Registry[cfg.Manager](cfg), From dade720bf599651444250a2807a8ba153b28910e Mon Sep 17 00:00:00 2001 From: "A.Unger" Date: Tue, 4 Feb 2020 12:17:41 +0100 Subject: [PATCH 13/14] fatal on unknown driver; named logger --- pkg/service/v0/service.go | 15 ++++++++++++--- pkg/store/filesystem/store.go | 9 ++++----- 2 files changed, 16 insertions(+), 8 deletions(-) diff --git a/pkg/service/v0/service.go b/pkg/service/v0/service.go index 2b1fd6190..682e51dbc 100644 --- a/pkg/service/v0/service.go +++ b/pkg/service/v0/service.go @@ -8,14 +8,23 @@ import ( "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" ) // New returns a new instance of Service func New(cfg *config.Config) Service { - return Service{ - Config: cfg, - Manager: account.Registry[cfg.Manager](cfg), + s := Service{ + Config: cfg, } + + if newReg, ok := account.Registry[cfg.Manager]; ok { + s.Manager = newReg(cfg) + } else { + l := olog.NewLogger(olog.Name("ocis-accounts")) + l.Fatal().Msgf("driver does not exist: %v", cfg.Manager) + } + + return s } // Service implements the SettingsServiceHandler interface diff --git a/pkg/store/filesystem/store.go b/pkg/store/filesystem/store.go index 9c98d9c8b..a4a5a09d4 100644 --- a/pkg/store/filesystem/store.go +++ b/pkg/store/filesystem/store.go @@ -14,10 +14,8 @@ import ( var ( // StoreName is the default name for the accounts store - StoreName string = "ocis-store" - - // managerName - managerName = "filesystem" + StoreName string = "ocis-store" + managerName = "filesystem" ) // StoreName is the default name for the store container @@ -31,7 +29,7 @@ type Store struct { // New creates a new store func New(cfg *config.Config) account.Manager { s := Store{ - Logger: olog.NewLogger(), + Logger: olog.NewLogger(olog.Name("ocis-accounts")), } dest := filepath.Join(cfg.MountPath, StoreName) @@ -96,6 +94,7 @@ func (s Store) Write(rec *account.Record) *account.Record { s.Logger.Info().Msgf("%v bytes written to %v", len(rec.Value), path) return rec } + func init() { account.Registry[managerName] = New } From 517ff78d7ff2277ab2c63f580fd391ddb3fb4409 Mon Sep 17 00:00:00 2001 From: "A.Unger" Date: Tue, 4 Feb 2020 12:18:18 +0100 Subject: [PATCH 14/14] less confusing error message --- pkg/service/v0/service.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/service/v0/service.go b/pkg/service/v0/service.go index 682e51dbc..0a432c1e2 100644 --- a/pkg/service/v0/service.go +++ b/pkg/service/v0/service.go @@ -21,7 +21,7 @@ func New(cfg *config.Config) Service { s.Manager = newReg(cfg) } else { l := olog.NewLogger(olog.Name("ocis-accounts")) - l.Fatal().Msgf("driver does not exist: %v", cfg.Manager) + l.Fatal().Msgf("unknown manager: %v", cfg.Manager) } return s