diff --git a/cmd/camino_messenger_bot.go b/cmd/camino_messenger_bot.go index 2bf8ce02..4201c60a 100644 --- a/cmd/camino_messenger_bot.go +++ b/cmd/camino_messenger_bot.go @@ -28,9 +28,29 @@ var rootCmd = &cobra.Command{ } func rootFunc(cmd *cobra.Command, _ []string) error { - configReader := config.NewConfigReader(cmd.Flags()) + configReaderLogger, err := zap.NewProduction() + if err != nil { + return fmt.Errorf("failed to create config-reader logger: %w", err) + } + + sugaredConfigReaderLogger := configReaderLogger.Sugar() + defer func() { _ = sugaredConfigReaderLogger.Sync() }() + + configReader, err := config.NewConfigReader(cmd.Flags(), sugaredConfigReaderLogger) + if err != nil { + return fmt.Errorf("failed to create config reader: %w", err) + } + + ctx, stop := signal.NotifyContext(context.Background(), syscall.SIGINT, syscall.SIGTERM) + defer stop() + + cfg, err := configReader.ReadConfig() + if err != nil { + return fmt.Errorf("failed to read config: %w", err) + } + + sugaredConfigReaderLogger.Sync() - var err error var zapLogger *zap.Logger if configReader.IsDevelopmentMode() { zapLogger, err = zap.NewDevelopment() @@ -46,15 +66,6 @@ func rootFunc(cmd *cobra.Command, _ []string) error { logger.Infof("App version: %s (git: %s)", Version, GitCommit) - ctx, stop := signal.NotifyContext(context.Background(), syscall.SIGINT, syscall.SIGTERM) - defer stop() - - cfg, err := configReader.ReadConfig(logger) - if err != nil { - logger.Error(err) - return err - } - app, err := app.NewApp(ctx, cfg, logger) if err != nil { logger.Error(err) diff --git a/config/config_reader.go b/config/config_reader.go index bc1b4da8..20bd1dee 100644 --- a/config/config_reader.go +++ b/config/config_reader.go @@ -26,15 +26,16 @@ var ( type Reader interface { IsDevelopmentMode() bool - ReadConfig(logger *zap.SugaredLogger) (*Config, error) + ReadConfig() (*Config, error) } // Returns a new config reader. -func NewConfigReader(flags *pflag.FlagSet) Reader { +func NewConfigReader(flags *pflag.FlagSet, logger *zap.SugaredLogger) (Reader, error) { return &reader{ - viper: viper.New(), - flags: flags, - } + viper: viper.New(), + flags: flags, + logger: logger, + }, nil } type reader struct { @@ -47,9 +48,7 @@ func (cr *reader) IsDevelopmentMode() bool { return cr.viper.GetBool(flagKeyDeveloperMode) } -func (cr *reader) ReadConfig(logger *zap.SugaredLogger) (*Config, error) { - cr.logger = logger - +func (cr *reader) ReadConfig() (*Config, error) { cr.viper.SetEnvPrefix(envPrefix) cr.viper.AutomaticEnv() cr.viper.SetEnvKeyReplacer(strings.NewReplacer(".", "_")) diff --git a/config/config_reader_test.go b/config/config_reader_test.go index 6c758bd5..bfeecdd2 100644 --- a/config/config_reader_test.go +++ b/config/config_reader_test.go @@ -62,10 +62,11 @@ func TestReadConfig(t *testing.T) { for name, tt := range tests { t.Run(name, func(t *testing.T) { - cr := NewConfigReader(tt.flags) + cr, err := NewConfigReader(tt.flags, zap.NewNop().Sugar()) + require.NoError(t, err) tt.prepare(t, cr.(*reader)) - config, err := cr.ReadConfig(zap.NewNop().Sugar()) + config, err := cr.ReadConfig() require.ErrorIs(t, err, tt.expectedErr) require.NoError(t, unsetEnvFromMap(envPrefix, rawMap)) diff --git a/examples/config/camino-messenger-bot-supplier-camino.yaml b/examples/config/camino-messenger-bot-supplier-camino.yaml index 573b2547..8832908c 100644 --- a/examples/config/camino-messenger-bot-supplier-camino.yaml +++ b/examples/config/camino-messenger-bot-supplier-camino.yaml @@ -86,7 +86,7 @@ partner_plugin: -### RPC server (NOT USED FOR SUPPLIER BOT) +### RPC server (NOT USED FOR SUPPLIER BOT IN THIS VERSION) # rpc_server: # enabled: false diff --git a/examples/config/camino-messenger-bot-supplier-columbus.yaml b/examples/config/camino-messenger-bot-supplier-columbus.yaml index bb91b593..f6cd62ab 100644 --- a/examples/config/camino-messenger-bot-supplier-columbus.yaml +++ b/examples/config/camino-messenger-bot-supplier-columbus.yaml @@ -86,7 +86,7 @@ partner_plugin: -### RPC server (NOT USED FOR SUPPLIER BOT) +### RPC server (NOT USED FOR SUPPLIER BOT IN THIS VERSION) # rpc_server: # enabled: false