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

WTF-400 Trello extracted to new config format

This commit is contained in:
Chris Cummer 2019-04-16 20:22:54 -07:00
parent 5593f19e1c
commit 4b5045a0bb
4 changed files with 78 additions and 47 deletions

View File

@ -301,7 +301,8 @@ func makeWidget(app *tview.Application, pages *tview.Pages, widgetName string) w
settings := travisci.NewSettingsFromYAML(wtf.Config) settings := travisci.NewSettingsFromYAML(wtf.Config)
widget = travisci.NewWidget(app, pages, settings) widget = travisci.NewWidget(app, pages, settings)
case "trello": case "trello":
widget = trello.NewWidget(app) settings := trello.NewSettingsFromYAML(wtf.Config)
widget = trello.NewWidget(app, settings)
case "twitter": case "twitter":
widget = twitter.NewWidget(app, pages) widget = twitter.NewWidget(app, pages)
case "victorops": case "victorops":

View File

@ -4,11 +4,10 @@ import (
"fmt" "fmt"
"github.com/adlio/trello" "github.com/adlio/trello"
"github.com/wtfutil/wtf/wtf"
) )
func GetCards(client *trello.Client, lists map[string]string) (*SearchResult, error) { func GetCards(client *trello.Client, username string, boardName string, lists map[string]string) (*SearchResult, error) {
boardID, err := getBoardID(client) boardID, err := getBoardID(client, username, boardName)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -43,8 +42,8 @@ func GetCards(client *trello.Client, lists map[string]string) (*SearchResult, er
return searchResult, nil return searchResult, nil
} }
func getBoardID(client *trello.Client) (string, error) { func getBoardID(client *trello.Client, username, boardName string) (string, error) {
member, err := client.GetMember(wtf.Config.UString("wtf.mods.trello.username"), trello.Defaults()) member, err := client.GetMember(username, trello.Defaults())
if err != nil { if err != nil {
return "", err return "", err
} }
@ -55,12 +54,12 @@ func getBoardID(client *trello.Client) (string, error) {
} }
for _, board := range boards { for _, board := range boards {
if board.Name == wtf.Config.UString("wtf.mods.trello.board") { if board.Name == boardName {
return board.ID, nil return board.ID, nil
} }
} }
return "", fmt.Errorf("could not find board with name %s", wtf.Config.UString("wtf.mods.trello.board")) return "", fmt.Errorf("could not find board with name %s", boardName)
} }
func getListIDs(client *trello.Client, boardID string, lists map[string]string) (map[string]string, error) { func getListIDs(client *trello.Client, boardID string, lists map[string]string) (map[string]string, error) {

View File

@ -0,0 +1,56 @@
package trello
import (
"os"
"github.com/olebedev/config"
"github.com/wtfutil/wtf/cfg"
)
type Settings struct {
common *cfg.Common
accessToken string
apiKey string
board string
list map[string]string
username string
}
func NewSettingsFromYAML(ymlConfig *config.Config) *Settings {
localConfig, _ := ymlConfig.Get("wtf.mods.trello")
settings := Settings{
common: cfg.NewCommonSettingsFromYAML(ymlConfig),
accessToken: localConfig.UString("accessToken", os.Getenv("WTF_TRELLO_ACCESS_TOKEN")),
apiKey: localConfig.UString("apiKey", os.Getenv("WTF_TRELLO_APP_KEY")),
board: localConfig.UString("board"),
username: localConfig.UString("username"),
}
settings.list = mapifyList(localConfig)
return &settings
}
func mapifyList(localConfig *config.Config) map[string]string {
lists := make(map[string]string)
// Single list
list, err := localConfig.String("list")
if err == nil {
lists[list] = ""
return lists
}
// Array of lists
listList := localConfig.UList("project")
for _, listName := range listList {
if list, ok := listName.(string); ok {
lists[list] = ""
}
}
return lists
}

View File

@ -2,7 +2,6 @@ package trello
import ( import (
"fmt" "fmt"
"os"
"github.com/adlio/trello" "github.com/adlio/trello"
"github.com/rivo/tview" "github.com/rivo/tview"
@ -11,11 +10,15 @@ import (
type Widget struct { type Widget struct {
wtf.TextWidget wtf.TextWidget
settings *Settings
} }
func NewWidget(app *tview.Application) *Widget { func NewWidget(app *tview.Application, settings *Settings) *Widget {
widget := Widget{ widget := Widget{
TextWidget: wtf.NewTextWidget(app, "Trello", "trello", false), TextWidget: wtf.NewTextWidget(app, "Trello", "trello", false),
settings: settings,
} }
return &widget return &widget
@ -25,12 +28,17 @@ func NewWidget(app *tview.Application) *Widget {
func (widget *Widget) Refresh() { func (widget *Widget) Refresh() {
client := trello.NewClient( client := trello.NewClient(
widget.apiKey(), widget.settings.apiKey,
widget.accessToken(), widget.settings.accessToken,
) )
// Get the cards // Get the cards
searchResult, err := GetCards(client, getLists()) searchResult, err := GetCards(
client,
widget.settings.username,
widget.settings.board,
widget.settings.list,
)
var content string var content string
if err != nil { if err != nil {
@ -43,7 +51,7 @@ func (widget *Widget) Refresh() {
fmt.Sprintf( fmt.Sprintf(
"[white]%s: [green]%s ", "[white]%s: [green]%s ",
widget.Name(), widget.Name(),
wtf.Config.UString("wtf.mods.trello.board"), widget.settings.board,
), ),
) )
content = widget.contentFrom(searchResult) content = widget.contentFrom(searchResult)
@ -54,20 +62,6 @@ func (widget *Widget) Refresh() {
/* -------------------- Unexported Functions -------------------- */ /* -------------------- 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 { func (widget *Widget) contentFrom(searchResult *SearchResult) string {
str := "" str := ""
@ -81,22 +75,3 @@ func (widget *Widget) contentFrom(searchResult *SearchResult) string {
return 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
}