Skip to content

Commit

Permalink
simplify source config access
Browse files Browse the repository at this point in the history
  • Loading branch information
utkuufuk committed Jun 6, 2020
1 parent e438c43 commit 53feef9
Show file tree
Hide file tree
Showing 8 changed files with 88 additions and 159 deletions.
8 changes: 4 additions & 4 deletions cmd/entrello/queue.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,19 @@ type CardQueue struct {
// queueActionables fetches new cards from the source, then pushes those to be created and
// to be deleted into the corresponding channels, as well as any errors encountered.
func queueActionables(src source, client trello.Client, q CardQueue) {
cards, err := src.FetchNewCards()
cards, err := src.api.FetchNewCards()
if err != nil {
q.err <- fmt.Errorf("could not fetch cards for source '%s': %v", src.GetName(), err)
q.err <- fmt.Errorf("could not fetch cards for source '%s': %v", src.cfg.Name, err)
return
}

new, stale := client.CompareWithExisting(cards, src.GetLabel())
new, stale := client.CompareWithExisting(cards, src.cfg.Label)

for _, c := range new {
q.add <- c
}

if !src.IsStrict() {
if !src.cfg.Strict {
return
}

Expand Down
56 changes: 21 additions & 35 deletions cmd/entrello/source.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,60 +11,46 @@ import (
"github.com/utkuufuk/entrello/internal/trello"
)

// source defines an interface for a Trello card source
type source interface {
// IsEnabled returns true if the source is enabled.
IsEnabled() bool

// IsStrict returns true if "strict" mode is enabled for the source
IsStrict() bool

// GetName returns a human-readable name of the source
GetName() string

// GetLabel returns the corresponding card label ID for the source
GetLabel() string

// GetPeriod returns the period in minutes that the source should be checked
GetPeriod() config.Period

// FetchNewCards returns a list of Trello cards to be inserted into the board from the source
FetchNewCards() ([]trello.Card, error)
type source struct {
cfg config.SourceConfig
api interface {
FetchNewCards() ([]trello.Card, error)
}
}

// getEnabledSourcesAndLabels returns a list of enabled sources & all relevant label IDs
func getEnabledSourcesAndLabels(ctx context.Context, cfg config.Sources) (sources []source, labels []string) {
arr := []source{
github.GetSource(ctx, cfg.GithubIssues),
tododock.GetSource(cfg.TodoDock),
func getEnabledSourcesAndLabels(ctx context.Context, cfg config.Sources) (s []source, l []string) {
sources := []source{
{cfg.GithubIssues.SourceConfig, github.GetSource(ctx, cfg.GithubIssues)},
{cfg.TodoDock.SourceConfig, tododock.GetSource(cfg.TodoDock)},
}
now := time.Now()

for _, src := range arr {
if ok, err := shouldQuery(src, now); !ok {
for _, src := range sources {
if ok, err := shouldQuery(src.cfg, now); !ok {
if err != nil {
logger.Errorf("could not check if '%s' should be queried or not, skipping", src.GetName())
logger.Errorf("could not check if '%s' should be queried or not, skipping", src.cfg.Name)
}
continue
}
sources = append(sources, src)
labels = append(labels, src.GetLabel())
s = append(s, src)
l = append(l, src.cfg.Label)
}
return sources, labels
return s, l
}

// shouldQuery checks if the given source should be queried at the given time
func shouldQuery(src source, now time.Time) (bool, error) {
if !src.IsEnabled() {
// shouldQuery checks if a query should be executed at the given time given the source configuration
func shouldQuery(cfg config.SourceConfig, now time.Time) (bool, error) {
if !cfg.Enabled {
return false, nil
}

interval := src.GetPeriod().Interval
interval := cfg.Period.Interval
if interval < 0 {
return false, fmt.Errorf("period interval must be a positive integer, got: '%d'", interval)
}

switch src.GetPeriod().Type {
switch cfg.Period.Type {
case config.PERIOD_TYPE_DEFAULT:
return true, nil
case config.PERIOD_TYPE_DAY:
Expand All @@ -84,5 +70,5 @@ func shouldQuery(src source, now time.Time) (bool, error) {
return now.Minute()%interval == 0, nil
}

return false, fmt.Errorf("unrecognized source period type: '%s'", src.GetPeriod().Type)
return false, fmt.Errorf("unrecognized source period type: '%s'", cfg.Period.Type)
}
46 changes: 22 additions & 24 deletions cmd/entrello/source_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (

"github.com/google/go-cmp/cmp"
"github.com/utkuufuk/entrello/internal/config"
"github.com/utkuufuk/entrello/internal/tododock"
)

func TestGetEnabledSourcesAndLabels(t *testing.T) {
Expand All @@ -18,33 +17,33 @@ func TestGetEnabledSourcesAndLabels(t *testing.T) {
}

tt := []struct {
name string
githubIssues config.GithubIssues
todoDock config.TodoDock
numResults int
labels []string
name string
githubIssuesCfg config.SourceConfig
todoDockCfg config.SourceConfig
numResults int
labels []string
}{
{
name: "nothing enabled",
githubIssues: config.GithubIssues{Enabled: false, Period: period},
todoDock: config.TodoDock{Enabled: false, Period: period},
numResults: 0,
name: "nothing enabled",
githubIssuesCfg: config.SourceConfig{Enabled: false, Period: period},
todoDockCfg: config.SourceConfig{Enabled: false, Period: period},
numResults: 0,
},
{
name: "only github issues enabled",
githubIssues: config.GithubIssues{
githubIssuesCfg: config.SourceConfig{
Enabled: true,
Period: period,
Label: "github-label",
},
todoDock: config.TodoDock{Enabled: false, Period: period},
numResults: 1,
labels: []string{"github-label"},
todoDockCfg: config.SourceConfig{Enabled: false, Period: period},
numResults: 1,
labels: []string{"github-label"},
},
{
name: "only tododock enabled",
githubIssues: config.GithubIssues{Enabled: false, Period: period},
todoDock: config.TodoDock{
name: "only tododock enabled",
githubIssuesCfg: config.SourceConfig{Enabled: false, Period: period},
todoDockCfg: config.SourceConfig{
Enabled: true,
Period: period,
Label: "tododock-label",
Expand All @@ -54,12 +53,12 @@ func TestGetEnabledSourcesAndLabels(t *testing.T) {
},
{
name: "all enabled",
githubIssues: config.GithubIssues{
githubIssuesCfg: config.SourceConfig{
Enabled: true,
Period: period,
Label: "github-label",
},
todoDock: config.TodoDock{
todoDockCfg: config.SourceConfig{
Enabled: true,
Period: period,
Label: "tododock-label",
Expand All @@ -73,8 +72,8 @@ func TestGetEnabledSourcesAndLabels(t *testing.T) {
for _, tc := range tt {
t.Run(tc.name, func(t *testing.T) {
cfg := config.Sources{
GithubIssues: tc.githubIssues,
TodoDock: tc.todoDock,
GithubIssues: config.GithubIssues{SourceConfig: tc.githubIssuesCfg},
TodoDock: config.TodoDock{SourceConfig: tc.todoDockCfg},
}

sources, labels := getEnabledSourcesAndLabels(ctx, cfg)
Expand Down Expand Up @@ -230,15 +229,14 @@ func TestShouldQuery(t *testing.T) {

for _, tc := range tt {
t.Run(tc.name, func(t *testing.T) {
cfg := config.TodoDock{
cfg := config.SourceConfig{
Enabled: true,
Period: config.Period{
Type: tc.pType,
Interval: tc.pInterval,
},
}
src := tododock.GetSource(cfg)
ok, err := shouldQuery(src, tc.date)
ok, err := shouldQuery(cfg, tc.date)

if err != nil || tc.err != nil {
if err == nil || tc.err == nil || err.Error() != tc.err.Error() {
Expand Down
50 changes: 28 additions & 22 deletions config.example.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,34 +6,40 @@ trello:
board_id: xxxxxxxxxxxxxxxxxxxxxxxx
list_id: xxxxxxxxxxxxxxxxxxxxxxxx

telegram:
enabled: true
token: xxxxxxxxxx:xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
chat_id: 1234567890

sources:
github_issues:
enabled: true
strict: true
period:
type: minute
interval: 15
personal_access_token: xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
label_id: xxxxxxxxxxxxxxxxxxxxxxxx
source_config:
name: "Github Issues"
enabled: true
strict: true
label_id: xxxxxxxxxxxxxxxxxxxxxxxx
period:
type: minute
interval: 15

tododock:
enabled: true
strict: false
period:
type: hour
interval: 1
email: abc@def.com
password: xxxxxxxx
label_id: xxxxxxxxxxxxxxxxxxxxxxxx
source_config:
name: "TodoDock"
enabled: true
strict: false
label_id: xxxxxxxxxxxxxxxxxxxxxxxx
period:
type: hour
interval: 1

habits:
enabled: true
strict: true
period:
type: hour
interval: 12

telegram:
enabled: true
token: xxxxxxxxxx:xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
chat_id: 1234567890
source_config:
name: "Google Spreadsheet Habits"
enabled: true
strict: true
period:
type: hour
interval: 12
23 changes: 11 additions & 12 deletions internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,28 +19,27 @@ type Period struct {
Interval int `yaml:"interval"`
}

type GithubIssues struct {
type SourceConfig struct {
Name string `yaml:"name"`
Enabled bool `yaml:"enabled"`
Strict bool `yaml:"strict"`
Label string `yaml:"label_id"`
Period Period `yaml:"period"`
Token string `yaml:"personal_access_token"`
}

type GithubIssues struct {
SourceConfig SourceConfig `yaml:"source_config"`
Token string `yaml:"personal_access_token"`
}

type TodoDock struct {
Enabled bool `yaml:"enabled"`
Strict bool `yaml:"strict"`
Label string `yaml:"label_id"`
Period Period `yaml:"period"`
Email string `yaml:"email"`
Password string `yaml:"password"`
SourceConfig SourceConfig `yaml:"source_config"`
Email string `yaml:"email"`
Password string `yaml:"password"`
}

type Habits struct {
Enabled bool `yaml:"enabled"`
Strict bool `yaml:"strict"`
Label string `yaml:"label_id"`
Period Period `yaml:"period"`
SourceConfig SourceConfig `yaml:"source_config"`
}

type Sources struct {
Expand Down
22 changes: 1 addition & 21 deletions internal/github/github.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,33 +24,13 @@ func GetSource(ctx context.Context, cfg config.GithubIssues) GithubIssuesSource
return GithubIssuesSource{client, ctx, cfg}
}

func (g GithubIssuesSource) IsEnabled() bool {
return g.cfg.Enabled
}

func (g GithubIssuesSource) IsStrict() bool {
return g.cfg.Strict
}

func (g GithubIssuesSource) GetName() string {
return "Github Issues"
}

func (g GithubIssuesSource) GetLabel() string {
return g.cfg.Label
}

func (g GithubIssuesSource) GetPeriod() config.Period {
return g.cfg.Period
}

func (g GithubIssuesSource) FetchNewCards() ([]trello.Card, error) {
issues, _, err := g.client.Issues.List(g.ctx, false, nil)
if err != nil {
return nil, fmt.Errorf("could not retrieve issues: %w", err)
}

return toCards(issues, g.cfg.Label)
return toCards(issues, g.cfg.SourceConfig.Label)
}

// toCards converts a list of issues into a list of trello card
Expand Down
20 changes: 0 additions & 20 deletions internal/habits/habits.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,26 +13,6 @@ func GetSource(cfg config.Habits) HabitsSource {
return HabitsSource{cfg}
}

func (h HabitsSource) IsEnabled() bool {
return h.cfg.Enabled
}

func (h HabitsSource) IsStrict() bool {
return h.cfg.Strict
}

func (h HabitsSource) GetName() string {
return "Google Spreadsheet Habits"
}

func (h HabitsSource) GetLabel() string {
return h.cfg.Label
}

func (h HabitsSource) GetPeriod() config.Period {
return h.cfg.Period
}

func (h HabitsSource) FetchNewCards() ([]trello.Card, error) {
// @todo: implement
return []trello.Card{}, nil
Expand Down
Loading

0 comments on commit 53feef9

Please sign in to comment.