Skip to content

Commit

Permalink
Get rid of hardcoded source keys from configuration (#44)
Browse files Browse the repository at this point in the history
  • Loading branch information
utkuufuk authored Sep 29, 2021
1 parent a6f163f commit 6f68c3c
Show file tree
Hide file tree
Showing 6 changed files with 9 additions and 32 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ An example use case could be to create a Trello card for each GitHub issue that'
Copy and rename `config.example.yml` as `config.yml` (default), then set your own values in `config.yml`.

You can also use a non-default config file path using the `-c` flag:
```console
```sh
go run ./cmd/entrello -c /path/to/config/file
```

Expand Down
8 changes: 0 additions & 8 deletions cmd/entrello/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,44 +16,36 @@ var (
)

func main() {
// read configuration file path
var configFile string
flag.StringVar(&configFile, "c", "config.yml", "config file path")
flag.Parse()

// read config params
cfg, err := config.ReadConfig(configFile)
if err != nil {
log.Fatalf("Could not read configuration: %v", err)
}

// get a system logger instance
logger = syslog.NewLogger(cfg.Telegram)

// get current time for the configured location
loc, err := time.LoadLocation(cfg.TimezoneLocation)
if err != nil {
logger.Fatalf("Invalid timezone location: %v", loc)
}

// get a list of sources and the corresponding labels for each source
sources, labels := getSources(cfg.Sources, time.Now().In(loc))
if len(sources) == 0 {
return
}

// initialize the Trello client
client, err := trello.NewClient(cfg.Trello)
if err != nil {
logger.Fatalf("Could not create trello client: %v", err)
}

// load Trello cards from the board with relevant labels
if err := client.LoadBoard(labels); err != nil {
logger.Fatalf("Could not load existing cards from the board: %v", err)
}

// fetch new cards from each source and handle the new & stale ones
var wg sync.WaitGroup
wg.Add(len(sources))
for _, src := range sources {
Expand Down
12 changes: 3 additions & 9 deletions cmd/entrello/source.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,9 @@ import (
"github.com/utkuufuk/entrello/pkg/trello"
)

// getSources returns a slice of sources & their labels as a separate slice
func getSources(cfg config.Sources, now time.Time) (sources []config.Source, labels []string) {
arr := []config.Source{
cfg.GithubIssues,
cfg.TodoDock,
cfg.Habits,
}

for _, src := range arr {
// getSources returns a slice of sources & all source labels as a separate slice
func getSources(srcArr []config.Source, now time.Time) (sources []config.Source, labels []string) {
for _, src := range srcArr {
if ok, err := shouldQuery(src, now); !ok {
if err != nil {
logger.Errorf("could not check if '%s' should be queried or not, skipping", src.Name)
Expand Down
9 changes: 3 additions & 6 deletions config.example.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@ telegram:
chat_id: 1234567890

sources:
github_issues:
name: "Github Issues"
- name: "Github Issues"
endpoint: https://<domain>:<port>/entrello
strict: true
label_id: xxxxxxxxxxxxxxxxxxxxxxxx
Expand All @@ -21,8 +20,7 @@ sources:
type: minute
interval: 15

tododock:
name: "TodoDock"
- name: "TodoDock"
endpoint: https://<domain>:<port>/entrello
strict: false
label_id: xxxxxxxxxxxxxxxxxxxxxxxx
Expand All @@ -31,8 +29,7 @@ sources:
type: hour
interval: 1

habits:
name: "Google Spreadsheet Habits"
- name: "Google Spreadsheet Habits"
endpoint: https://<domain>:<port>/entrello
strict: true
label_id: xxxxxxxxxxxxxxxxxxxxxxxx
Expand Down
8 changes: 1 addition & 7 deletions internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,6 @@ type Source struct {
Period Period `yaml:"period"`
}

type Sources struct {
GithubIssues Source `yaml:"github_issues"`
TodoDock Source `yaml:"tododock"`
Habits Source `yaml:"habits"`
}

type Trello struct {
ApiKey string `yaml:"api_key"`
ApiToken string `yaml:"api_token"`
Expand All @@ -49,7 +43,7 @@ type Telegram struct {
type Config struct {
TimezoneLocation string `yaml:"timezone_location"`
Trello Trello `yaml:"trello"`
Sources Sources `yaml:"sources"`
Sources []Source `yaml:"sources"`
Telegram Telegram `yaml:"telegram"`
}

Expand Down
2 changes: 1 addition & 1 deletion pkg/trello/trello_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ func TestNewClient(t *testing.T) {
ApiToken: tc.apiToken,
BoardId: tc.boardId,
},
Sources: config.Sources{},
Sources: []config.Source{},
}
_, err := NewClient(cfg.Trello)
if (err != nil && !tc.err) || err == nil && tc.err {
Expand Down

0 comments on commit 6f68c3c

Please sign in to comment.