config.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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(
  52. "user configuration directory path cannot be determined; falling back to the current directory path",
  53. )
  54. path = "."
  55. }
  56. return filepath.Join(path, consts.Name, fileName)
  57. }
  58. // Reads the configuration file and parses it.
  59. func Load(path string) (*Config, error) {
  60. f, err := os.Open(path)
  61. var cfg *Config
  62. if err := toml.Unmarshal(defaultCfg, &cfg); err != nil {
  63. return nil, err
  64. }
  65. if os.IsNotExist(err) {
  66. slog.Info(
  67. "the configuration file does not exist, falling back to the default configuration",
  68. "path",
  69. path,
  70. "err",
  71. err,
  72. )
  73. handleDefaults(cfg)
  74. return cfg, nil
  75. }
  76. if err != nil {
  77. return nil, err
  78. }
  79. defer f.Close()
  80. if _, err := toml.NewDecoder(f).Decode(&cfg); err != nil {
  81. return nil, err
  82. }
  83. handleDefaults(cfg)
  84. return cfg, nil
  85. }
  86. func handleDefaults(cfg *Config) {
  87. if cfg.Identify.Browser == "default" {
  88. cfg.Identify.Browser = consts.Browser
  89. }
  90. if cfg.Identify.BrowserVersion == "default" {
  91. cfg.Identify.BrowserVersion = consts.BrowserVersion
  92. }
  93. if cfg.Identify.UserAgent == "default" {
  94. cfg.Identify.UserAgent = consts.UserAgent
  95. }
  96. }