Use templates

This commit is contained in:
Glenn Y. Rolland 2018-11-23 12:41:16 +01:00
parent bbfc5ec7f6
commit f49f2c15d6
6 changed files with 63 additions and 26 deletions

View file

@ -86,9 +86,6 @@ func (self *Config) Parse() error {
// environment & config // environment & config
// viper.SetEnvPrefix("") // viper.SetEnvPrefix("")
fmt.Printf("all: %#v\n", viper.AllSettings())
fmt.Printf("email-from %s\n", viper.Get("email-from"))
if err := self.Parser.Execute(); err != nil { if err := self.Parser.Execute(); err != nil {
fmt.Println(err) fmt.Println(err)
os.Exit(1) os.Exit(1)

View file

@ -62,7 +62,6 @@ func encodeRFC2047(text string) string {
func (email *EmailCtx) SetHeaders(config EmailConfig) { func (email *EmailCtx) SetHeaders(config EmailConfig) {
email.Headers["Return-Path"] = config.From email.Headers["Return-Path"] = config.From
email.Headers["From"] = config.From email.Headers["From"] = config.From
fmt.Printf("config.To %#v\n", config.To)
if len(config.To) < 1 { if len(config.To) < 1 {
errmsg := "EMAIL_TO must contain at least one value" errmsg := "EMAIL_TO must contain at least one value"
log.Panic(errors.New(errmsg)) log.Panic(errors.New(errmsg))

View file

@ -10,7 +10,6 @@ func main() {
// Setup config // Setup config
config := NewConfig() config := NewConfig()
config.Parse() config.Parse()
fmt.Printf("%#v\n", config)
// Get task list as markdown // Get task list as markdown
trelloCtx := NewTrello(config.TrelloToken) trelloCtx := NewTrello(config.TrelloToken)

View file

@ -5,11 +5,11 @@ import (
"fmt" "fmt"
"github.com/adlio/trello" "github.com/adlio/trello"
"github.com/russross/blackfriday/v2" "github.com/russross/blackfriday/v2"
// "gopkg.in/russross/blackfriday.v2"
"log" "log"
"net/url" "net/url"
"os/exec" "os/exec"
"strings" "strings"
"text/template"
) )
const ( const (
@ -87,36 +87,76 @@ func (ctx *TrelloCtx) GetBoard(boardUrl string) TrelloBoard {
return TrelloBoard{Ctx: ctx, Ptr: board, Name: board.Name} return TrelloBoard{Ctx: ctx, Ptr: board, Name: board.Name}
} }
func (board *TrelloBoard) ExportToMarkdown() string { type CardData struct {
var markdown bytes.Buffer Name string
var text string Url string
}
type ListData struct {
Name string
Cards []CardData
}
type BoardData struct {
Name string
Url string
Lists []ListData
}
func (board *TrelloBoard) ExportData() BoardData {
var boardData = BoardData{
Name: board.Ptr.Name,
Url: board.Ptr.ShortUrl,
Lists: make([]ListData, 0),
}
// get lists
lists, err := board.Ptr.GetLists(trello.Defaults()) lists, err := board.Ptr.GetLists(trello.Defaults())
if err != nil { if err != nil {
log.Panic(err) log.Panic(err)
} }
text = fmt.Sprintf("# Board %s\n\n", board.Ptr.Name) // fill in reverse order
markdown.WriteString(text)
text = fmt.Sprintf("URL: %s\n", board.Ptr.ShortUrl)
markdown.WriteString(text)
for listIdx := len(lists) - 1; listIdx >= 0; listIdx -= 1 { for listIdx := len(lists) - 1; listIdx >= 0; listIdx -= 1 {
list := lists[listIdx] list := lists[listIdx]
text := fmt.Sprintf("\n## %s\n\n", list.Name) listData := ListData{
markdown.WriteString(text) Name: list.Name,
Cards: make([]CardData, 0),
}
cards, err := list.GetCards(trello.Defaults()) cards, err := list.GetCards(trello.Defaults())
if err != nil { if err != nil {
log.Panic(err) log.Panic(err)
} }
for _, card := range cards { for _, card := range cards {
text := fmt.Sprintf("* %s ([link](%s))\n", card.Name, card.Url) cardData := CardData{
markdown.WriteString(text) Name: card.Name,
Url: card.Url,
}
listData.Cards = append(listData.Cards, cardData)
} }
boardData.Lists = append(boardData.Lists, listData)
} }
return markdown.String()
return boardData
}
func (board *TrelloBoard) ExportToMarkdown() string {
var buf bytes.Buffer
data := board.ExportData()
t, err := template.ParseFiles("templates/markdown.tmpl")
if err != nil {
log.Panic("Unable to parse template files")
}
if err := t.Execute(&buf, data); err != nil {
log.Panicf("Template execution: %s", err)
}
return buf.String()
} }
func (board *TrelloBoard) ExportToHtml() string { func (board *TrelloBoard) ExportToHtml() string {
@ -124,9 +164,3 @@ func (board *TrelloBoard) ExportToHtml() string {
html := blackfriday.Run([]byte(markdown)) html := blackfriday.Run([]byte(markdown))
return string(html) return string(html)
} }
/*
func RunTaskell(boardUrl string) {
cmd := fmt.Sprintf("taskell -t %s -", boardUrl)
markdown := strings.TrimSpace(runcmd(cmd))
}*/

0
templates/html.tmpl Normal file
View file

8
templates/markdown.tmpl Normal file
View file

@ -0,0 +1,8 @@
# Board {{ .Name }}
{{ range .Lists }}{{ if .Cards }}
## {{ .Name }}
{{ range .Cards }}* {{ .Name }} ([link]({{.Url}}))
{{ end }}
{{ end }}{{ end }}