config.go 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. package config
  2. import (
  3. "log/slog"
  4. "os"
  5. "path/filepath"
  6. "time"
  7. "github.com/BurntSushi/toml"
  8. )
  9. const (
  10. Name = "discordo"
  11. Browser = "Chrome"
  12. BrowserVersion = "132.0.0.0"
  13. UserAgent = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) " + Browser + "/" + BrowserVersion + " Safari/537.36"
  14. )
  15. type Config struct {
  16. Mouse bool `toml:"mouse"`
  17. HideBlockedUsers bool `toml:"hide_blocked_users"`
  18. Timestamps bool `toml:"timestamps"`
  19. ShowAttachmentLinks bool `toml:"show_attachment_links"`
  20. MessagesLimit uint8 `toml:"messages_limit"`
  21. Editor string `toml:"editor"`
  22. Browser string `toml:"browser"`
  23. BrowserVersion string `toml:"browser_version"`
  24. UserAgent string `toml:"user_agent"`
  25. TimestampsFormat string `toml:"timestamps_format"`
  26. Keys Keys `toml:"keys"`
  27. Theme Theme `toml:"theme"`
  28. }
  29. func defaultConfig() *Config {
  30. return &Config{
  31. Mouse: true,
  32. HideBlockedUsers: true,
  33. Timestamps: false,
  34. ShowAttachmentLinks: true,
  35. MessagesLimit: 50,
  36. Editor: "default",
  37. TimestampsFormat: time.Kitchen,
  38. Browser: Browser,
  39. BrowserVersion: BrowserVersion,
  40. UserAgent: UserAgent,
  41. Keys: defaultKeys(),
  42. Theme: defaultTheme(),
  43. }
  44. }
  45. // Reads the configuration file and parses it.
  46. func Load() (*Config, error) {
  47. path, err := os.UserConfigDir()
  48. if err != nil {
  49. slog.Info("user configuration directory path cannot be determined; falling back to the current directory path")
  50. path = "."
  51. }
  52. path = filepath.Join(path, Name, "config.toml")
  53. f, err := os.Open(path)
  54. cfg := defaultConfig()
  55. if os.IsNotExist(err) {
  56. slog.Info("the configuration file does not exist, falling back to the default configuration", "path", path, "err", err)
  57. return cfg, nil
  58. }
  59. if err != nil {
  60. return nil, err
  61. }
  62. defer f.Close()
  63. if _, err := toml.NewDecoder(f).Decode(&cfg); err != nil {
  64. return nil, err
  65. }
  66. return cfg, nil
  67. }