config.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. package config
  2. import (
  3. _ "embed"
  4. "fmt"
  5. "log/slog"
  6. "os"
  7. "path/filepath"
  8. "github.com/BurntSushi/toml"
  9. "github.com/ayn2op/discordo/internal/consts"
  10. "github.com/diamondburned/arikawa/v3/discord"
  11. )
  12. const fileName = "config.toml"
  13. type (
  14. Timestamps struct {
  15. Enabled bool `toml:"enabled"`
  16. Format string `toml:"format"`
  17. }
  18. Identify struct {
  19. Status discord.Status `toml:"status"`
  20. Browser string `toml:"browser"`
  21. BrowserVersion string `toml:"browser_version"`
  22. UserAgent string `toml:"user_agent"`
  23. }
  24. Notifications struct {
  25. Enabled bool `toml:"enabled"`
  26. Duration int `toml:"duration"`
  27. Sound Sound `toml:"sound"`
  28. }
  29. Sound struct {
  30. Enabled bool `toml:"enabled"`
  31. OnlyOnPing bool `toml:"only_on_ping"`
  32. }
  33. Config struct {
  34. Mouse bool `toml:"mouse"`
  35. Editor string `toml:"editor"`
  36. Markdown bool `toml:"markdown"`
  37. HideBlockedUsers bool `toml:"hide_blocked_users"`
  38. ShowAttachmentLinks bool `toml:"show_attachment_links"`
  39. MessagesLimit uint8 `toml:"messages_limit"`
  40. Timestamps Timestamps `toml:"timestamps"`
  41. Identify Identify `toml:"identify"`
  42. Notifications Notifications `toml:"notifications"`
  43. Keys Keys `toml:"keys"`
  44. Theme Theme `toml:"theme"`
  45. }
  46. )
  47. //go:embed config.toml
  48. var defaultCfg []byte
  49. func DefaultPath() string {
  50. path, err := os.UserConfigDir()
  51. if err != nil {
  52. slog.Info(
  53. "user configuration directory path cannot be determined; falling back to the current directory path",
  54. )
  55. path = "."
  56. }
  57. return filepath.Join(path, consts.Name, fileName)
  58. }
  59. // Load reads the configuration file and parses it.
  60. func Load(path string) (*Config, error) {
  61. file, err := os.Open(path)
  62. var cfg *Config
  63. if err := toml.Unmarshal(defaultCfg, &cfg); err != nil {
  64. return nil, fmt.Errorf("failed to unmarshal default config: %w", err)
  65. }
  66. if os.IsNotExist(err) {
  67. slog.Info(
  68. "the configuration file does not exist, falling back to the default configuration",
  69. "path",
  70. path,
  71. "err",
  72. err,
  73. )
  74. handleDefaults(cfg)
  75. return cfg, nil
  76. }
  77. if err != nil {
  78. return nil, fmt.Errorf("failed to open config file: %w", err)
  79. }
  80. defer file.Close()
  81. if _, err := toml.NewDecoder(file).Decode(&cfg); err != nil {
  82. return nil, fmt.Errorf("failed to decode config: %w", err)
  83. }
  84. handleDefaults(cfg)
  85. return cfg, nil
  86. }
  87. func handleDefaults(cfg *Config) {
  88. if cfg.Editor == "default" {
  89. cfg.Editor = os.Getenv("EDITOR")
  90. }
  91. if cfg.Identify.Browser == "default" {
  92. cfg.Identify.Browser = consts.Browser
  93. }
  94. if cfg.Identify.BrowserVersion == "default" {
  95. cfg.Identify.BrowserVersion = consts.BrowserVersion
  96. }
  97. if cfg.Identify.UserAgent == "default" {
  98. cfg.Identify.UserAgent = consts.UserAgent
  99. }
  100. }