Skip to content

Commit

Permalink
fix config reader / logging
Browse files Browse the repository at this point in the history
  • Loading branch information
evlekht committed Oct 15, 2024
1 parent 06733a6 commit ffda261
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 23 deletions.
33 changes: 22 additions & 11 deletions cmd/camino_messenger_bot.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Check failure on line 52 in cmd/camino_messenger_bot.go

View workflow job for this annotation

GitHub Actions / Static Analysis

Error return value of `sugaredConfigReaderLogger.Sync` is not checked (errcheck)

var err error
var zapLogger *zap.Logger
if configReader.IsDevelopmentMode() {
zapLogger, err = zap.NewDevelopment()
Expand All @@ -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)
Expand Down
15 changes: 7 additions & 8 deletions config/config_reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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(".", "_"))
Expand Down
5 changes: 3 additions & 2 deletions config/config_reader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
2 changes: 1 addition & 1 deletion examples/config/camino-messenger-bot-supplier-camino.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down

0 comments on commit ffda261

Please sign in to comment.