diff options
Diffstat (limited to 'notif_config.go')
| -rw-r--r-- | notif_config.go | 56 |
1 files changed, 44 insertions, 12 deletions
diff --git a/notif_config.go b/notif_config.go index 7fa7d2d..c3ed43c 100644 --- a/notif_config.go +++ b/notif_config.go @@ -11,43 +11,75 @@ import ( const ( notifKindMattermost = `mattermost` + notifKindSMTP = `smtp` ) type NotifConfig struct { - webhookURL *url.URL - downTmpl *template.Template - upTmpl *template.Template + remoteURL *url.URL + + downTmpl *template.Template + upTmpl *template.Template + + Kind string `ini:"notif::kind"` + + // RemoteURL define the remote server to send notification. + // For mattermost this is the webhook URL. + // For smtp this is the domain or IP of SMTP server. + RemoteURL string `ini:"notif::remote_url"` + + // Channel that receive the mattermost message. + Channel string `ini:"notif::channel"` + + // User for authentication with SMTP server. + User string `ini:"notif::user"` + + // Password for authentication with SMTP server. + Password string `ini:"notif::password"` - Kind string `ini:"notif::kind"` - WebhookURL string `ini:"notif::webhook_url"` - Channel string `ini:"notif::channel"` DownTemplate string `ini:"notif::down_template"` UpTemplate string `ini:"notif::up_template"` + + // Recipient of email. + Recipient []string `ini:"notif::recipient"` } func (notifConfig *NotifConfig) init() (err error) { + var logp = `notifConfig.init` + switch notifConfig.Kind { case notifKindMattermost: - notifConfig.webhookURL, err = url.Parse(notifConfig.WebhookURL) + notifConfig.remoteURL, err = url.Parse(notifConfig.RemoteURL) if err != nil { - return fmt.Errorf(`invalid WebhookURL %q: %s`, - notifConfig.WebhookURL, err) + return fmt.Errorf(`invalid RemoteURL %q: %s`, + notifConfig.RemoteURL, err) + } + case notifKindSMTP: + if notifConfig.User == `` { + return fmt.Errorf(`%s: %s: empty user`, logp, notifConfig.Kind) + } + if notifConfig.Password == `` { + return fmt.Errorf(`%s: %s: empty password`, logp, notifConfig.Kind) + } + if len(notifConfig.Recipient) == 0 { + return fmt.Errorf(`%s: %s: empty recipient`, logp, notifConfig.Kind) } default: - return fmt.Errorf(`unknown notif kind: %s`, notifConfig.Kind) + return fmt.Errorf(`%s: unknown notif kind %q`, logp, notifConfig.Kind) } if notifConfig.DownTemplate != "" { notifConfig.downTmpl = template.New(`down`) notifConfig.downTmpl, err = notifConfig.downTmpl.Parse(notifConfig.DownTemplate) if err != nil { - return fmt.Errorf(`failed to parse down_template: %s`, err) + return fmt.Errorf(`%s: %s: failed to parse down_template: %s`, + logp, notifConfig.Kind, err) } } if notifConfig.UpTemplate != "" { notifConfig.upTmpl = template.New(`up`) notifConfig.upTmpl, err = notifConfig.upTmpl.Parse(notifConfig.UpTemplate) if err != nil { - return fmt.Errorf(`failed to parse up_template: %s`, err) + return fmt.Errorf(`%s: %s: failed to parse up_template: %s`, + logp, notifConfig.Kind, err) } } return nil |
