From 6f68c3c57c1df75b7c2fb808050f50c2588db48a Mon Sep 17 00:00:00 2001 From: Utku Ufuk Date: Wed, 29 Sep 2021 21:20:03 +0300 Subject: [PATCH] Get rid of hardcoded source keys from configuration (#44) --- README.md | 2 +- cmd/entrello/main.go | 8 -------- cmd/entrello/source.go | 12 +++--------- config.example.yml | 9 +++------ internal/config/config.go | 8 +------- pkg/trello/trello_test.go | 2 +- 6 files changed, 9 insertions(+), 32 deletions(-) diff --git a/README.md b/README.md index 9114820..2a8e45c 100644 --- a/README.md +++ b/README.md @@ -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 ``` diff --git a/cmd/entrello/main.go b/cmd/entrello/main.go index 67c5eaf..954fb1f 100644 --- a/cmd/entrello/main.go +++ b/cmd/entrello/main.go @@ -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 { diff --git a/cmd/entrello/source.go b/cmd/entrello/source.go index 309c3bd..c6f6442 100644 --- a/cmd/entrello/source.go +++ b/cmd/entrello/source.go @@ -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) diff --git a/config.example.yml b/config.example.yml index bf6110e..a19f597 100644 --- a/config.example.yml +++ b/config.example.yml @@ -11,8 +11,7 @@ telegram: chat_id: 1234567890 sources: - github_issues: - name: "Github Issues" + - name: "Github Issues" endpoint: https://:/entrello strict: true label_id: xxxxxxxxxxxxxxxxxxxxxxxx @@ -21,8 +20,7 @@ sources: type: minute interval: 15 - tododock: - name: "TodoDock" + - name: "TodoDock" endpoint: https://:/entrello strict: false label_id: xxxxxxxxxxxxxxxxxxxxxxxx @@ -31,8 +29,7 @@ sources: type: hour interval: 1 - habits: - name: "Google Spreadsheet Habits" + - name: "Google Spreadsheet Habits" endpoint: https://:/entrello strict: true label_id: xxxxxxxxxxxxxxxxxxxxxxxx diff --git a/internal/config/config.go b/internal/config/config.go index 0c5567c..0837d44 100644 --- a/internal/config/config.go +++ b/internal/config/config.go @@ -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"` @@ -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"` } diff --git a/pkg/trello/trello_test.go b/pkg/trello/trello_test.go index a775974..2f54ea3 100644 --- a/pkg/trello/trello_test.go +++ b/pkg/trello/trello_test.go @@ -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 {