config.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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. const fileName = "config.toml"
  12. type (
  13. Timestamps struct {
  14. Enabled bool `toml:"enabled"`
  15. Format string `toml:"format"`
  16. }
  17. Identify struct {
  18. Status discord.Status `toml:"status"`
  19. Browser string `toml:"browser"`
  20. BrowserVersion string `toml:"browser_version"`
  21. UserAgent string `toml:"user_agent"`
  22. }
  23. Notifications struct {
  24. Enabled bool `toml:"enabled"`
  25. Duration int `toml:"duration"`
  26. Sound Sound `toml:"sound"`
  27. }
  28. Sound struct {
  29. Enabled bool `toml:"enabled"`
  30. OnlyOnPing bool `toml:"only_on_ping"`
  31. }
  32. Config struct {
  33. Mouse bool `toml:"mouse"`
  34. Editor string `toml:"editor"`
  35. HideBlockedUsers bool `toml:"hide_blocked_users"`
  36. ShowAttachmentLinks bool `toml:"show_attachment_links"`
  37. MessagesLimit uint8 `toml:"messages_limit"`
  38. Timestamps Timestamps `toml:"timestamps"`
  39. Identify Identify `toml:"identify"`
  40. Notifications Notifications `toml:"notifications"`
  41. Keys Keys `toml:"keys"`
  42. Theme Theme `toml:"theme"`
  43. }
  44. )
  45. func defaultConfig() *Config {
  46. return &Config{
  47. Mouse: true,
  48. Editor: "default",
  49. HideBlockedUsers: true,
  50. ShowAttachmentLinks: true,
  51. MessagesLimit: 50,
  52. Timestamps: Timestamps{
  53. Enabled: false,
  54. Format: time.Kitchen,
  55. },
  56. Identify: Identify{
  57. Status: discord.OnlineStatus,
  58. Browser: consts.Browser,
  59. BrowserVersion: consts.BrowserVersion,
  60. UserAgent: consts.UserAgent,
  61. },
  62. Notifications: Notifications{
  63. Enabled: true,
  64. Duration: 500,
  65. Sound: Sound{
  66. Enabled: true,
  67. OnlyOnPing: true,
  68. },
  69. },
  70. Keys: defaultKeys(),
  71. Theme: defaultTheme(),
  72. }
  73. }
  74. // Reads the configuration file and parses it.
  75. func Load() (*Config, error) {
  76. path, err := os.UserConfigDir()
  77. if err != nil {
  78. slog.Info("user configuration directory path cannot be determined; falling back to the current directory path")
  79. path = "."
  80. }
  81. path = filepath.Join(path, consts.Name, fileName)
  82. f, err := os.Open(path)
  83. cfg := defaultConfig()
  84. if os.IsNotExist(err) {
  85. slog.Info("the configuration file does not exist, falling back to the default configuration", "path", path, "err", err)
  86. return cfg, nil
  87. }
  88. if err != nil {
  89. return nil, err
  90. }
  91. defer f.Close()
  92. if _, err := toml.NewDecoder(f).Decode(&cfg); err != nil {
  93. return nil, err
  94. }
  95. return cfg, nil
  96. }