11package plugins
22
33import (
4- "bytes"
54 "github.com/reddec/container"
6- "time"
75 "log"
86 "os"
97 "net/smtp"
@@ -13,38 +11,24 @@ import (
1311)
1412
1513type Email struct {
16- Smtp string `yaml:"smtp"`
17- From string `yaml:"from"`
18- Password string `yaml:"password"`
19- To []string `yaml:"to"`
20- Template string `yaml:"template"`
21- TemplateFile string `yaml:"templateFile"` // template file (relative to config dir) has priority. Template supports basic utils
22- Services []string `yaml:"services"`
14+ Smtp string `yaml:"smtp"`
15+ From string `yaml:"from"`
16+ Password string `yaml:"password"`
17+ To []string `yaml:"to"`
18+ Services []string `yaml:"services"`
19+ withTemplate `mapstructure:",squash" yaml:",inline"`
2320
2421 log * log.Logger
2522 hostname string
2623 servicesSet map [string ]bool
2724 workDir string
2825}
2926
30- func (c * Email ) renderAndSend (params map [string ]interface {}) {
31- message := & bytes.Buffer {}
32-
33- parser , err := parseFileOrTemplate (c .TemplateFile , c .Template , c .log )
34- if err != nil {
35- c .log .Println ("failed parse template:" , err )
36- return
37- }
38- renderErr := parser .Execute (message , params )
39- if renderErr != nil {
40- c .log .Println ("failed render:" , renderErr , "; params:" , params )
41- return
42- }
43-
44- c .log .Println (message .String ())
27+ func (c * Email ) renderAndSend (message string ) {
28+ c .log .Println (message )
4529 host , _ , _ := net .SplitHostPort (c .Smtp )
4630 auth := smtp .PlainAuth ("" , c .From , c .Password , host )
47- err = smtp .SendMail (c .Smtp , auth , c .From , c .To , message . Bytes ( ))
31+ err : = smtp .SendMail (c .Smtp , auth , c .From , c .To , [] byte ( message ))
4832 if err != nil {
4933 c .log .Println ("failed send mail:" , err )
5034 } else {
@@ -54,14 +38,12 @@ func (c *Email) renderAndSend(params map[string]interface{}) {
5438
5539func (c * Email ) Spawned (runnable container.Runnable , id container.ID ) {
5640 if c .servicesSet [runnable .Label ()] {
57- params := map [string ]interface {}{
58- "action" : "spawned" ,
59- "id" : id ,
60- "label" : runnable .Label (),
61- "hostname" : c .hostname ,
62- "time" : time .Now ().String (),
41+ content , renderErr := c .renderDefault ("spawned" , string (id ), runnable .Label (), nil , c .log )
42+ if renderErr != nil {
43+ c .log .Println ("failed render:" , renderErr )
44+ } else {
45+ c .renderAndSend (content )
6346 }
64- c .renderAndSend (params )
6547 }
6648}
6749
@@ -74,15 +56,12 @@ func (c *Email) Prepare() error {
7456
7557func (c * Email ) Stopped (runnable container.Runnable , id container.ID , err error ) {
7658 if c .servicesSet [runnable .Label ()] {
77- params := map [string ]interface {}{
78- "action" : "stopped" ,
79- "id" : id ,
80- "error" : err ,
81- "label" : runnable .Label (),
82- "hostname" : c .hostname ,
83- "time" : time .Now ().String (),
59+ content , renderErr := c .renderDefault ("stopped" , string (id ), runnable .Label (), err , c .log )
60+ if renderErr != nil {
61+ c .log .Println ("failed render:" , renderErr )
62+ } else {
63+ c .renderAndSend (content )
8464 }
85- c .renderAndSend (params )
8665 }
8766}
8867
0 commit comments