Skip to content

Commit

Permalink
Move country config to top level
Browse files Browse the repository at this point in the history
  • Loading branch information
ahobsonsayers committed Sep 27, 2024
1 parent 64de554 commit 73f062f
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 14 deletions.
16 changes: 8 additions & 8 deletions cmd/twitchets/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
}
Expand All @@ -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)
Expand Down Expand Up @@ -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
Expand Down
6 changes: 3 additions & 3 deletions cmd/twitchets/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
3 changes: 1 addition & 2 deletions cmd/twitchets/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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.")
}
Expand Down
2 changes: 1 addition & 1 deletion test/assets/config/config.yaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
apiKey: test
country: GB

global:
country: GB
regions:
- GBLO
- GBNW
Expand Down

0 comments on commit 73f062f

Please sign in to comment.