From 4b5045a0bb953756e74c61cbbb34ba4305164ef4 Mon Sep 17 00:00:00 2001 From: Chris Cummer Date: Tue, 16 Apr 2019 20:22:54 -0700 Subject: [PATCH] WTF-400 Trello extracted to new config format --- main.go | 3 +- modules/trello/client.go | 13 ++++----- modules/trello/settings.go | 56 ++++++++++++++++++++++++++++++++++++++ modules/trello/widget.go | 53 ++++++++++-------------------------- 4 files changed, 78 insertions(+), 47 deletions(-) create mode 100644 modules/trello/settings.go diff --git a/main.go b/main.go index f55be2d7..c7a59392 100644 --- a/main.go +++ b/main.go @@ -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": diff --git a/modules/trello/client.go b/modules/trello/client.go index 3252eb57..f61f9968 100644 --- a/modules/trello/client.go +++ b/modules/trello/client.go @@ -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) { diff --git a/modules/trello/settings.go b/modules/trello/settings.go new file mode 100644 index 00000000..c02bcf5d --- /dev/null +++ b/modules/trello/settings.go @@ -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 +} diff --git a/modules/trello/widget.go b/modules/trello/widget.go index 61ddf0cc..8ba8c89e 100644 --- a/modules/trello/widget.go +++ b/modules/trello/widget.go @@ -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 -}