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)
widget = travisci.NewWidget(app, pages, settings)
case "trello":
widget = trello.NewWidget(app)
settings := trello.NewSettingsFromYAML(wtf.Config)
widget = trello.NewWidget(app, settings)
case "twitter":
widget = twitter.NewWidget(app, pages)
case "victorops":

View File

@ -4,11 +4,10 @@ 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)
func GetCards(client *trello.Client, username string, boardName string, lists map[string]string) (*SearchResult, error) {
boardID, err := getBoardID(client, username, boardName)
if err != nil {
return nil, err
}
@ -43,8 +42,8 @@ func GetCards(client *trello.Client, lists map[string]string) (*SearchResult, er
return searchResult, nil
}
func getBoardID(client *trello.Client) (string, error) {
member, err := client.GetMember(wtf.Config.UString("wtf.mods.trello.username"), trello.Defaults())
func getBoardID(client *trello.Client, username, boardName string) (string, error) {
member, err := client.GetMember(username, trello.Defaults())
if err != nil {
return "", err
}
@ -55,12 +54,12 @@ func getBoardID(client *trello.Client) (string, error) {
}
for _, board := range boards {
if board.Name == wtf.Config.UString("wtf.mods.trello.board") {
if board.Name == boardName {
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) {

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