mirror of
https://github.com/Luzifer/twitch-bot.git
synced 2025-03-16 03:35:59 +00:00
Lint: Reduce complexity of loadConfig function
Signed-off-by: Knut Ahlers <knut@ahlers.me>
This commit is contained in:
parent
e2cad4e839
commit
29fd5f25f5
1 changed files with 33 additions and 29 deletions
62
config.go
62
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
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue