Skip to content

Commit

Permalink
Use config in program
Browse files Browse the repository at this point in the history
  • Loading branch information
ahobsonsayers committed Sep 13, 2024
1 parent 840e14e commit dd855f7
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 58 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,5 @@ go.work

# Local go mod vendor ignore
vendor/

.aider*
4 changes: 3 additions & 1 deletion .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,15 @@
"type": "go",
"request": "launch",
"mode": "auto",
"program": "${workspaceFolder}/main.go"
"cwd": "${workspaceFolder}",
"program": "${workspaceFolder}/cmd/twitchets"
},
{
"name": "Scratch",
"type": "go",
"request": "launch",
"mode": "auto",
"cwd": "${workspaceFolder}",
"program": "${workspaceFolder}/scratch/scratch.go"
}
]
Expand Down
2 changes: 1 addition & 1 deletion Taskfile.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ tasks:

run:
cmds:
- go run .
- go run ./cmd/twitchets

run:docker:
deps: [build:docker]
Expand Down
9 changes: 9 additions & 0 deletions cmd/twitchets/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,15 @@ type Config struct {
Events []EventConfig `koanf:"events"`
}

func (c Config) EventNames() []string {
eventNames := make([]string, 0, len(c.Events))
for _, event := range c.Events {
eventName := event.Name
eventNames = append(eventNames, eventName)
}
return eventNames
}

func (c *Config) parseKoanf(k *koanf.Koanf) error {
if k == nil {
return nil
Expand Down
73 changes: 17 additions & 56 deletions cmd/twitchets/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import (
"fmt"
"log"
"log/slog"
"os"
"path/filepath"
"strings"
"time"

Expand All @@ -18,67 +20,25 @@ const (
refetchTime = 1 * time.Minute
)

var (
// Config variables
// NOTE:
// Region coeds are currently ignored due to
// issues with the twickets filter api
countryCode = "GB"
regionCodes = []string{"GBLO"} // TODO reenable. See note in config variables.
monitoredEventNames = []string{
// Theatre
"Back to the Future",
"Frozen",
"Hadestown",
"Hamilton",
"Harry Potter & the Cursed Child",
"Kiss Me Kate",
"Lion King",
"Matilda",
"Mean Girls",
"Moulin Rouge",
"My Neighbour Totoro",
"Operation Mincemeat",
"Starlight Express",
"Stranger Things",
"The Phantom Opera",
"The Wizard of Oz",
// Gigs
"Coldplay",
"Gary Clark Jr.",
"Glass Animals",
"Jungle",
"Oasis",
"Taylor Swift",
}

// Package variables
country twickets.Country
regions []twickets.Region
lastCheckTime = time.Now()
)
var lastCheckTime = time.Now()

func init() {
_ = godotenv.Load()
}

func main() {
// Twickets client
parsedCountryCode := twickets.Countries.Parse(countryCode)
if parsedCountryCode == nil {
log.Fatalf("'%s' is not a valid country code", parsedCountryCode)
cwd, err := os.Getwd()
if err != nil {
log.Fatalf("failed to get working directory:, %v", err)
}
country = *parsedCountryCode

regions = make([]twickets.Region, 0, len(regionCodes))
for _, regionCode := range regionCodes {
parsedRegionCode := twickets.Regions.Parse(regionCode)
if parsedRegionCode == nil {
log.Fatalf("'%s' is not a valid region code", parsedRegionCode)
}
regions = append(regions, *parsedRegionCode)
configPath := filepath.Join(cwd, "config.yaml")
config, err := LoadConfig(configPath)
if err != nil {
log.Fatalf("config error:, %v", err)
}

// Twickets client
twicketsClient := twickets.NewClient(nil)

// Notification Client
Expand All @@ -88,11 +48,11 @@ func main() {
}

slog.Info(
fmt.Sprintf("Monitoring: %s", strings.Join(monitoredEventNames, ", ")),
fmt.Sprintf("Monitoring: %s", strings.Join(config.EventNames(), ", ")),
)

// Initial execution
fetchAndProcessTickets(twicketsClient, notificationClient)
fetchAndProcessTickets(config, twicketsClient, notificationClient)

// Create ticker
ticker := time.NewTicker(refetchTime)
Expand All @@ -103,14 +63,15 @@ func main() {
for {
select {
case <-ticker.C:
fetchAndProcessTickets(twicketsClient, notificationClient)
fetchAndProcessTickets(config, twicketsClient, notificationClient)
case <-exitChan:
return
}
}
}

func fetchAndProcessTickets(
config Config,
twicketsClient *twickets.Client,
notificationClient notification.Client,
) {
Expand All @@ -122,7 +83,7 @@ func fetchAndProcessTickets(
tickets, err := twicketsClient.FetchTickets(
context.Background(),
twickets.FetchTicketsInput{
Country: country,
Country: config.Country,
// Regions: regions, // TODO reenable. See note in config variables.
CreatedBefore: time.Now(),
CreatedAfter: lastCheckTime,
Expand All @@ -140,7 +101,7 @@ func fetchAndProcessTickets(

filteredTickets := tickets.Filter(
twickets.TicketFilter{
EventNames: monitoredEventNames,
EventNames: config.EventNames(),
},
)

Expand Down
27 changes: 27 additions & 0 deletions config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
country: GB
regions: GBLO # TODO Currently ignored.
events:
# Theatre
- name: Back to the Future
- name: Frozen
- name: Hadestown
- name: Hamilton
- name: Harry Potter & the Cursed Child
- name: Kiss Me Kate
- name: Lion King
- name: Matilda
- name: Mean Girls
- name: Moulin Rouge
- name: My Neighbour Totoro
- name: Operation Mincemeat
- name: Starlight Express
- name: Stranger Things
- name: The Phantom Opera
- name: The Wizard of Oz
# Gigs
- name: Coldplay
- name: Gary Clark Jr.
- name: Glass Animals
- name: Jungle
- name: Oasis
- name: Taylor Swift

0 comments on commit dd855f7

Please sign in to comment.