(broken) Split into services w/ contexts

This commit is contained in:
Glenn Y. Rolland 2018-08-21 19:21:46 +02:00
parent 8cea2f2f9d
commit 4be45535b4
6 changed files with 213 additions and 124 deletions

View file

@ -17,18 +17,29 @@ type ConfigEntry struct {
Values []string 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 { type Config struct {
EmailFrom string Email EmailConfig
EmailTo string Smtp SmtpConfig
EmailSubject string Trello TrelloConfig
SmtpHostname string
SmtpPort uint16
SmtpUsername string
SmtpPassword string
SmtpAuthType string
SmtpSecurityType string
TrelloUrl string
TrelloToken string
} }
func NewConfig() *Config { func NewConfig() *Config {
@ -38,19 +49,19 @@ func NewConfig() *Config {
func (config *Config) ParseEnv() (int, error) { func (config *Config) ParseEnv() (int, error) {
// map env variables to config pointers // map env variables to config pointers
dataMap := map[string](ConfigEntry){ dataMap := map[string](ConfigEntry){
"EMAIL_FROM": ConfigEntry{"string", &(config.EmailFrom), nil}, "EMAIL_FROM": ConfigEntry{"string", &(config.Email.From), nil},
"EMAIL_TO": ConfigEntry{"string", &(config.EmailTo), nil}, "EMAIL_TO": ConfigEntry{"string", &(config.Email.To), nil},
"EMAIL_SUBJECT": ConfigEntry{"string", &(config.EmailSubject), nil}, "EMAIL_SUBJECT": ConfigEntry{"string", &(config.Email.Subject), nil},
"TRELLO_URL": ConfigEntry{"string", &(config.TrelloUrl), nil}, "TRELLO_URL": ConfigEntry{"string", &(config.Trello.Url), nil},
"TRELLO_TOKEN": ConfigEntry{"string", &(config.TrelloToken), nil}, "TRELLO_TOKEN": ConfigEntry{"string", &(config.Trello.Token), nil},
"SMTP_HOSTNAME": ConfigEntry{"string", &(config.SmtpHostname), nil}, "SMTP_HOSTNAME": ConfigEntry{"string", &(config.Smtp.Hostname), nil},
"SMTP_USERNAME": ConfigEntry{"string", &(config.SmtpUsername), nil}, "SMTP_USERNAME": ConfigEntry{"string", &(config.Smtp.Username), nil},
"SMTP_PASSWORD": ConfigEntry{"string", &(config.SmtpPassword), nil}, "SMTP_PASSWORD": ConfigEntry{"string", &(config.Smtp.Password), nil},
"SMTP_PORT": ConfigEntry{"uint16", &(config.SmtpPort), nil}, "SMTP_PORT": ConfigEntry{"uint16", &(config.Smtp.Port), nil},
"SMTP_AUTH_TYPE": ConfigEntry{"string", &(config.SmtpAuthType), []string{"none", "plain", "login"}}, "SMTP_AUTH_TYPE": ConfigEntry{"string", &(config.Smtp.AuthType), []string{"none", "plain", "login"}},
"SMTP_SECURITY_TYPE": ConfigEntry{"string", &(config.SmtpSecurityType), []string{"none", "tls", "starttls"}}, "SMTP_SECURITY_TYPE": ConfigEntry{"string", &(config.Smtp.SecurityType), []string{"none", "tls", "starttls"}},
} }
for envVar, mapEntry := range dataMap { for envVar, mapEntry := range dataMap {

40
cmd/trello2mail/email.go Normal file
View file

@ -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() {
}

View file

@ -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
}
}

View file

@ -34,11 +34,23 @@ func main() {
fmt.Fprintf(os.Stderr, "Error: %s\n", err.Error()) fmt.Fprintf(os.Stderr, "Error: %s\n", err.Error())
os.Exit(1) os.Exit(1)
} }
fmt.Printf("%#v\n", config)
// Build headers // Get task list as markdown
headers := make(MailHeaders) trelloCtx := NewTrello(config.Trello.Token)
headers.ParseConfig(*config) 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 // Connect & authenticate
fmt.Println("Connecting...") fmt.Println("Connecting...")
@ -49,14 +61,8 @@ func main() {
fmt.Printf("Authenticating...\n") fmt.Printf("Authenticating...\n")
if err := client.Auth(*authConfig); err != nil { if err := client.Auth(*authConfig); err != nil {
log.Panic(err)
}
fmt.Println("Disconnecting...") fmt.Println("Disconnecting...")
client.Quit() client.Quit()
// Write email // Write email
// mdTasklist := ImportFromTrello(config)
// htmlTasklist := ConvertMarkdown(markdown)
// BuildEmail(config, htmlTasklist)
} }

View file

@ -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
}
}

View file

@ -6,10 +6,28 @@ package main
import ( import (
"fmt" "fmt"
//"github.com/VojtechVitek/go-trello"
"os/exec" "os/exec"
"strings" "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 { func runcmd(command string) string {
shell := "/bin/sh" shell := "/bin/sh"
flag := "-c" flag := "-c"
@ -22,7 +40,15 @@ func runcmd(command string) string {
return string(out) 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) { func RunTaskell(boardUrl string) {