Fix build errors

This commit is contained in:
Glenn Y. Rolland 2018-08-22 01:50:50 +02:00
parent 4be45535b4
commit 63d476f9d5
5 changed files with 152 additions and 98 deletions

View file

@ -3,12 +3,10 @@ package main
import ( import (
"errors" "errors"
"fmt" "fmt"
// "log" // "github.com/davecgh/go-spew/spew"
"log"
"os" "os"
"strconv" "strconv"
// "net"
// "net/mail"
// "gopkg.in/russross/blackfriday.v2"
) )
type ConfigEntry struct { type ConfigEntry struct {
@ -60,15 +58,20 @@ func (config *Config) ParseEnv() (int, error) {
"SMTP_PASSWORD": ConfigEntry{"string", &(config.Smtp.Password), nil}, "SMTP_PASSWORD": ConfigEntry{"string", &(config.Smtp.Password), nil},
"SMTP_PORT": ConfigEntry{"uint16", &(config.Smtp.Port), nil}, "SMTP_PORT": ConfigEntry{"uint16", &(config.Smtp.Port), nil},
"SMTP_AUTH_TYPE": ConfigEntry{"string", &(config.Smtp.AuthType), []string{"none", "plain", "login"}}, "SMTP_AUTH_TYPE": ConfigEntry{"string",
"SMTP_SECURITY_TYPE": ConfigEntry{"string", &(config.Smtp.SecurityType), []string{"none", "tls", "starttls"}}, &(config.Smtp.AuthType), []string{"none", "plain", "login"}},
"SMTP_SECURITY_TYPE": ConfigEntry{"string",
&(config.Smtp.SecurityType), []string{"none", "tls", "starttls"}},
} }
for envVar, mapEntry := range dataMap { for envVar, mapEntry := range dataMap {
envValue := os.Getenv(envVar) envValue := os.Getenv(envVar)
if len(envValue) == 0 { if len(envValue) == 0 {
return -1, errors.New(fmt.Sprintf( errmsg := fmt.Sprintf(
"Empty environment variable. Please set %s value", envVar)) "Empty environment variable. Please set %s value",
envVar,
)
log.Panic(errors.New(errmsg))
} }
if mapEntry.Values != nil { if mapEntry.Values != nil {
@ -79,8 +82,13 @@ func (config *Config) ParseEnv() (int, error) {
} }
} }
if !allowedValue { if !allowedValue {
return -1, errors.New(fmt.Sprintf( errmsg := fmt.Sprintf(
"Wrong value for %s=%s. Value must be one of %v", envVar, envValue, mapEntry.Values)) "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": case "uint16":
u64, err := strconv.ParseUint(envValue, 10, 16) u64, err := strconv.ParseUint(envValue, 10, 16)
if err != nil { if err != nil {
return -1, errors.New(fmt.Sprintf( errmsg := fmt.Sprintf(
"Unable to convert %s=%s to unsigned int", envVar, envValue)) "Unable to convert %s=%s to unsigned int",
envVar,
envValue,
)
log.Panic(errors.New(errmsg))
} }
*(mapEntry.Ptr.(*uint16)) = uint16(u64) *(mapEntry.Ptr.(*uint16)) = uint16(u64)
case "bool": case "bool":
b, err := strconv.ParseBool(envValue) b, err := strconv.ParseBool(envValue)
if err != nil { if err != nil {
return -1, errors.New(fmt.Sprintf( errmsg := fmt.Sprintf(
"Unable to convert %s=%s to boolean", envVar, envValue)) "Unable to convert %s=%s to boolean",
envVar,
envValue,
)
log.Panic(errors.New(errmsg))
} }
*(mapEntry.Ptr.(*bool)) = b *(mapEntry.Ptr.(*bool)) = b
default: 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 return 0, nil
} }

View file

@ -1,15 +1,14 @@
package main package main
import ( import (
"crypto/tls" // "errors"
// "errors" // "fmt"
"fmt" // "log"
"log" // "os"
// "os" // "strconv"
// "strconv" // "net"
// "net" // "net/mail"
// "net/mail" // "net/smtp"
"net/smtp"
) )
type MailHeaders map[string]string type MailHeaders map[string]string

View file

@ -6,12 +6,8 @@ package main
// - Markdown rendering https://github.com/russross/blackfriday // - Markdown rendering https://github.com/russross/blackfriday
import ( import (
"fmt" // "gopkg.in/russross/blackfriday.v2"
"log" // "github.com/davecgh/go-spew/spew"
"os"
// "net"
// "net/mail"
// "gopkg.in/russross/blackfriday.v2"
) )
func BuildContent(config Config) []string { func BuildContent(config Config) []string {
@ -30,39 +26,24 @@ func ImportFromTrello() {
func main() { func main() {
// Setup config // Setup config
config := NewConfig() config := NewConfig()
if _, err := config.ParseEnv(); err != nil { config.ParseEnv()
fmt.Fprintf(os.Stderr, "Error: %s\n", err.Error())
os.Exit(1)
}
// Get task list as markdown // Get task list as markdown
trelloCtx := NewTrello(config.Trello.Token) trelloCtx := NewTrello(config.Trello.Token)
trelloBoard := trelloCtx.GetBoard(config.Trello.Url) trelloBoard := trelloCtx.GetBoard(config.Trello.Url)
trelloMarkdown := trelloBoard.ExportToMarkdown() trelloMarkdown := trelloBoard.ExportToMarkdown()
panic("samere")
// Create and send email // Create email enveloppe
email := NewEmail() email := NewEmail()
email.MakeHeaders(config.Email) email.MakeHeaders(config.Email)
email.MakeBody(trelloMarkdown) email.MakeBody(trelloMarkdown)
email.Send() email.Send()
transport := NewTransport(config.Smtp) // Connect and send email
if err := transport.Authenticate() { transport := NewTransport(config.Smtp)
log.Panic(err) transport.Dial()
} transport.Authenticate()
transport.Send(email) transport.Send(email)
transport.Quit()
// 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
} }

View file

@ -1,7 +1,10 @@
package main package main
import ( import (
"crypto/tls"
"fmt" "fmt"
"log"
"net/smtp"
) )
type TransportCtx struct { type TransportCtx struct {
@ -18,7 +21,7 @@ func NewTransport(config SmtpConfig) *TransportCtx {
ctx.Address = fmt.Sprintf("%s:%d", config.Hostname, config.Port) ctx.Address = fmt.Sprintf("%s:%d", config.Hostname, config.Port)
ctx.Auth = NewTransportAuth(config) ctx.Auth = NewTransportAuth(config)
ctx.Tls = NewTransportTls(config) ctx.Tls = NewTransportTls(config)
return ctx return &ctx
} }
func NewTransportAuth(config SmtpConfig) *smtp.Auth { 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 { func (ctx *TransportCtx) Dial() {
switch config.SecurityType { switch ctx.Config.SecurityType {
case "tls": case "tls":
fmt.Printf("Creating TLS connection to %s...\n", address) ctx.DialTls()
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": case "starttls":
fmt.Println("Creating SMTP client...") ctx.DialStartTls()
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: default:
// no SSL/TLS ctx.DialInsecure()
fmt.Println("Creating SMTP client...")
c, err := smtp.Dial(address)
if err != nil {
log.Panic(err)
}
return c
} }
} }
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
}

View file

@ -1,18 +1,23 @@
// create taskell configuration
// run taskell and read content ?
// use https://github.com/adlio/trello ?
package main package main
import ( import (
"fmt" // "errors"
//"github.com/VojtechVitek/go-trello" // "fmt"
"github.com/adlio/trello"
// "github.com/davecgh/go-spew/spew"
// "log"
"os/exec" "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 { type TrelloCtx struct {
Token string Token string
Client *trello.Client
} }
type TrelloItem struct { type TrelloItem struct {
@ -41,7 +46,32 @@ func runcmd(command string) string {
} }
func NewTrello(token string) *TrelloCtx { 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=<your-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 { func (ctx *TrelloCtx) GetBoard(boardUrl string) TrelloBoard {
@ -49,9 +79,11 @@ func (ctx *TrelloCtx) GetBoard(boardUrl string) TrelloBoard {
} }
func (*TrelloBoard) ExportToMarkdown() []string { func (*TrelloBoard) ExportToMarkdown() []string {
return []string{}
} }
/*
func RunTaskell(boardUrl string) { func RunTaskell(boardUrl string) {
cmd := fmt.Sprintf("taskell -t %s -", boardUrl) cmd := fmt.Sprintf("taskell -t %s -", boardUrl)
markdown := strings.TrimSpace(runcmd(cmd)) markdown := strings.TrimSpace(runcmd(cmd))
} }*/