diff --git a/cmd/trello2mail/config.go b/cmd/trello2mail/config.go index d09ecb5..ebdef1a 100644 --- a/cmd/trello2mail/config.go +++ b/cmd/trello2mail/config.go @@ -3,12 +3,10 @@ package main import ( "errors" "fmt" - // "log" + // "github.com/davecgh/go-spew/spew" + "log" "os" "strconv" - // "net" - // "net/mail" - // "gopkg.in/russross/blackfriday.v2" ) type ConfigEntry struct { @@ -60,15 +58,20 @@ func (config *Config) ParseEnv() (int, error) { "SMTP_PASSWORD": ConfigEntry{"string", &(config.Smtp.Password), nil}, "SMTP_PORT": ConfigEntry{"uint16", &(config.Smtp.Port), nil}, - "SMTP_AUTH_TYPE": ConfigEntry{"string", &(config.Smtp.AuthType), []string{"none", "plain", "login"}}, - "SMTP_SECURITY_TYPE": ConfigEntry{"string", &(config.Smtp.SecurityType), []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 { envValue := os.Getenv(envVar) if len(envValue) == 0 { - return -1, errors.New(fmt.Sprintf( - "Empty environment variable. Please set %s value", envVar)) + errmsg := fmt.Sprintf( + "Empty environment variable. Please set %s value", + envVar, + ) + log.Panic(errors.New(errmsg)) } if mapEntry.Values != nil { @@ -79,8 +82,13 @@ func (config *Config) ParseEnv() (int, error) { } } if !allowedValue { - return -1, errors.New(fmt.Sprintf( - "Wrong value for %s=%s. Value must be one of %v", envVar, envValue, mapEntry.Values)) + errmsg := fmt.Sprintf( + "Wrong value for %s=%s. Value must be one of %v", + envVar, + envValue, + mapEntry.Values, + ) + log.Panic(errors.New(errmsg)) } } @@ -91,23 +99,36 @@ func (config *Config) ParseEnv() (int, error) { case "uint16": u64, err := strconv.ParseUint(envValue, 10, 16) if err != nil { - return -1, errors.New(fmt.Sprintf( - "Unable to convert %s=%s to unsigned int", envVar, envValue)) + errmsg := fmt.Sprintf( + "Unable to convert %s=%s to unsigned int", + envVar, + envValue, + ) + log.Panic(errors.New(errmsg)) } *(mapEntry.Ptr.(*uint16)) = uint16(u64) case "bool": b, err := strconv.ParseBool(envValue) if err != nil { - return -1, errors.New(fmt.Sprintf( - "Unable to convert %s=%s to boolean", envVar, envValue)) + errmsg := fmt.Sprintf( + "Unable to convert %s=%s to boolean", + envVar, + envValue, + ) + log.Panic(errors.New(errmsg)) } *(mapEntry.Ptr.(*bool)) = b default: - return -1, errors.New(fmt.Sprintf("Undefined parser for %s<%s>", envVar, mapEntry.Type)) + errmsg := fmt.Sprintf( + "Undefined parser for %s<%s>", + envVar, + mapEntry.Type, + ) + log.Panic(errors.New(errmsg)) } } - fmt.Printf("%#v\n", config) + // spew.Dump(config) return 0, nil } diff --git a/cmd/trello2mail/email.go b/cmd/trello2mail/email.go index 7d6d2e0..e7283f2 100644 --- a/cmd/trello2mail/email.go +++ b/cmd/trello2mail/email.go @@ -1,15 +1,14 @@ package main import ( - "crypto/tls" - // "errors" - "fmt" - "log" - // "os" - // "strconv" - // "net" - // "net/mail" - "net/smtp" +// "errors" +// "fmt" +// "log" +// "os" +// "strconv" +// "net" +// "net/mail" +// "net/smtp" ) type MailHeaders map[string]string diff --git a/cmd/trello2mail/main.go b/cmd/trello2mail/main.go index e8bd659..73a3b6b 100644 --- a/cmd/trello2mail/main.go +++ b/cmd/trello2mail/main.go @@ -6,12 +6,8 @@ package main // - Markdown rendering https://github.com/russross/blackfriday import ( - "fmt" - "log" - "os" - // "net" - // "net/mail" - // "gopkg.in/russross/blackfriday.v2" +// "gopkg.in/russross/blackfriday.v2" +// "github.com/davecgh/go-spew/spew" ) func BuildContent(config Config) []string { @@ -30,39 +26,24 @@ func ImportFromTrello() { func main() { // Setup config config := NewConfig() - if _, err := config.ParseEnv(); err != nil { - fmt.Fprintf(os.Stderr, "Error: %s\n", err.Error()) - os.Exit(1) - } + config.ParseEnv() // Get task list as markdown trelloCtx := NewTrello(config.Trello.Token) trelloBoard := trelloCtx.GetBoard(config.Trello.Url) trelloMarkdown := trelloBoard.ExportToMarkdown() + panic("samere") - // Create and send email + // Create email enveloppe email := NewEmail() email.MakeHeaders(config.Email) email.MakeBody(trelloMarkdown) email.Send() - transport := NewTransport(config.Smtp) - if err := transport.Authenticate() { - log.Panic(err) - } + // Connect and send email + transport := NewTransport(config.Smtp) + transport.Dial() + transport.Authenticate() transport.Send(email) - - // Connect & authenticate - fmt.Println("Connecting...") - client := NewSmtpClient(*config) - - // Build auth - authConfig := NewAuth(*config) - fmt.Printf("Authenticating...\n") - - if err := client.Auth(*authConfig); err != nil { - fmt.Println("Disconnecting...") - client.Quit() - - // Write email + transport.Quit() } diff --git a/cmd/trello2mail/transport.go b/cmd/trello2mail/transport.go index 1457f80..99a05c1 100644 --- a/cmd/trello2mail/transport.go +++ b/cmd/trello2mail/transport.go @@ -1,7 +1,10 @@ package main import ( + "crypto/tls" "fmt" + "log" + "net/smtp" ) type TransportCtx struct { @@ -18,7 +21,7 @@ func NewTransport(config SmtpConfig) *TransportCtx { ctx.Address = fmt.Sprintf("%s:%d", config.Hostname, config.Port) ctx.Auth = NewTransportAuth(config) ctx.Tls = NewTransportTls(config) - return ctx + return &ctx } func NewTransportAuth(config SmtpConfig) *smtp.Auth { @@ -49,49 +52,67 @@ func NewTransportTls(config SmtpConfig) *tls.Config { } } -func (*TransportCtx) DialInsecure() error { +func (ctx *TransportCtx) DialInsecure() { + // no SSL/TLS + fmt.Println("Creating SMTP client...") + c, err := smtp.Dial(ctx.Address) + if err != nil { + log.Panic(err) + } + ctx.Client = c } -func (*TransportCtx) DialTls() error { +func (ctx *TransportCtx) DialTls() { + fmt.Printf("Creating TLS connection to %s...\n", ctx.Address) + conn, err := tls.Dial("tcp", ctx.Address, ctx.Tls) + if err != nil { + log.Panic(err) + } + + fmt.Println("Creating SMTP client...") + c, err := smtp.NewClient(conn, ctx.Config.Hostname) + if err != nil { + log.Panic(err) + } + ctx.Client = c } -func (*TransportCtx) DialStartTls(address) error { +func (ctx *TransportCtx) DialStartTls() { + fmt.Println("Creating SMTP client...") + c, err := smtp.Dial(ctx.Address) + if err != nil { + log.Panic(err) + } + fmt.Printf("Creating StartTLS connection to %s...\n", ctx.Address) + c.StartTLS(ctx.Tls) + + ctx.Client = c } -func (*TransportCtx) Dial() *smtp.Client { - switch config.SecurityType { +func (ctx *TransportCtx) Dial() { + switch ctx.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 + ctx.DialTls() 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 + ctx.DialStartTls() default: - // no SSL/TLS - fmt.Println("Creating SMTP client...") - c, err := smtp.Dial(address) - if err != nil { - log.Panic(err) - } - return c + ctx.DialInsecure() } } + +func (ctx *TransportCtx) Authenticate() { + 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) { + return +} diff --git a/cmd/trello2mail/trello.go b/cmd/trello2mail/trello.go index e202b1c..ecc1b55 100644 --- a/cmd/trello2mail/trello.go +++ b/cmd/trello2mail/trello.go @@ -1,18 +1,23 @@ -// create taskell configuration -// run taskell and read content ? -// use https://github.com/adlio/trello ? - package main import ( - "fmt" - //"github.com/VojtechVitek/go-trello" + // "errors" + // "fmt" + "github.com/adlio/trello" + // "github.com/davecgh/go-spew/spew" + // "log" "os/exec" - "strings" + // "strings" +) + +const ( + // FIXME: declare app to trello and get a real token for this app + APP_KEY string = "80dbcf6f88f62cc5639774e13342c20b" ) type TrelloCtx struct { - Token string + Token string + Client *trello.Client } type TrelloItem struct { @@ -41,7 +46,32 @@ func runcmd(command string) string { } func NewTrello(token string) *TrelloCtx { - return &TrelloCtx{Token: token} + client := trello.NewClient(APP_KEY, token) + /* + spew.Dump(client) + if client == nil { + url := strings.Join([]string{ + "https://trello.com/1/authorize?expiration=never", + "name=taskell", + "scope=read", + "response_type=token", + fmt.Sprintf("key=%s", APP_KEY), + }, "&") + + text := strings.Join([]string{ + "Wrong TRELLO_TOKEN value. Please visit:", + url, + "When you have your access token, set TRELLO_TOKEN=", + }, "\n\n") + + log.Panic(errors.New(text)) + } + */ + ctx := TrelloCtx{} + ctx.Token = token + ctx.Client = client + + return &ctx } func (ctx *TrelloCtx) GetBoard(boardUrl string) TrelloBoard { @@ -49,9 +79,11 @@ func (ctx *TrelloCtx) GetBoard(boardUrl string) TrelloBoard { } func (*TrelloBoard) ExportToMarkdown() []string { + return []string{} } +/* func RunTaskell(boardUrl string) { cmd := fmt.Sprintf("taskell -t %s -", boardUrl) markdown := strings.TrimSpace(runcmd(cmd)) -} +}*/