config.go 1.9 KB

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