diff --git a/cmd/trello2mail/config.go b/cmd/trello2mail/config.go index bb5ffca..d09ecb5 100644 --- a/cmd/trello2mail/config.go +++ b/cmd/trello2mail/config.go @@ -17,18 +17,29 @@ type ConfigEntry struct { Values []string } +type TrelloConfig struct { + Url string + Token string +} + +type SmtpConfig struct { + Hostname string + Port uint16 + Username string + Password string + AuthType string + SecurityType string +} + +type EmailConfig struct { + From string + To string + Subject string +} type Config struct { - EmailFrom string - EmailTo string - EmailSubject string - SmtpHostname string - SmtpPort uint16 - SmtpUsername string - SmtpPassword string - SmtpAuthType string - SmtpSecurityType string - TrelloUrl string - TrelloToken string + Email EmailConfig + Smtp SmtpConfig + Trello TrelloConfig } func NewConfig() *Config { @@ -38,19 +49,19 @@ func NewConfig() *Config { func (config *Config) ParseEnv() (int, error) { // map env variables to config pointers dataMap := map[string](ConfigEntry){ - "EMAIL_FROM": ConfigEntry{"string", &(config.EmailFrom), nil}, - "EMAIL_TO": ConfigEntry{"string", &(config.EmailTo), nil}, - "EMAIL_SUBJECT": ConfigEntry{"string", &(config.EmailSubject), nil}, - "TRELLO_URL": ConfigEntry{"string", &(config.TrelloUrl), nil}, - "TRELLO_TOKEN": ConfigEntry{"string", &(config.TrelloToken), nil}, + "EMAIL_FROM": ConfigEntry{"string", &(config.Email.From), nil}, + "EMAIL_TO": ConfigEntry{"string", &(config.Email.To), nil}, + "EMAIL_SUBJECT": ConfigEntry{"string", &(config.Email.Subject), nil}, + "TRELLO_URL": ConfigEntry{"string", &(config.Trello.Url), nil}, + "TRELLO_TOKEN": ConfigEntry{"string", &(config.Trello.Token), nil}, - "SMTP_HOSTNAME": ConfigEntry{"string", &(config.SmtpHostname), nil}, - "SMTP_USERNAME": ConfigEntry{"string", &(config.SmtpUsername), nil}, - "SMTP_PASSWORD": ConfigEntry{"string", &(config.SmtpPassword), nil}, - "SMTP_PORT": ConfigEntry{"uint16", &(config.SmtpPort), nil}, + "SMTP_HOSTNAME": ConfigEntry{"string", &(config.Smtp.Hostname), nil}, + "SMTP_USERNAME": ConfigEntry{"string", &(config.Smtp.Username), nil}, + "SMTP_PASSWORD": ConfigEntry{"string", &(config.Smtp.Password), nil}, + "SMTP_PORT": ConfigEntry{"uint16", &(config.Smtp.Port), nil}, - "SMTP_AUTH_TYPE": ConfigEntry{"string", &(config.SmtpAuthType), []string{"none", "plain", "login"}}, - "SMTP_SECURITY_TYPE": ConfigEntry{"string", &(config.SmtpSecurityType), []string{"none", "tls", "starttls"}}, + "SMTP_AUTH_TYPE": ConfigEntry{"string", &(config.Smtp.AuthType), []string{"none", "plain", "login"}}, + "SMTP_SECURITY_TYPE": ConfigEntry{"string", &(config.Smtp.SecurityType), []string{"none", "tls", "starttls"}}, } for envVar, mapEntry := range dataMap { diff --git a/cmd/trello2mail/email.go b/cmd/trello2mail/email.go new file mode 100644 index 0000000..7d6d2e0 --- /dev/null +++ b/cmd/trello2mail/email.go @@ -0,0 +1,40 @@ +package main + +import ( + "crypto/tls" + // "errors" + "fmt" + "log" + // "os" + // "strconv" + // "net" + // "net/mail" + "net/smtp" +) + +type MailHeaders map[string]string +type MailBody []string + +type EmailCtx struct { + Headers MailHeaders + Body MailBody +} + +func NewEmail() *EmailCtx { + return &EmailCtx{} +} + +func (email *EmailCtx) MakeHeaders(config EmailConfig) (int, error) { + email.Headers["From"] = config.From + email.Headers["To"] = config.To + email.Headers["Subject"] = config.Subject + return 0, nil +} + +func (email *EmailCtx) MakeBody(content []string) (int, error) { + email.Body = content + return 0, nil +} + +func (email *EmailCtx) Send() { +} diff --git a/cmd/trello2mail/mail.go b/cmd/trello2mail/mail.go deleted file mode 100644 index 9b3ad44..0000000 --- a/cmd/trello2mail/mail.go +++ /dev/null @@ -1,91 +0,0 @@ -package main - -import ( - "crypto/tls" - // "errors" - "fmt" - "log" - // "os" - // "strconv" - // "net" - // "net/mail" - "net/smtp" -) - -type MailHeaders map[string]string - -func (headers *MailHeaders) ParseConfig(config Config) (int, error) { - (*headers)["From"] = config.EmailFrom - (*headers)["To"] = config.EmailTo - (*headers)["Subject"] = config.EmailSubject - return 0, nil -} - -func NewAuth(config Config) *smtp.Auth { - - switch config.SmtpAuthType { - case "plain": - auth := smtp.PlainAuth( - "", - config.SmtpUsername, - config.SmtpPassword, - config.SmtpHostname, - ) - return &auth - - case "login": - auth := LoginAuth(config.SmtpUsername, config.SmtpPassword) - return &auth - - default: - } - return nil -} - -func NewTLS(config Config) *tls.Config { - // TLS config - return &tls.Config{ - InsecureSkipVerify: true, - ServerName: config.SmtpHostname, - } -} - -func NewSmtpClient(config Config) *smtp.Client { - address := fmt.Sprintf("%s:%d", config.SmtpHostname, config.SmtpPort) - tlsConfig := NewTLS(config) - switch config.SmtpSecurityType { - case "tls": - fmt.Printf("Creating TLS connection to %s...\n", address) - conn, err := tls.Dial("tcp", address, tlsConfig) - if err != nil { - log.Panic(err) - } - - fmt.Println("Creating SMTP client...") - c, err := smtp.NewClient(conn, config.SmtpHostname) - if err != nil { - log.Panic(err) - } - return c - - case "starttls": - fmt.Println("Creating SMTP client...") - c, err := smtp.Dial(address) - if err != nil { - log.Panic(err) - } - fmt.Printf("Creating StartTLS connection to %s...\n", address) - c.StartTLS(tlsConfig) - - return c - - default: - // no SSL/TLS - fmt.Println("Creating SMTP client...") - c, err := smtp.Dial(address) - if err != nil { - log.Panic(err) - } - return c - } -} diff --git a/cmd/trello2mail/main.go b/cmd/trello2mail/main.go index 4f227bc..e8bd659 100644 --- a/cmd/trello2mail/main.go +++ b/cmd/trello2mail/main.go @@ -34,11 +34,23 @@ func main() { fmt.Fprintf(os.Stderr, "Error: %s\n", err.Error()) os.Exit(1) } - fmt.Printf("%#v\n", config) - // Build headers - headers := make(MailHeaders) - headers.ParseConfig(*config) + // Get task list as markdown + trelloCtx := NewTrello(config.Trello.Token) + trelloBoard := trelloCtx.GetBoard(config.Trello.Url) + trelloMarkdown := trelloBoard.ExportToMarkdown() + + // Create and send email + email := NewEmail() + email.MakeHeaders(config.Email) + email.MakeBody(trelloMarkdown) + email.Send() + + transport := NewTransport(config.Smtp) + if err := transport.Authenticate() { + log.Panic(err) + } + transport.Send(email) // Connect & authenticate fmt.Println("Connecting...") @@ -49,14 +61,8 @@ func main() { fmt.Printf("Authenticating...\n") if err := client.Auth(*authConfig); err != nil { - log.Panic(err) - } fmt.Println("Disconnecting...") client.Quit() // Write email - // mdTasklist := ImportFromTrello(config) - // htmlTasklist := ConvertMarkdown(markdown) - // BuildEmail(config, htmlTasklist) - } diff --git a/cmd/trello2mail/transport.go b/cmd/trello2mail/transport.go new file mode 100644 index 0000000..1457f80 --- /dev/null +++ b/cmd/trello2mail/transport.go @@ -0,0 +1,97 @@ +package main + +import ( + "fmt" +) + +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 (*TransportCtx) DialInsecure() error { +} + +func (*TransportCtx) DialTls() error { +} + +func (*TransportCtx) DialStartTls(address) error { +} + +func (*TransportCtx) Dial() *smtp.Client { + switch config.SecurityType { + case "tls": + fmt.Printf("Creating TLS connection to %s...\n", address) + conn, err := tls.Dial("tcp", address, tlsConfig) + if err != nil { + log.Panic(err) + } + + fmt.Println("Creating SMTP client...") + c, err := smtp.NewClient(conn, config.Hostname) + if err != nil { + log.Panic(err) + } + return c + + case "starttls": + fmt.Println("Creating SMTP client...") + c, err := smtp.Dial(address) + if err != nil { + log.Panic(err) + } + fmt.Printf("Creating StartTLS connection to %s...\n", address) + c.StartTLS(tlsConfig) + + return c + + default: + // no SSL/TLS + fmt.Println("Creating SMTP client...") + c, err := smtp.Dial(address) + if err != nil { + log.Panic(err) + } + return c + } +} diff --git a/cmd/trello2mail/import.go b/cmd/trello2mail/trello.go similarity index 51% rename from cmd/trello2mail/import.go rename to cmd/trello2mail/trello.go index 378c9aa..e202b1c 100644 --- a/cmd/trello2mail/import.go +++ b/cmd/trello2mail/trello.go @@ -6,10 +6,28 @@ package main import ( "fmt" + //"github.com/VojtechVitek/go-trello" "os/exec" "strings" ) +type TrelloCtx struct { + Token string +} + +type TrelloItem struct { + Title string +} + +type TrelloList struct { + Items []TrelloItem +} + +type TrelloBoard struct { + Ctx TrelloCtx + Lists []TrelloList +} + func runcmd(command string) string { shell := "/bin/sh" flag := "-c" @@ -22,7 +40,15 @@ func runcmd(command string) string { return string(out) } -func CreateTaskell() { +func NewTrello(token string) *TrelloCtx { + return &TrelloCtx{Token: token} +} + +func (ctx *TrelloCtx) GetBoard(boardUrl string) TrelloBoard { + return TrelloBoard{Ctx: *ctx} +} + +func (*TrelloBoard) ExportToMarkdown() []string { } func RunTaskell(boardUrl string) {