-
Notifications
You must be signed in to change notification settings - Fork 0
/
env_notif.go
56 lines (47 loc) · 1.35 KB
/
env_notif.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
// SPDX-FileCopyrightText: 2023 M. Shulhan <[email protected]>
// SPDX-License-Identifier: GPL-3.0-or-later
package karajo
import (
"fmt"
"log"
"os"
)
const (
notifKindEmail = `email`
)
// EnvNotif environment for notification.
type EnvNotif struct {
Name string
Kind string `ini:"::kind"`
SMTPServer string `ini:"::smtp_server"`
SMTPUser string `ini:"::smtp_user"`
SMTPPassword string `ini:"::smtp_password"`
From string `ini:"::from"`
To []string `ini:"::to"`
SMTPInsecure bool `ini:"::smtp_insecure"`
}
// init initialize the envNotif.
func (envNotif *EnvNotif) init() {
if envNotif.SMTPUser[0] == '$' {
envNotif.SMTPUser = os.Getenv(envNotif.SMTPUser)
}
if envNotif.SMTPPassword[0] == '$' {
envNotif.SMTPPassword = os.Getenv(envNotif.SMTPPassword)
}
}
// createClient create client for notification based on its kind.
// It will return an error if kind is unknown or the client failed to created.
func (envNotif *EnvNotif) createClient() (cl notifClient, err error) {
var logp = `createClient`
switch envNotif.Kind {
case notifKindEmail:
cl, err = newClientSMTP(*envNotif)
default:
err = fmt.Errorf(`unknown kind %q`, envNotif.Kind)
}
if err != nil {
return nil, fmt.Errorf(`%s: %s: %w`, logp, envNotif.Name, err)
}
log.Printf(`notif %q: connected`, envNotif.Name)
return cl, nil
}