musala/cmd/trello2mail/trello.go

127 lines
2.5 KiB
Go

package main
import (
"bytes"
"fmt"
"github.com/adlio/trello"
// "github.com/davecgh/go-spew/spew"
"gopkg.in/russross/blackfriday.v2"
"log"
"net/url"
"os/exec"
"strings"
)
const (
// FIXME: declare app to trello and get a real token for this app
APP_KEY string = "80dbcf6f88f62cc5639774e13342c20b"
)
type TrelloCtx struct {
Token string
Client *trello.Client
}
type TrelloBoard struct {
Ctx *TrelloCtx
Ptr *trello.Board
}
func runcmd(command string) string {
shell := "/bin/sh"
flag := "-c"
out, err := exec.Command(shell, flag, command).Output()
if err != nil {
panic(err)
}
return string(out)
}
func GetTokenProcessMessage() string {
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")
return text
}
func NewTrello(token string) *TrelloCtx {
client := trello.NewClient(APP_KEY, token)
ctx := TrelloCtx{}
ctx.Token = token
ctx.Client = client
return &ctx
}
func (ctx *TrelloCtx) GetBoard(boardUrl string) TrelloBoard {
parsedUrl, err := url.Parse(boardUrl)
if err != nil {
panic(err)
}
boardId := strings.Split(parsedUrl.Path, "/")[2]
// spew.Dump(boardId)
board, err := ctx.Client.GetBoard(boardId, trello.Defaults())
// spew.Dump(board)
return TrelloBoard{Ctx: ctx, Ptr: board}
}
func (board *TrelloBoard) ExportToMarkdown() string {
var markdown bytes.Buffer
var text string
lists, err := board.Ptr.GetLists(trello.Defaults())
if err != nil {
log.Panic(err)
}
text = fmt.Sprintf("# Board %s\n\n", board.Ptr.Name)
markdown.WriteString(text)
text = fmt.Sprintf("URL: %s\n", board.Ptr.ShortUrl)
markdown.WriteString(text)
for listIdx := len(lists) - 1; listIdx >= 0; listIdx -= 1 {
list := lists[listIdx]
text := fmt.Sprintf("\n## %s\n\n", list.Name)
markdown.WriteString(text)
cards, err := list.GetCards(trello.Defaults())
if err != nil {
log.Panic(err)
}
for _, card := range cards {
text := fmt.Sprintf("* %s\n", card.Name)
markdown.WriteString(text)
// spew.Dump(card)
}
}
return markdown.String()
}
func (board *TrelloBoard) ExportToHtml() string {
markdown := board.ExportToMarkdown()
html := blackfriday.Run([]byte(markdown))
return string(html)
}
/*
func RunTaskell(boardUrl string) {
cmd := fmt.Sprintf("taskell -t %s -", boardUrl)
markdown := strings.TrimSpace(runcmd(cmd))
}*/