-
Notifications
You must be signed in to change notification settings - Fork 3
/
example.go
73 lines (63 loc) · 1.56 KB
/
example.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
package main
import (
"fmt"
"net/mail"
"os"
"github.com/adrianduke/configr"
"github.com/adrianduke/configr/sources"
)
const (
EmailFromAddressKey = "email.fromAddress"
EmailSubjectKey = "email.subject"
EmailRetryOnFailKey = "email.retryOnFail"
EmailMaxRetriesKey = "email.maxRetries"
)
type Email struct {
FromAddress string
Subject string
MaxRetries int // Note env vars are string
RetryOnFail bool // Note env vars are string
}
func init() {
configr.RequireKey(EmailFromAddressKey, "Email from address", func(v interface{}) error {
_, err := mail.ParseAddress(v.(string))
return err
})
configr.RequireKey(EmailSubjectKey, "Email subject")
configr.RegisterKey(EmailRetryOnFailKey, "Retry sending email if it fails", false)
configr.RegisterKey(EmailMaxRetriesKey, "How many times to retry email resending", 3)
}
func main() {
configr.AddSource(sources.NewEnvVars("configr"))
if err := configr.Parse(); err != nil {
fmt.Println(err)
os.Exit(1)
}
fromAddress, err := configr.String(EmailFromAddressKey)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
emailSubject, err := configr.String(EmailSubjectKey)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
retryOnFail, err := configr.Bool(EmailRetryOnFailKey)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
maxRetries, err := configr.Int(EmailMaxRetriesKey)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
email := Email{
FromAddress: fromAddress,
Subject: emailSubject,
MaxRetries: maxRetries,
RetryOnFail: retryOnFail,
}
fmt.Printf("Values:\n\t%#v\n\n", email)
}