diff --git a/cmd/twitchets/config.go b/cmd/twitchets/config.go index 85a7162..dfbc01c 100644 --- a/cmd/twitchets/config.go +++ b/cmd/twitchets/config.go @@ -13,6 +13,7 @@ import ( type Config struct { APIKey string `json:"apiKey"` + Country twickets.Country `json:"country"` GlobalConfig GlobalEventConfig `json:"global"` TicketsConfig []TicketConfig `json:"tickets"` } @@ -22,6 +23,13 @@ func (c Config) Validate() error { return errors.New("api key must be set") } + if c.Country.Value == "" { + return errors.New("country must be set") + } + if !twickets.Countries.Contains(c.Country) { + return fmt.Errorf("country '%s' is not valid", c.Country) + } + err := c.GlobalConfig.Validate() if err != nil { return fmt.Errorf("global config is not valid: %w", err) @@ -75,20 +83,12 @@ func (c Config) Filters() []twickets.Filter { // unless an event explicitly overwrites its. // Country is required. type GlobalEventConfig struct { - Country twickets.Country `json:"country"` Regions []twickets.Region `json:"regions"` NumTickets int `json:"numTickets"` Discount float64 `json:"discount"` } func (c GlobalEventConfig) Validate() error { - if c.Country.Value == "" { - return errors.New("country must be set") - } - if !twickets.Countries.Contains(c.Country) { - return fmt.Errorf("country '%s' is not valid", c.Country) - } - // Reuse the filter validation logic globalFilter := twickets.Filter{ Name: "global", // Name must be be set - this is arbitrary diff --git a/cmd/twitchets/config_test.go b/cmd/twitchets/config_test.go index d68c595..51bf710 100644 --- a/cmd/twitchets/config_test.go +++ b/cmd/twitchets/config_test.go @@ -14,15 +14,15 @@ func TestLoadConfig(t *testing.T) { actualConfig, err := LoadConfig(configPath) require.NoError(t, err) - globalCountry := twickets.CountryUnitedKingdom + country := twickets.CountryUnitedKingdom globalRegions := []twickets.Region{twickets.RegionLondon, twickets.RegionNorthWest} globalNumTickets := 2 globalDiscount := 25.0 expectedConfig := Config{ - APIKey: "test", + APIKey: "test", + Country: country, GlobalConfig: GlobalEventConfig{ - Country: globalCountry, Regions: globalRegions, NumTickets: globalNumTickets, Discount: globalDiscount, diff --git a/cmd/twitchets/main.go b/cmd/twitchets/main.go index 767a313..d6b741e 100644 --- a/cmd/twitchets/main.go +++ b/cmd/twitchets/main.go @@ -90,7 +90,7 @@ func fetchAndProcessTickets( twickets.FetchTicketsInput{ // Required APIKey: config.APIKey, - Country: config.GlobalConfig.Country, + Country: config.Country, // Optional CreatedBefore: time.Now(), CreatedAfter: lastCheckTime, @@ -101,7 +101,6 @@ func fetchAndProcessTickets( slog.Error(err.Error()) return } - if len(tickets) == maxNumTickets { slog.Warn("Fetched the max number of tickets allowed. It is possible tickets have been missed.") } diff --git a/test/assets/config/config.yaml b/test/assets/config/config.yaml index 8890216..d95da60 100644 --- a/test/assets/config/config.yaml +++ b/test/assets/config/config.yaml @@ -1,7 +1,7 @@ apiKey: test +country: GB global: - country: GB regions: - GBLO - GBNW