musala/cmd/trello2mail/trello.go

120 lines
2.4 KiB
Go
Raw Normal View History

2018-08-21 08:50:30 +00:00
package main
2018-08-21 08:57:59 +00:00
import (
2018-08-22 10:29:14 +00:00
"bytes"
2018-08-22 00:39:08 +00:00
"fmt"
2018-08-21 23:50:50 +00:00
"github.com/adlio/trello"
2018-08-22 00:39:08 +00:00
// "github.com/davecgh/go-spew/spew"
2018-08-22 10:34:33 +00:00
"log"
2018-08-22 00:39:08 +00:00
"net/url"
2018-08-21 08:57:59 +00:00
"os/exec"
2018-08-22 00:39:08 +00:00
"strings"
2018-08-21 23:50:50 +00:00
)
const (
// FIXME: declare app to trello and get a real token for this app
APP_KEY string = "80dbcf6f88f62cc5639774e13342c20b"
2018-08-21 08:57:59 +00:00
)
type TrelloCtx struct {
2018-08-21 23:50:50 +00:00
Token string
Client *trello.Client
}
type TrelloBoard struct {
2018-08-22 00:39:08 +00:00
Ctx *TrelloCtx
Ptr *trello.Board
}
2018-08-21 08:57:59 +00:00
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)
}
2018-08-22 10:29:14 +00:00
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 {
2018-08-21 23:50:50 +00:00
client := trello.NewClient(APP_KEY, token)
2018-08-22 10:29:14 +00:00
2018-08-21 23:50:50 +00:00
ctx := TrelloCtx{}
ctx.Token = token
ctx.Client = client
return &ctx
}
func (ctx *TrelloCtx) GetBoard(boardUrl string) TrelloBoard {
2018-08-22 00:39:08 +00:00
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}
}
2018-08-22 10:29:14 +00:00
func (board *TrelloBoard) ExportToMarkdown() string {
var markdown bytes.Buffer
var text string
2018-08-22 00:39:08 +00:00
2018-08-22 10:34:33 +00:00
lists, err := board.Ptr.GetLists(trello.Defaults())
if err != nil {
log.Panic(err)
}
2018-08-22 10:29:14 +00:00
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)
2018-08-22 10:34:33 +00:00
cards, err := list.GetCards(trello.Defaults())
if err != nil {
log.Panic(err)
}
2018-08-22 10:29:14 +00:00
for _, card := range cards {
text := fmt.Sprintf("* %s\n", card.Name)
markdown.WriteString(text)
// spew.Dump(card)
}
2018-08-22 00:39:08 +00:00
}
2018-08-22 10:29:14 +00:00
return markdown.String()
2018-08-21 08:50:30 +00:00
}
2018-08-21 23:50:50 +00:00
/*
2018-08-21 08:57:59 +00:00
func RunTaskell(boardUrl string) {
cmd := fmt.Sprintf("taskell -t %s -", boardUrl)
markdown := strings.TrimSpace(runcmd(cmd))
2018-08-21 23:50:50 +00:00
}*/