From d4fe27b6a3ebbe8307db1b126d44dbf2e6e072e9 Mon Sep 17 00:00:00 2001 From: PxyUp Date: Wed, 31 Jan 2024 19:31:32 +0100 Subject: [PATCH] code(pkg/notifier) - imrpove console --- pkg/config/config.go | 4 +++- pkg/notifier/console.go | 9 +++++++++ pkg/notifier/telegram.go | 2 +- pkg/processor/processor.go | 2 +- 4 files changed, 14 insertions(+), 3 deletions(-) diff --git a/pkg/config/config.go b/pkg/config/config.go index 6d8273b..8510462 100644 --- a/pkg/config/config.go +++ b/pkg/config/config.go @@ -212,7 +212,9 @@ type HttpConfig struct { Timeout uint32 `yaml:"timeout" json:"timeout"` } -type ConsoleConfig struct{} +type ConsoleConfig struct { + OnlyResult bool `json:"only_result" yaml:"only_result"` +} type RedisNotifierConfig struct { Addr string `json:"addr" yaml:"addr"` diff --git a/pkg/notifier/console.go b/pkg/notifier/console.go index d84e1d9..4644a85 100644 --- a/pkg/notifier/console.go +++ b/pkg/notifier/console.go @@ -16,6 +16,15 @@ type console struct { } func (o *console) notify(record *singleRecord) error { + if o.cfg.OnlyResult { + _, errOut := fmt.Fprintln(os.Stdout, string(record.Body)) + if errOut != nil { + o.logger.Errorw("cant send to stdout", "error", errOut.Error()) + return errOut + } + return nil + } + bb, err := json.Marshal(record) if err != nil { o.logger.Errorw("cant unmarshal record", "error", err.Error()) diff --git a/pkg/notifier/telegram.go b/pkg/notifier/telegram.go index dd61ddf..7c175f3 100644 --- a/pkg/notifier/telegram.go +++ b/pkg/notifier/telegram.go @@ -35,7 +35,7 @@ var ( _ Notifier = &telegramBot{} ) -func NewTelegramBot(name string, generalCfg *config.NotifierConfig, cfg *config.TelegramBotConfig) (*telegramBot, error) { +func NewTelegramBot(name string, cfg *config.TelegramBotConfig) (*telegramBot, error) { botApi, err := tgbotapi.NewBotAPI(utils.Format(cfg.Token, nil, nil)) if err != nil { return nil, err diff --git a/pkg/processor/processor.go b/pkg/processor/processor.go index 2ace121..1b74651 100644 --- a/pkg/processor/processor.go +++ b/pkg/processor/processor.go @@ -103,7 +103,7 @@ func CreateProcessor(item *config.Item, refMap config.RefMap, logger logger.Logg if item.NotifierConfig != nil { if item.NotifierConfig.TelegramBot != nil { - tgBot, errBot := notifier.NewTelegramBot(item.Name, item.NotifierConfig, item.NotifierConfig.TelegramBot) + tgBot, errBot := notifier.NewTelegramBot(item.Name, item.NotifierConfig.TelegramBot) if errBot != nil { logger.Infow("cant setup telegram bot notifier", "error", errBot.Error()) } else {