config.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. package config
  2. import (
  3. "log/slog"
  4. "os"
  5. "path/filepath"
  6. "github.com/BurntSushi/toml"
  7. "github.com/ayn2op/discordo/internal/consts"
  8. "github.com/diamondburned/arikawa/v3/discord"
  9. )
  10. const fileName = "config.toml"
  11. type (
  12. Timestamps struct {
  13. Enabled bool `toml:"enabled"`
  14. Format string `toml:"format"`
  15. }
  16. Identify struct {
  17. Status discord.Status `toml:"status"`
  18. Browser string `toml:"browser"`
  19. BrowserVersion string `toml:"browser_version"`
  20. UserAgent string `toml:"user_agent"`
  21. }
  22. Notifications struct {
  23. Enabled bool `toml:"enabled"`
  24. Duration int `toml:"duration"`
  25. Sound Sound `toml:"sound"`
  26. }
  27. Sound struct {
  28. Enabled bool `toml:"enabled"`
  29. OnlyOnPing bool `toml:"only_on_ping"`
  30. }
  31. Config struct {
  32. Mouse bool `toml:"mouse"`
  33. Editor string `toml:"editor"`
  34. HideBlockedUsers bool `toml:"hide_blocked_users"`
  35. ShowAttachmentLinks bool `toml:"show_attachment_links"`
  36. MessagesLimit uint8 `toml:"messages_limit"`
  37. MarkdownEnabled bool `toml:"markdown_enabled"`
  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. // go:embed config.toml
  46. var defaultCfg []byte
  47. func DefaultPath() string {
  48. path, err := os.UserConfigDir()
  49. if err != nil {
  50. slog.Info("user configuration directory path cannot be determined; falling back to the current directory path")
  51. path = "."
  52. }
  53. return filepath.Join(path, consts.Name, fileName)
  54. }
  55. // Reads the configuration file and parses it.
  56. func Load(path string) (*Config, error) {
  57. f, err := os.Open(path)
  58. var cfg *Config
  59. if err := toml.Unmarshal(defaultCfg, &cfg); err != nil {
  60. return nil, err
  61. }
  62. if os.IsNotExist(err) {
  63. slog.Info("the configuration file does not exist, falling back to the default configuration", "path", path, "err", err)
  64. handleDefaults(cfg)
  65. return cfg, nil
  66. }
  67. if err != nil {
  68. return nil, err
  69. }
  70. defer f.Close()
  71. if _, err := toml.NewDecoder(f).Decode(&cfg); err != nil {
  72. return nil, err
  73. }
  74. handleDefaults(cfg)
  75. return cfg, nil
  76. }
  77. func handleDefaults(cfg *Config) {
  78. if cfg.Identify.Browser == "default" {
  79. cfg.Identify.Browser = consts.Browser
  80. }
  81. if cfg.Identify.BrowserVersion == "default" {
  82. cfg.Identify.BrowserVersion = consts.BrowserVersion
  83. }
  84. if cfg.Identify.UserAgent == "default" {
  85. cfg.Identify.UserAgent = consts.UserAgent
  86. }
  87. }