Some restructuring of code, no functional change

This commit is contained in:
Thomas Boerger 2015-12-11 09:46:36 +01:00
parent 40520edfdb
commit 9d5c1e3333
4 changed files with 81 additions and 55 deletions

58
main.go
View file

@ -8,60 +8,46 @@ import (
"github.com/drone/drone-go/plugin"
)
type Email struct {
Recipients []string `json:"recipients"`
Host string `json:"host"`
Port int `json:"port"`
From string `json:"from"`
Username string `json:"username"`
Password string `json:"password"`
}
type Context struct {
Email Email
Build drone.Build
Repo drone.Repo
System drone.System
}
var (
build string
buildDate string
)
func main() {
fmt.Printf("Drone Email Plugin built at %s\n", buildDate)
system := drone.System{}
repo := drone.Repo{}
build := drone.Build{}
system := drone.System{}
email := Email{}
vargs := Params{}
plugin.Param("system", &system)
plugin.Param("build", &build)
plugin.Param("repo", &repo)
plugin.Param("vargs", &email)
err := plugin.Parse()
if err != nil {
fmt.Println(err)
os.Exit(1)
}
plugin.Param("build", &build)
plugin.Param("vargs", &vargs)
plugin.MustParse()
// default to the commit email address
// if no email recipient list is provided
if len(email.Recipients) == 0 {
email.Recipients = []string{
if len(vargs.Recipients) == 0 {
vargs.Recipients = []string{
build.Email,
}
}
// default smtp port
if email.Port == 0 {
email.Port = 587
if vargs.Port == 0 {
vargs.Port = 587
}
err = Send(&Context{
Email: email,
Build: build,
Repo: repo,
err := Send(&Context{
System: system,
Repo: repo,
Build: build,
Vargs: vargs,
})
if err != nil {
fmt.Println(err)
os.Exit(1)
return
}
}

View file

@ -27,10 +27,10 @@ func Send(context *Context) error {
// SendFailure sends email notifications to the list of
// recipients indicating the build failed.
func SendFailure(context *Context) error {
// generate the email failure template
var buf bytes.Buffer
err := failureTemplate.ExecuteTemplate(&buf, "_", context)
if err != nil {
return err
}
@ -51,10 +51,10 @@ func SendFailure(context *Context) error {
// SendSuccess sends email notifications to the list of
// recipients indicating the build was a success.
func SendSuccess(context *Context) error {
// generate the email success template
var buf bytes.Buffer
err := successTemplate.ExecuteTemplate(&buf, "_", context)
if err != nil {
return err
}
@ -73,25 +73,44 @@ func SendSuccess(context *Context) error {
}
func send(subject, body string, c *Context) error {
if len(c.Email.Recipients) == 0 {
c.Email.Recipients = []string{
if len(c.Vargs.Recipients) == 0 {
c.Vargs.Recipients = []string{
c.Build.Email,
}
}
var auth smtp.Auth
var addr = net.JoinHostPort(c.Email.Host, strconv.Itoa(c.Email.Port))
var addr = net.JoinHostPort(
c.Vargs.Host,
strconv.Itoa(c.Vargs.Port))
// setup the authentication to the smtp server
// if the username and password are provided.
if len(c.Email.Username) > 0 {
auth = smtp.PlainAuth("", c.Email.Username, c.Email.Password, c.Email.Host)
if len(c.Vargs.Username) > 0 {
auth = smtp.PlainAuth(
"",
c.Vargs.Username,
c.Vargs.Password,
c.Vargs.Host)
}
// genereate the raw email message
var to = strings.Join(c.Email.Recipients, ",")
var raw = fmt.Sprintf(rawMessage, c.Email.From, to, subject, body)
var to = strings.Join(
c.Vargs.Recipients,
",")
return smtp.SendMail(addr, auth, c.Email.From, c.Email.Recipients, []byte(raw))
var raw = fmt.Sprintf(
rawMessage,
c.Vargs.From,
to,
subject,
body)
return smtp.SendMail(
addr,
auth,
c.Vargs.From,
c.Vargs.Recipients,
[]byte(raw))
}

View file

@ -18,10 +18,10 @@ var successTemplate = template.Must(template.New("_").Parse(`
<b>Build was Successful</b>
(<a href="{{.System.Link}}/{{.Repo.Owner}}/{{.Repo.Name}}/{{.Build.Number}}">see results</a>)
</p>
<p>Repository : {{.Repo.Owner}}/{{.Repo.Name}}</p>
<p>Commit : {{.Build.Commit}}</p>
<p>Author : {{.Build.Author}}</p>
<p>Branch : {{.Build.Branch}}</p>
<p>Repository: {{.Repo.Owner}}/{{.Repo.Name}}</p>
<p>Commit: {{.Build.Commit}}</p>
<p>Author: {{.Build.Author}}</p>
<p>Branch: {{.Build.Branch}}</p>
<p>Message:</p>
<p>{{ .Build.Message }}</p>
`))
@ -32,10 +32,10 @@ var failureTemplate = template.Must(template.New("_").Parse(`
<b>Build Failed</b>
(<a href="{{.System.Link}}/{{.Repo.Owner}}/{{.Repo.Name}}/{{.Build.Number}}">see results</a>)
</p>
<p>Repository : {{.Repo.Owner}}/{{.Repo.Name}}</p>
<p>Commit : {{.Build.Commit}}</p>
<p>Author : {{.Build.Author}}</p>
<p>Branch : {{.Build.Branch}}</p>
<p>Repository: {{.Repo.Owner}}/{{.Repo.Name}}</p>
<p>Commit: {{.Build.Commit}}</p>
<p>Author: {{.Build.Author}}</p>
<p>Branch: {{.Build.Branch}}</p>
<p>Message:</p>
<p>{{ .Build.Message }}</p>
`))

21
types.go Normal file
View file

@ -0,0 +1,21 @@
package main
import (
"github.com/drone/drone-go/drone"
)
type Params struct {
Recipients []string `json:"recipients"`
Host string `json:"host"`
Port int `json:"port"`
From string `json:"from"`
Username string `json:"username"`
Password string `json:"password"`
}
type Context struct {
System drone.System
Repo drone.Repo
Build drone.Build
Vargs Params
}