config.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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. // Use 0 to disable
  40. AutocompleteLimit uint8 `toml:"autocomplete_limit"`
  41. MessagesLimit uint8 `toml:"messages_limit"`
  42. Timestamps Timestamps `toml:"timestamps"`
  43. Identify Identify `toml:"identify"`
  44. Notifications Notifications `toml:"notifications"`
  45. Keys Keys `toml:"keys"`
  46. Theme Theme `toml:"theme"`
  47. }
  48. )
  49. //go:embed config.toml
  50. var defaultCfg []byte
  51. func DefaultPath() string {
  52. path, err := os.UserConfigDir()
  53. if err != nil {
  54. slog.Info(
  55. "user configuration directory path cannot be determined; falling back to the current directory path",
  56. )
  57. path = "."
  58. }
  59. return filepath.Join(path, consts.Name, fileName)
  60. }
  61. // Load reads the configuration file and parses it.
  62. func Load(path string) (*Config, error) {
  63. file, err := os.Open(path)
  64. var cfg *Config
  65. if err := toml.Unmarshal(defaultCfg, &cfg); err != nil {
  66. return nil, fmt.Errorf("failed to unmarshal default config: %w", err)
  67. }
  68. if os.IsNotExist(err) {
  69. slog.Info(
  70. "the configuration file does not exist, falling back to the default configuration",
  71. "path",
  72. path,
  73. "err",
  74. err,
  75. )
  76. handleDefaults(cfg)
  77. return cfg, nil
  78. }
  79. if err != nil {
  80. return nil, fmt.Errorf("failed to open config file: %w", err)
  81. }
  82. defer file.Close()
  83. if _, err := toml.NewDecoder(file).Decode(&cfg); err != nil {
  84. return nil, fmt.Errorf("failed to decode config: %w", err)
  85. }
  86. handleDefaults(cfg)
  87. return cfg, nil
  88. }
  89. func handleDefaults(cfg *Config) {
  90. if cfg.Editor == "default" {
  91. cfg.Editor = os.Getenv("EDITOR")
  92. }
  93. if cfg.Identify.Browser == "default" {
  94. cfg.Identify.Browser = consts.Browser
  95. }
  96. if cfg.Identify.BrowserVersion == "default" {
  97. cfg.Identify.BrowserVersion = consts.BrowserVersion
  98. }
  99. if cfg.Identify.UserAgent == "default" {
  100. cfg.Identify.UserAgent = consts.UserAgent
  101. }
  102. }