From 29fd5f25f57233e41bcd933bce0e373f8b8432c3 Mon Sep 17 00:00:00 2001 From: Knut Ahlers Date: Wed, 21 Apr 2021 20:03:25 +0200 Subject: [PATCH] Lint: Reduce complexity of loadConfig function Signed-off-by: Knut Ahlers --- config.go | 62 +++++++++++++++++++++++++++++-------------------------- 1 file changed, 33 insertions(+), 29 deletions(-) diff --git a/config.go b/config.go index 09ad0d7..d3f9e89 100644 --- a/config.go +++ b/config.go @@ -56,38 +56,11 @@ func loadConfig(filename string) error { log.Warn("Loaded config with empty ruleset") } - for idx, nam := range tmpConfig.AutoMessages { - // By default assume last message to be sent now - // in order not to spam messages at startup - nam.lastMessageSent = time.Now() - - if !nam.IsValid() { - log.WithField("index", idx).Warn("Auto-Message configuration is invalid and therefore disabled") - } - - if config == nil { - // Initial config load, do not update timers - continue - } - - for _, oam := range config.AutoMessages { - if nam.ID() != oam.ID() { - continue - } - - // We disable the old message as executing it would - // mess up the constraints of the new message - oam.lock.Lock() - oam.disabled = true - - nam.lastMessageSent = oam.lastMessageSent - nam.linesSinceLastMessage = oam.linesSinceLastMessage - } - } - configLock.Lock() defer configLock.Unlock() + tmpConfig.updateAutoMessagesFromConfig(config) + switch { case config != nil && config.RawLog == tmpConfig.RawLog: tmpConfig.rawLogWriter = config.rawLogWriter @@ -141,3 +114,34 @@ func (c configFile) LogRawMessage(m *irc.Message) error { _, err := fmt.Fprintln(c.rawLogWriter, m.String()) return errors.Wrap(err, "writing raw log message") } + +func (c *configFile) updateAutoMessagesFromConfig(old *configFile) { + for idx, nam := range c.AutoMessages { + // By default assume last message to be sent now + // in order not to spam messages at startup + nam.lastMessageSent = time.Now() + + if !nam.IsValid() { + log.WithField("index", idx).Warn("Auto-Message configuration is invalid and therefore disabled") + } + + if old == nil { + // Initial config load, do not update timers + continue + } + + for _, oam := range old.AutoMessages { + if nam.ID() != oam.ID() { + continue + } + + // We disable the old message as executing it would + // mess up the constraints of the new message + oam.lock.Lock() + oam.disabled = true + + nam.lastMessageSent = oam.lastMessageSent + nam.linesSinceLastMessage = oam.linesSinceLastMessage + } + } +}