From 9f58f8e83ebca5b6de2e11e11f6e3270ce0a2ad4 Mon Sep 17 00:00:00 2001 From: Denis Arh Date: Wed, 31 Jul 2019 15:20:09 +0200 Subject: [PATCH] Migrate storage path to options.StorageOpt struct --- compose/compose.go | 6 +++--- compose/internal/service/service.go | 11 ++++++++--- messaging/internal/service/main_test.go | 7 ++++++- messaging/internal/service/service.go | 11 ++++++++--- messaging/messaging.go | 5 +++-- pkg/cli/options/storage.go | 17 +++++++++++++++++ pkg/cli/runner.go | 2 ++ 7 files changed, 47 insertions(+), 12 deletions(-) create mode 100644 pkg/cli/options/storage.go diff --git a/compose/compose.go b/compose/compose.go index 975a80883..d9543f369 100644 --- a/compose/compose.go +++ b/compose/compose.go @@ -11,7 +11,6 @@ import ( "github.com/cortezaproject/corteza-server/compose/internal/service" "github.com/cortezaproject/corteza-server/compose/rest" "github.com/cortezaproject/corteza-server/pkg/cli" - "github.com/cortezaproject/corteza-server/pkg/cli/options" ) const ( @@ -36,8 +35,9 @@ func Configure() *cli.Config { } servicesInitialized = true - storagePath := options.EnvString("", "COMPOSE_STORAGE_PATH", "var/store") - cli.HandleError(service.Init(ctx, c.Log, storagePath)) + cli.HandleError(service.Init(ctx, c.Log, service.Config{ + Storage: *c.StorageOpt, + })) }, ApiServerPreRun: cli.Runners{ diff --git a/compose/internal/service/service.go b/compose/internal/service/service.go index 2484e945b..6991c0bf0 100644 --- a/compose/internal/service/service.go +++ b/compose/internal/service/service.go @@ -9,6 +9,7 @@ import ( "github.com/cortezaproject/corteza-server/compose/internal/repository" "github.com/cortezaproject/corteza-server/internal/permissions" "github.com/cortezaproject/corteza-server/internal/store" + "github.com/cortezaproject/corteza-server/pkg/cli/options" ) type ( @@ -16,6 +17,10 @@ type ( accessControlPermissionServicer Watch(ctx context.Context) } + + Config struct { + Storage options.StorageOpt + } ) var ( @@ -35,11 +40,11 @@ var ( DefaultNamespace NamespaceService ) -func Init(ctx context.Context, log *zap.Logger, storePath string) (err error) { +func Init(ctx context.Context, log *zap.Logger, c Config) (err error) { DefaultLogger = log.Named("service") - fs, err := store.New(storePath) - log.Info("initializing store", zap.String("path", storePath), zap.Error(err)) + fs, err := store.New(c.Storage.Path) + log.Info("initializing store", zap.String("path", c.Storage.Path), zap.Error(err)) if err != nil { return err } diff --git a/messaging/internal/service/main_test.go b/messaging/internal/service/main_test.go index 790f81c95..866846eae 100644 --- a/messaging/internal/service/main_test.go +++ b/messaging/internal/service/main_test.go @@ -12,6 +12,7 @@ import ( "go.uber.org/zap" messagingMigrate "github.com/cortezaproject/corteza-server/messaging/db" + "github.com/cortezaproject/corteza-server/pkg/cli/options" "github.com/cortezaproject/corteza-server/pkg/logger" dbLogger "github.com/titpetric/factory/logger" ) @@ -33,7 +34,11 @@ func TestMain(m *testing.M) { return } - Init(context.Background(), zap.NewNop(), "/tmp/corteza-messaging-store") + Init(context.Background(), zap.NewNop(), Config{ + Storage: options.StorageOpt{ + Path: "/tmp/corteza-messaging-store", + }, + }) os.Exit(m.Run()) } diff --git a/messaging/internal/service/service.go b/messaging/internal/service/service.go index 0cdd328ca..00dd76c44 100644 --- a/messaging/internal/service/service.go +++ b/messaging/internal/service/service.go @@ -10,6 +10,7 @@ import ( "github.com/cortezaproject/corteza-server/internal/permissions" "github.com/cortezaproject/corteza-server/internal/store" "github.com/cortezaproject/corteza-server/messaging/internal/repository" + "github.com/cortezaproject/corteza-server/pkg/cli/options" ) type ( @@ -21,6 +22,10 @@ type ( accessControlPermissionServicer Watch(ctx context.Context) } + + Config struct { + Storage options.StorageOpt + } ) var ( @@ -38,11 +43,11 @@ var ( DefaultWebhook WebhookService ) -func Init(ctx context.Context, log *zap.Logger, storePath string) (err error) { +func Init(ctx context.Context, log *zap.Logger, c Config) (err error) { DefaultLogger = log.Named("service") - fs, err := store.New(storePath) - log.Info("initializing store", zap.String("path", storePath), zap.Error(err)) + fs, err := store.New(c.Storage.Path) + log.Info("initializing store", zap.String("path", c.Storage.Path), zap.Error(err)) if err != nil { return err } diff --git a/messaging/messaging.go b/messaging/messaging.go index ec27a9bab..eee8da2c7 100644 --- a/messaging/messaging.go +++ b/messaging/messaging.go @@ -43,8 +43,9 @@ func Configure() *cli.Config { } servicesInitialized = true - storagePath := options.EnvString("", "MESSAGING_STORAGE_PATH", "var/store") - cli.HandleError(service.Init(ctx, c.Log, storagePath)) + cli.HandleError(service.Init(ctx, c.Log, service.Config{ + Storage: *c.StorageOpt, + })) }, ApiServerPreRun: cli.Runners{ diff --git a/pkg/cli/options/storage.go b/pkg/cli/options/storage.go new file mode 100644 index 000000000..e1ab81e51 --- /dev/null +++ b/pkg/cli/options/storage.go @@ -0,0 +1,17 @@ +package options + +type ( + StorageOpt struct { + Path string `env:"STORAGE_PATH"` + } +) + +func Storage(pfix string) (o *StorageOpt) { + o = &StorageOpt{ + Path: "var/store", + } + + fill(o, pfix) + + return +} diff --git a/pkg/cli/runner.go b/pkg/cli/runner.go index f68ecd4a5..c584f57f8 100644 --- a/pkg/cli/runner.go +++ b/pkg/cli/runner.go @@ -49,6 +49,7 @@ type ( DbOpt *options.DBOpt ProvisionOpt *options.ProvisionOpt SentryOpt *options.SentryOpt + StorageOpt *options.StorageOpt // DB Connection name, defaults to ServiceName DatabaseName string @@ -186,6 +187,7 @@ func (c *Config) Init() { c.DbOpt = options.DB(c.ServiceName) c.ProvisionOpt = options.Provision(c.ServiceName) c.SentryOpt = options.Sentry(c.EnvPrefix) + c.StorageOpt = options.Storage(c.EnvPrefix) if c.RootCommandDBSetup == nil { c.RootCommandDBSetup = Runners{func(ctx context.Context, cmd *cobra.Command, c *Config) (err error) {