config.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. package config
  2. import (
  3. _ "embed"
  4. "log/slog"
  5. "os"
  6. "path/filepath"
  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. MarkdownEnabled bool `toml:"markdown_enabled"`
  39. Timestamps Timestamps `toml:"timestamps"`
  40. Identify Identify `toml:"identify"`
  41. Notifications Notifications `toml:"notifications"`
  42. Keys Keys `toml:"keys"`
  43. Theme Theme `toml:"theme"`
  44. }
  45. )
  46. //go:embed config.toml
  47. var defaultCfg []byte
  48. func DefaultPath() string {
  49. path, err := os.UserConfigDir()
  50. if err != nil {
  51. slog.Info("user configuration directory path cannot be determined; falling back to the current directory path")
  52. path = "."
  53. }
  54. return filepath.Join(path, consts.Name, fileName)
  55. }
  56. // Reads the configuration file and parses it.
  57. func Load(path string) (*Config, error) {
  58. f, err := os.Open(path)
  59. var cfg *Config
  60. if err := toml.Unmarshal(defaultCfg, &cfg); err != nil {
  61. return nil, err
  62. }
  63. if os.IsNotExist(err) {
  64. slog.Info("the configuration file does not exist, falling back to the default configuration", "path", path, "err", err)
  65. handleDefaults(cfg)
  66. return cfg, nil
  67. }
  68. if err != nil {
  69. return nil, err
  70. }
  71. defer f.Close()
  72. if _, err := toml.NewDecoder(f).Decode(&cfg); err != nil {
  73. return nil, err
  74. }
  75. handleDefaults(cfg)
  76. return cfg, nil
  77. }
  78. func handleDefaults(cfg *Config) {
  79. if cfg.Identify.Browser == "default" {
  80. cfg.Identify.Browser = consts.Browser
  81. }
  82. if cfg.Identify.BrowserVersion == "default" {
  83. cfg.Identify.BrowserVersion = consts.BrowserVersion
  84. }
  85. if cfg.Identify.UserAgent == "default" {
  86. cfg.Identify.UserAgent = consts.UserAgent
  87. }
  88. }