config.go 1.8 KB

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