musala/cmd/trello2mail/main.go

77 lines
1.9 KiB
Go
Raw Normal View History

2018-08-21 08:36:54 +00:00
package main
import (
2019-08-23 10:23:33 +00:00
"crypto/tls"
2018-08-22 16:57:50 +00:00
"fmt"
2019-08-23 10:23:33 +00:00
"github.com/go-mail/mail"
2018-08-21 08:36:54 +00:00
)
func main() {
// Setup config
2019-08-23 10:23:33 +00:00
fmt.Println("d: parsing config")
2018-08-21 08:36:54 +00:00
config := NewConfig()
config.Parse()
2018-08-21 08:36:54 +00:00
// Get task list as markdown
2019-08-23 10:23:33 +00:00
fmt.Println("d: configuring trello")
trelloCtx := NewTrello(config.TrelloToken)
2019-08-23 10:23:33 +00:00
fmt.Println("d: getting trello boards")
var trelloBoardsList []TrelloBoard
if len(config.TrelloUrl) > 0 {
2019-08-23 10:23:33 +00:00
fmt.Printf("d: using given url %s\n", config.TrelloUrl)
trelloBoard := trelloCtx.GetBoard(config.TrelloUrl)
trelloBoardsList = append(trelloBoardsList, trelloBoard)
} else {
2019-08-23 10:23:33 +00:00
fmt.Println("d: fetching boards")
trelloBoardsList = trelloCtx.GetBoards()
}
for _, trelloBoard := range trelloBoardsList {
2019-08-23 10:23:33 +00:00
fmt.Printf("d: loading board %s\n", trelloBoard.Name)
2018-11-23 12:22:03 +00:00
if !trelloBoard.Starred || trelloBoard.Closed {
2019-08-23 10:23:33 +00:00
fmt.Println("d: skipping")
2018-11-23 12:22:03 +00:00
continue
}
2019-08-23 10:23:33 +00:00
fmt.Println("d: exporting content")
2018-11-23 12:22:03 +00:00
trelloMarkdown := trelloBoard.ExportToMarkdown()
trelloHtml := trelloBoard.ExportToHtml()
config.EmailSubject = fmt.Sprintf("Daily mail for %s", trelloBoard.Name)
// Create email enveloppe
2019-08-23 10:23:33 +00:00
email := mail.NewMessage()
email.SetHeader("To", config.EmailTo[0])
if len(config.EmailTo) > 0 {
email.SetHeader("Cc", config.EmailTo[1:]...)
}
email.SetHeader("From", config.EmailFrom)
email.SetHeader("Subject", config.EmailSubject)
2018-11-23 12:22:03 +00:00
email.SetBody(trelloHtml, trelloMarkdown)
// Connect and send email
2019-08-23 10:23:33 +00:00
var transport *mail.Dialer
if len(config.SmtpUsername) > 0 {
fmt.Println("transport w/ username")
transport = mail.NewDialer(
config.SmtpHostname,
int(config.SmtpPort),
config.SmtpUsername,
config.SmtpPassword,
)
// disable cert verification
transport.TLSConfig = &tls.Config{InsecureSkipVerify: true}
} else {
fmt.Println("transport w/ no username")
transport = &mail.Dialer{
Host: config.SmtpHostname,
Port: int(config.SmtpPort),
}
}
2018-11-23 12:22:03 +00:00
2019-08-23 10:23:33 +00:00
if err := transport.DialAndSend(email); err != nil {
panic(err)
}
2018-11-23 12:22:03 +00:00
}
2018-08-21 08:36:54 +00:00
}