Skip to content

Commit

Permalink
Allow diff file path to be customised
Browse files Browse the repository at this point in the history
  • Loading branch information
RyanConnell committed Dec 31, 2023
1 parent b4f3d6e commit aa22838
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 9 deletions.
11 changes: 7 additions & 4 deletions cmd/watcher/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ type flags struct {
ticketmasterConfigFile string
discordWebhookURL string
diffMode bool
diffFile string
}

func main() {
Expand All @@ -38,7 +39,8 @@ func main() {
}

reader := ticketmaster.NewReader(f.apiKey)
watcher := watcher.NewWatcher(reader, f.ticketmasterConfigFile, artists, f.discordWebhookURL)
watcher := watcher.NewWatcher(reader, f.ticketmasterConfigFile, artists,
f.discordWebhookURL, f.diffFile)
events, err := watcher.FindEvents(f.diffMode)
if err != nil {
log.Fatalf("Error retrieving events: %v", err)
Expand Down Expand Up @@ -68,9 +70,10 @@ func parseFlags() *flags {
flag.StringVar(&f.discordWebhookURL, "discordWebhookURL", "", "Discord webhook URL")
flag.StringVar(&f.ticketmasterConfigFile, "ticketmasterConfig", "ticketmaster.yaml",
"Path to a file containing search criteria for ticketmaster event lookups")
flag.BoolVar(&f.diffMode, "diff", false, fmt.Sprintf("Run in diff mode which only notifies "+
"for events we haven't already seen. Notification history is stored in the %q file "+
"between runs", watcher.DIFF_FILE))
flag.BoolVar(&f.diffMode, "diff", false, "Run in diff mode which only notifies "+
"for events we haven't already seen.")
flag.StringVar(&f.diffFile, "diffFile", "previous-ids", "Path to a file that stores the list "+
"of events that we have previously sent notifications for")
flag.Parse()
return f
}
Expand Down
10 changes: 5 additions & 5 deletions internal/watcher/watcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,22 +13,22 @@ import (
"github.com/RyanConnell/concert-watcher/pkg/ticketmaster"
)

const DIFF_FILE = "previous-event-ids"

type Watcher struct {
ticketmasterAPI ticketmaster.API
ticketmasterConfigFile string
trackedArtists *set.Set[string]
discordWebhookURL string
diffFile string
}

func NewWatcher(ticketmasterAPI ticketmaster.API, ticketmasterConfigFile string,
trackedArtists []string, discordWebhookURL string) *Watcher {
trackedArtists []string, discordWebhookURL string, diffFile string) *Watcher {
return &Watcher{
ticketmasterAPI: ticketmasterAPI,
ticketmasterConfigFile: ticketmasterConfigFile,
trackedArtists: set.New(trackedArtists...),
discordWebhookURL: discordWebhookURL,
diffFile: diffFile,
}
}

Expand Down Expand Up @@ -126,7 +126,7 @@ func (w *Watcher) Notify(events []*ticketmaster.Event) error {

// Query for a list of events that we've previously notiied on.
func (w *Watcher) previousEventIDs() ([]string, error) {
file, err := os.Open(DIFF_FILE)
file, err := os.Open(w.diffFile)
if errors.Is(err, fs.ErrNotExist) {
return nil, nil
}
Expand All @@ -145,7 +145,7 @@ func (w *Watcher) previousEventIDs() ([]string, error) {

// Save seen event IDs so that we don't send duplicate notifications each time we run.
func (w *Watcher) saveEventIDs(ids []string) error {
file, err := os.Create(DIFF_FILE)
file, err := os.Create(w.diffFile)
if err != nil {
return err
}
Expand Down

0 comments on commit aa22838

Please sign in to comment.