aerc/config/triggers.go
Robin Jarry cd19995557 logging: use level-based logger functions
Do not pass logger objects around anymore. Shuffle some messages to make
them consistent with the new logging API. Avoid using %v when a more
specific verb exists for the argument types.

The loggers are completely disabled (i.e. Sprintf is not even called)
by default. They are only enabled when redirecting stdout to a file.

Signed-off-by: Robin Jarry <robin@jarry.cc>
Acked-by: Moritz Poldrack <moritz@poldrack.dev>
2022-07-23 22:52:15 +02:00

58 lines
1.3 KiB
Go

package config
import (
"errors"
"fmt"
"github.com/google/shlex"
"git.sr.ht/~rjarry/aerc/lib/format"
"git.sr.ht/~rjarry/aerc/logging"
"git.sr.ht/~rjarry/aerc/models"
)
func (trig *TriggersConfig) ExecTrigger(triggerCmd string,
triggerFmt func(string) (string, error)) error {
if len(triggerCmd) == 0 {
return errors.New("Trigger command empty")
}
triggerCmdParts, err := shlex.Split(triggerCmd)
if err != nil {
return err
}
var command []string
for _, part := range triggerCmdParts {
formattedPart, err := triggerFmt(part)
if err != nil {
return err
}
command = append(command, formattedPart)
}
return trig.ExecuteCommand(command)
}
func (trig *TriggersConfig) ExecNewEmail(account *AccountConfig,
conf *AercConfig, msg *models.MessageInfo) {
err := trig.ExecTrigger(trig.NewEmail,
func(part string) (string, error) {
formatstr, args, err := format.ParseMessageFormat(
part, conf.Ui.TimestampFormat,
conf.Ui.ThisDayTimeFormat,
conf.Ui.ThisWeekTimeFormat,
conf.Ui.ThisYearTimeFormat,
format.Ctx{
FromAddress: account.From,
AccountName: account.Name,
MsgInfo: msg},
)
if err != nil {
return "", err
}
return fmt.Sprintf(formatstr, args...), nil
})
if err != nil {
logging.Errorf("failed to run new-email trigger: %v", err)
}
}