Add broken version

This commit is contained in:
Glenn Y. Rolland 2019-08-23 12:23:33 +02:00
parent 15b2e6e23f
commit 4f2a9a4f5b
7 changed files with 56 additions and 212 deletions

View file

@ -3,6 +3,7 @@ package main
import ( import (
// "errors" // "errors"
"fmt" "fmt"
// "github.com/davecgh/go-spew/spew"
"github.com/spf13/cobra" "github.com/spf13/cobra"
"github.com/spf13/viper" "github.com/spf13/viper"
"os" "os"
@ -50,14 +51,9 @@ func NewConfig() *Config {
cmd.PersistentFlags().StringVarP(&self.EmailFrom, "email-from", "", "", "address of sender") cmd.PersistentFlags().StringVarP(&self.EmailFrom, "email-from", "", "", "address of sender")
cmd.PersistentFlags().StringArrayVarP(&self.EmailTo, "email-to", "", []string{}, "address(es) of recipient(s)") cmd.PersistentFlags().StringArrayVarP(&self.EmailTo, "email-to", "", []string{}, "address(es) of recipient(s)")
cmd.PersistentFlags().StringVarP(&self.EmailSubject, "email-subject", "", "", "email subject") cmd.PersistentFlags().StringVarP(&self.EmailSubject, "email-subject", "", "", "email subject")
viper.BindPFlag("email-from", cmd.PersistentFlags().Lookup("email-from"))
viper.BindPFlag("email-to", cmd.PersistentFlags().Lookup("email-to"))
viper.BindPFlag("email-subject", cmd.PersistentFlags().Lookup("email-subject"))
cmd.PersistentFlags().StringVarP(&self.TrelloUrl, "trello-url", "", "", "url of trello board") cmd.PersistentFlags().StringVarP(&self.TrelloUrl, "trello-url", "", "", "url of trello board")
cmd.PersistentFlags().StringVarP(&self.TrelloToken, "trello-token", "", "", "url of trello token") cmd.PersistentFlags().StringVarP(&self.TrelloToken, "trello-token", "", "", "url of trello token")
viper.BindPFlag("trello-url", cmd.PersistentFlags().Lookup("trello-url"))
viper.BindPFlag("trello-token", cmd.PersistentFlags().Lookup("trello-token"))
cmd.PersistentFlags().StringVarP(&self.SmtpHostname, "smtp-hostname", "", "", "address of smtp server") cmd.PersistentFlags().StringVarP(&self.SmtpHostname, "smtp-hostname", "", "", "address of smtp server")
cmd.PersistentFlags().StringVarP(&self.SmtpUsername, "smtp-username", "", "", "username for smtp server") cmd.PersistentFlags().StringVarP(&self.SmtpUsername, "smtp-username", "", "", "username for smtp server")
@ -65,6 +61,12 @@ func NewConfig() *Config {
cmd.PersistentFlags().Uint16VarP(&self.SmtpPort, "smtp-port", "", 25, "port for smtp server") cmd.PersistentFlags().Uint16VarP(&self.SmtpPort, "smtp-port", "", 25, "port for smtp server")
cmd.PersistentFlags().StringVarP(&self.SmtpAuthType, "smtp-auth-type", "", "", "authentication type for smtp server") cmd.PersistentFlags().StringVarP(&self.SmtpAuthType, "smtp-auth-type", "", "", "authentication type for smtp server")
cmd.PersistentFlags().StringVarP(&self.SmtpSecurityType, "smtp-security-type", "", "", "security type for smtp server") cmd.PersistentFlags().StringVarP(&self.SmtpSecurityType, "smtp-security-type", "", "", "security type for smtp server")
viper.BindPFlag("email-from", cmd.PersistentFlags().Lookup("email-from"))
viper.BindPFlag("email-to", cmd.PersistentFlags().Lookup("email-to"))
viper.BindPFlag("email-subject", cmd.PersistentFlags().Lookup("email-subject"))
viper.BindPFlag("trello-url", cmd.PersistentFlags().Lookup("trello-url"))
viper.BindPFlag("trello-token", cmd.PersistentFlags().Lookup("trello-token"))
viper.BindPFlag("smtp-hostname", cmd.PersistentFlags().Lookup("smtp-hostname")) viper.BindPFlag("smtp-hostname", cmd.PersistentFlags().Lookup("smtp-hostname"))
viper.BindPFlag("smtp-username", cmd.PersistentFlags().Lookup("smtp-username")) viper.BindPFlag("smtp-username", cmd.PersistentFlags().Lookup("smtp-username"))
viper.BindPFlag("smtp-password", cmd.PersistentFlags().Lookup("smtp-password")) viper.BindPFlag("smtp-password", cmd.PersistentFlags().Lookup("smtp-password"))
@ -96,6 +98,6 @@ func (self *Config) Parse() error {
panic("Unable to unmarshal config") panic("Unable to unmarshal config")
} }
// spew.Dump(config) // spew.Dump(self)
return nil return nil
} }

View file

@ -1,58 +1,76 @@
package main package main
import ( import (
"crypto/tls"
"fmt" "fmt"
"github.com/go-mail/mail"
) )
func main() { func main() {
// Setup config // Setup config
fmt.Println("d: parsing config")
config := NewConfig() config := NewConfig()
config.Parse() config.Parse()
// Get task list as markdown // Get task list as markdown
fmt.Println("d: configuring trello")
trelloCtx := NewTrello(config.TrelloToken) trelloCtx := NewTrello(config.TrelloToken)
fmt.Println("d: getting trello boards")
var trelloBoardsList []TrelloBoard var trelloBoardsList []TrelloBoard
if len(config.TrelloUrl) > 0 { if len(config.TrelloUrl) > 0 {
fmt.Printf("d: using given url %s\n", config.TrelloUrl)
trelloBoard := trelloCtx.GetBoard(config.TrelloUrl) trelloBoard := trelloCtx.GetBoard(config.TrelloUrl)
trelloBoardsList = append(trelloBoardsList, trelloBoard) trelloBoardsList = append(trelloBoardsList, trelloBoard)
} else { } else {
fmt.Println("d: fetching boards")
trelloBoardsList = trelloCtx.GetBoards() trelloBoardsList = trelloCtx.GetBoards()
} }
for _, trelloBoard := range trelloBoardsList { for _, trelloBoard := range trelloBoardsList {
fmt.Printf("d: loading board %s\n", trelloBoard.Name)
if !trelloBoard.Starred || trelloBoard.Closed { if !trelloBoard.Starred || trelloBoard.Closed {
fmt.Println("d: skipping")
continue continue
} }
fmt.Printf("Loading board %s\n", trelloBoard.Name) fmt.Println("d: exporting content")
trelloMarkdown := trelloBoard.ExportToMarkdown() trelloMarkdown := trelloBoard.ExportToMarkdown()
trelloHtml := trelloBoard.ExportToHtml() trelloHtml := trelloBoard.ExportToHtml()
config.EmailSubject = fmt.Sprintf("Daily mail for %s", trelloBoard.Name) config.EmailSubject = fmt.Sprintf("Daily mail for %s", trelloBoard.Name)
// Create email enveloppe // Create email enveloppe
email := NewEmail() email := mail.NewMessage()
email.SetHeaders(EmailConfig{ email.SetHeader("To", config.EmailTo[0])
From: config.EmailFrom, if len(config.EmailTo) > 0 {
To: config.EmailTo, email.SetHeader("Cc", config.EmailTo[1:]...)
Subject: config.EmailSubject, }
}) email.SetHeader("From", config.EmailFrom)
email.SetHeader("Subject", config.EmailSubject)
email.SetBody(trelloHtml, trelloMarkdown) email.SetBody(trelloHtml, trelloMarkdown)
// Connect and send email // Connect and send email
transport := NewTransport(SmtpConfig{ var transport *mail.Dialer
Hostname: config.SmtpHostname, if len(config.SmtpUsername) > 0 {
Port: config.SmtpPort, fmt.Println("transport w/ username")
Username: config.SmtpUsername, transport = mail.NewDialer(
Password: config.SmtpPassword, config.SmtpHostname,
AuthType: config.SmtpAuthType, int(config.SmtpPort),
SecurityType: config.SmtpSecurityType, config.SmtpUsername,
}) config.SmtpPassword,
)
transport.Dial() // disable cert verification
transport.Authenticate() transport.TLSConfig = &tls.Config{InsecureSkipVerify: true}
transport.Send(email) } else {
transport.Quit() fmt.Println("transport w/ no username")
transport = &mail.Dialer{
Host: config.SmtpHostname,
Port: int(config.SmtpPort),
}
}
if err := transport.DialAndSend(email); err != nil {
panic(err)
}
} }
} }

View file

@ -1,41 +0,0 @@
// MIT license (c) andelf 2013
package main
import (
"errors"
"net/smtp"
)
type loginAuth struct {
username, password string
}
func LoginAuth(username, password string) smtp.Auth {
return &loginAuth{username, password}
}
func (a *loginAuth) Start(server *smtp.ServerInfo) (string, []byte, error) {
return "LOGIN", []byte(a.username), nil
}
func (a *loginAuth) Next(fromServer []byte, more bool) ([]byte, error) {
if more {
switch string(fromServer) {
case "Username:":
return []byte(a.username), nil
case "Password:":
return []byte(a.password), nil
default:
return nil, errors.New("Unkown fromServer")
}
}
return nil, nil
}
// usage:
// auth := LoginAuth("loginname", "password")
// err := smtp.SendMail(smtpServer + ":25", auth, fromAddress, toAddresses, []byte(message))
// or
// client, err := smtp.Dial(smtpServer)
// client.Auth(LoginAuth("loginname", "password"))

View file

@ -1,145 +0,0 @@
package main
import (
"bytes"
"crypto/tls"
"fmt"
"log"
"net/smtp"
)
type SmtpConfig struct {
Hostname string
Port uint16
Username string
Password string
AuthType string
SecurityType string
}
type TransportCtx struct {
Config SmtpConfig
Address string
Auth *smtp.Auth
Tls *tls.Config
Client *smtp.Client
}
func NewTransport(config SmtpConfig) *TransportCtx {
ctx := TransportCtx{}
ctx.Config = config
ctx.Address = fmt.Sprintf("%s:%d", config.Hostname, config.Port)
ctx.Auth = NewTransportAuth(config)
ctx.Tls = NewTransportTls(config)
return &ctx
}
func NewTransportAuth(config SmtpConfig) *smtp.Auth {
switch config.AuthType {
case "plain":
auth := smtp.PlainAuth(
"",
config.Username,
config.Password,
config.Hostname,
)
return &auth
case "login":
auth := LoginAuth(config.Username, config.Password)
return &auth
default:
return nil
}
}
func NewTransportTls(config SmtpConfig) *tls.Config {
// TLS config
return &tls.Config{
InsecureSkipVerify: true,
ServerName: config.Hostname,
}
}
func (ctx *TransportCtx) DialInsecure() {
// no SSL/TLS
c, err := smtp.Dial(ctx.Address)
if err != nil {
log.Panic(err)
}
ctx.Client = c
}
func (ctx *TransportCtx) DialTls() {
conn, err := tls.Dial("tcp", ctx.Address, ctx.Tls)
if err != nil {
log.Panic(err)
}
c, err := smtp.NewClient(conn, ctx.Config.Hostname)
if err != nil {
log.Panic(err)
}
ctx.Client = c
}
func (ctx *TransportCtx) DialStartTls() {
c, err := smtp.Dial(ctx.Address)
if err != nil {
log.Panic(err)
}
c.StartTLS(ctx.Tls)
ctx.Client = c
}
func (ctx *TransportCtx) Dial() {
switch ctx.Config.SecurityType {
case "tls":
ctx.DialTls()
case "starttls":
ctx.DialStartTls()
default:
ctx.DialInsecure()
}
}
func (ctx *TransportCtx) Authenticate() {
// guard
if ctx.Auth == nil {
return
}
// ok, we have to
err := ctx.Client.Auth(*ctx.Auth)
if err != nil {
log.Panic(err)
}
}
func (ctx *TransportCtx) Quit() {
ctx.Client.Quit()
}
func (ctx *TransportCtx) Send(email *EmailCtx) {
// Set email header
ctx.Client.Mail(email.Headers["From"])
ctx.Client.Rcpt(email.Headers["To"])
// Set email body
wc, err := ctx.Client.Data()
if err != nil {
log.Panic(err)
}
defer wc.Close()
buffer := bytes.NewBufferString(email.String())
if _, err = buffer.WriteTo(wc); err != nil {
log.Panic(err)
}
return
}

View file

@ -1,9 +1,12 @@
package main package main
import "github.com/davecgh/go-spew/spew"
import ( import (
"bytes" "bytes"
"fmt" "fmt"
// "github.com/adlio/trello" // "github.com/adlio/trello"
trello "github.com/glenux/contrib-trello" trello "github.com/glenux/contrib-trello"
"github.com/russross/blackfriday/v2" "github.com/russross/blackfriday/v2"
"log" "log"
@ -118,6 +121,9 @@ func (ctx *TrelloCtx) GetBoard(boardUrl string) TrelloBoard {
if err != nil { if err != nil {
log.Panic(err) log.Panic(err)
} }
// fmt.Printf("%#v\n", board)
spew.Dump(board)
return TrelloBoard{ return TrelloBoard{
Ctx: ctx, Ctx: ctx,
Starred: board.Starred, Starred: board.Starred,

2
go.mod
View file

@ -2,7 +2,9 @@ module github.com/glenux/trello2mail
require ( require (
github.com/BurntSushi/toml v0.3.1 // indirect github.com/BurntSushi/toml v0.3.1 // indirect
github.com/davecgh/go-spew v1.1.1
github.com/glenux/contrib-trello v0.0.0-20181123120736-c8291e6a707a github.com/glenux/contrib-trello v0.0.0-20181123120736-c8291e6a707a
github.com/go-mail/mail v2.3.1+incompatible
github.com/inconshreveable/mousetrap v1.0.0 // indirect github.com/inconshreveable/mousetrap v1.0.0 // indirect
github.com/pkg/errors v0.8.0 // indirect github.com/pkg/errors v0.8.0 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect github.com/pmezard/go-difflib v1.0.0 // indirect

2
go.sum
View file

@ -8,6 +8,8 @@ github.com/glenux/contrib-trello v0.0.0-20181009211147-e4cc07c871d0 h1:u6tAr2/SS
github.com/glenux/contrib-trello v0.0.0-20181009211147-e4cc07c871d0/go.mod h1:A69TOILEJYbyfZyLw5gfn5E8hQPzXuBG2Oh36nutIFA= github.com/glenux/contrib-trello v0.0.0-20181009211147-e4cc07c871d0/go.mod h1:A69TOILEJYbyfZyLw5gfn5E8hQPzXuBG2Oh36nutIFA=
github.com/glenux/contrib-trello v0.0.0-20181123120736-c8291e6a707a h1:Oie4PAFrDkFI+B19wC7jzI87QiFrX/O6EBNiIsp4n0E= github.com/glenux/contrib-trello v0.0.0-20181123120736-c8291e6a707a h1:Oie4PAFrDkFI+B19wC7jzI87QiFrX/O6EBNiIsp4n0E=
github.com/glenux/contrib-trello v0.0.0-20181123120736-c8291e6a707a/go.mod h1:A69TOILEJYbyfZyLw5gfn5E8hQPzXuBG2Oh36nutIFA= github.com/glenux/contrib-trello v0.0.0-20181123120736-c8291e6a707a/go.mod h1:A69TOILEJYbyfZyLw5gfn5E8hQPzXuBG2Oh36nutIFA=
github.com/go-mail/mail v2.3.1+incompatible h1:UzNOn0k5lpfVtO31cK3hn6I4VEVGhe3lX8AJBAxXExM=
github.com/go-mail/mail v2.3.1+incompatible/go.mod h1:VPWjmmNyRsWXQZHVHT3g0YbIINUkSmuKOiLIDkWbL6M=
github.com/hashicorp/hcl v1.0.0 h1:0Anlzjpi4vEasTeNFn2mLJgTSwt0+6sfsiTG8qcWGx4= github.com/hashicorp/hcl v1.0.0 h1:0Anlzjpi4vEasTeNFn2mLJgTSwt0+6sfsiTG8qcWGx4=
github.com/hashicorp/hcl v1.0.0/go.mod h1:E5yfLk+7swimpb2L/Alb/PJmXilQ/rhwaUYs4T20WEQ= github.com/hashicorp/hcl v1.0.0/go.mod h1:E5yfLk+7swimpb2L/Alb/PJmXilQ/rhwaUYs4T20WEQ=
github.com/inconshreveable/mousetrap v1.0.0 h1:Z8tu5sraLXCXIcARxBp/8cbvlwVa7Z1NHg9XEKhtSvM= github.com/inconshreveable/mousetrap v1.0.0 h1:Z8tu5sraLXCXIcARxBp/8cbvlwVa7Z1NHg9XEKhtSvM=