providers.go 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. // Package providers builds the store clients selected by a store name.
  2. //
  3. // It is shared by the CLI (cmd/cheapo) and the MCP server so both select
  4. // stores and regional settings the same way.
  5. package providers
  6. import (
  7. "fmt"
  8. "gogs.netdome.biz/paul/cheapo/internal/epic"
  9. "gogs.netdome.biz/paul/cheapo/internal/game"
  10. "gogs.netdome.biz/paul/cheapo/internal/gog"
  11. "gogs.netdome.biz/paul/cheapo/internal/steam"
  12. )
  13. // Names lists the accepted values of Options.Store.
  14. var Names = []string{"epic", "steam", "gog", "all"}
  15. // Options selects stores and regional settings.
  16. type Options struct {
  17. Store string // epic, steam, gog or all; empty means all
  18. Country string // ISO country code for Epic, empty keeps the client default
  19. Locale string // store locale for Epic, empty keeps the client default
  20. }
  21. // New returns the providers selected by opts.Store.
  22. func New(opts Options) ([]game.Provider, error) {
  23. newEpic := func() *epic.Client {
  24. c := epic.NewClient()
  25. if opts.Country != "" {
  26. c.Country = opts.Country
  27. }
  28. if opts.Locale != "" {
  29. c.Locale = opts.Locale
  30. }
  31. return c
  32. }
  33. switch opts.Store {
  34. case "epic":
  35. return []game.Provider{newEpic()}, nil
  36. case "steam":
  37. return []game.Provider{steam.NewClient()}, nil
  38. case "gog":
  39. return []game.Provider{gog.NewClient()}, nil
  40. case "", "all":
  41. return []game.Provider{newEpic(), steam.NewClient(), gog.NewClient()}, nil
  42. default:
  43. return nil, fmt.Errorf("unknown store %q (epic, steam, gog, all)", opts.Store)
  44. }
  45. }