1
0
mirror of https://github.com/taigrr/wtf synced 2025-01-18 04:03:14 -08:00

Migrate all modules to their own subfolder

Handles #375
This commit is contained in:
Sean Smith
2019-02-18 11:16:34 -05:00
parent c28c31aedb
commit 8030380f89
123 changed files with 51 additions and 51 deletions

8
modules/trello/card.go Normal file
View File

@@ -0,0 +1,8 @@
package trello
type TrelloCard struct {
ID string
Name string
List string
Description string
}

98
modules/trello/client.go Normal file
View File

@@ -0,0 +1,98 @@
package trello
import (
"fmt"
"github.com/adlio/trello"
"github.com/wtfutil/wtf/wtf"
)
func GetCards(client *trello.Client, lists map[string]string) (*SearchResult, error) {
boardID, err := getBoardID(client)
if err != nil {
return nil, err
}
lists, err = getListIDs(client, boardID, lists)
if err != nil {
return nil, err
}
searchResult := &SearchResult{Total: 0}
searchResult.TrelloCards = make(map[string][]TrelloCard)
for listName, listID := range lists {
cards, err := getCardsOnList(client, listID)
if err != nil {
return nil, err
}
searchResult.Total = searchResult.Total + len(cards)
cardArray := make([]TrelloCard, 0)
for _, card := range cards {
trelloCard := TrelloCard{
ID: card.ID,
List: listName,
Name: card.Name,
Description: card.Desc,
}
cardArray = append(cardArray, trelloCard)
}
searchResult.TrelloCards[listName] = cardArray
}
return searchResult, nil
}
func getBoardID(client *trello.Client) (string, error) {
member, err := client.GetMember(wtf.Config.UString("wtf.mods.trello.username"), trello.Defaults())
if err != nil {
return "", err
}
boards, err := member.GetBoards(trello.Defaults())
if err != nil {
return "", err
}
for _, board := range boards {
if board.Name == wtf.Config.UString("wtf.mods.trello.board") {
return board.ID, nil
}
}
return "", fmt.Errorf("could not find board with name %s", wtf.Config.UString("wtf.mods.trello.board"))
}
func getListIDs(client *trello.Client, boardID string, lists map[string]string) (map[string]string, error) {
board, err := client.GetBoard(boardID, trello.Defaults())
if err != nil {
return nil, err
}
boardLists, err := board.GetLists(trello.Defaults())
if err != nil {
return nil, err
}
for _, list := range boardLists {
if _, ok := lists[list.Name]; ok {
lists[list.Name] = list.ID
}
}
return lists, nil
}
func getCardsOnList(client *trello.Client, listID string) ([]*trello.Card, error) {
list, err := client.GetList(listID, trello.Defaults())
if err != nil {
return nil, err
}
cards, err := list.GetCards(trello.Defaults())
if err != nil {
return nil, err
}
return cards, nil
}

View File

@@ -0,0 +1,6 @@
package trello
type SearchResult struct {
Total int
TrelloCards map[string][]TrelloCard
}

102
modules/trello/widget.go Normal file
View File

@@ -0,0 +1,102 @@
package trello
import (
"fmt"
"os"
"github.com/adlio/trello"
"github.com/rivo/tview"
"github.com/wtfutil/wtf/wtf"
)
type Widget struct {
wtf.TextWidget
}
func NewWidget(app *tview.Application) *Widget {
widget := Widget{
TextWidget: wtf.NewTextWidget(app, "Trello", "trello", false),
}
return &widget
}
/* -------------------- Exported Functions -------------------- */
func (widget *Widget) Refresh() {
client := trello.NewClient(
widget.apiKey(),
widget.accessToken(),
)
// Get the cards
searchResult, err := GetCards(client, getLists())
var content string
if err != nil {
widget.View.SetWrap(true)
widget.View.SetTitle(widget.Name)
content = err.Error()
} else {
widget.View.SetWrap(false)
widget.View.SetTitle(
fmt.Sprintf(
"[white]%s: [green]%s ",
widget.Name,
wtf.Config.UString("wtf.mods.trello.board"),
),
)
content = widget.contentFrom(searchResult)
}
widget.View.SetText(content)
}
/* -------------------- Unexported Functions -------------------- */
func (widget *Widget) accessToken() string {
return wtf.Config.UString(
"wtf.mods.trello.accessToken",
os.Getenv("WTF_TRELLO_ACCESS_TOKEN"),
)
}
func (widget *Widget) apiKey() string {
return wtf.Config.UString(
"wtf.mods.trello.apiKey",
os.Getenv("WTF_TRELLO_APP_KEY"),
)
}
func (widget *Widget) contentFrom(searchResult *SearchResult) string {
str := ""
for list, cardArray := range searchResult.TrelloCards {
str = fmt.Sprintf("%s [red]Cards in %s[white]\n", str, list)
for _, card := range cardArray {
str = fmt.Sprintf("%s [green]%s[white]\n", str, card.Name)
}
str = fmt.Sprintf("%s\n", str)
}
return str
}
func getLists() map[string]string {
list := make(map[string]string)
// see if project is set to a single string
configPath := "wtf.mods.trello.list"
singleList, err := wtf.Config.String(configPath)
if err == nil {
list[singleList] = ""
return list
}
// else, assume list
multiList := wtf.Config.UList(configPath)
for _, proj := range multiList {
if str, ok := proj.(string); ok {
list[str] = ""
}
}
return list
}