Lint: Reduce complexity of loadConfig function

Signed-off-by: Knut Ahlers <knut@ahlers.me>
This commit is contained in:
Knut Ahlers 2021-04-21 20:03:25 +02:00
parent e2cad4e839
commit 29fd5f25f5
Signed by: luzifer
GPG Key ID: 0066F03ED215AD7D

View File

@ -56,38 +56,11 @@ func loadConfig(filename string) error {
log.Warn("Loaded config with empty ruleset") 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() configLock.Lock()
defer configLock.Unlock() defer configLock.Unlock()
tmpConfig.updateAutoMessagesFromConfig(config)
switch { switch {
case config != nil && config.RawLog == tmpConfig.RawLog: case config != nil && config.RawLog == tmpConfig.RawLog:
tmpConfig.rawLogWriter = config.rawLogWriter tmpConfig.rawLogWriter = config.rawLogWriter
@ -141,3 +114,34 @@ func (c configFile) LogRawMessage(m *irc.Message) error {
_, err := fmt.Fprintln(c.rawLogWriter, m.String()) _, err := fmt.Fprintln(c.rawLogWriter, m.String())
return errors.Wrap(err, "writing raw log message") 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
}
}
}