config.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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. Notifications struct {
  19. Enabled bool `toml:"enabled"`
  20. Duration int `toml:"duration"`
  21. Sound Sound `toml:"sound"`
  22. }
  23. Sound struct {
  24. Enabled bool `toml:"enabled"`
  25. OnlyOnPing bool `toml:"only_on_ping"`
  26. }
  27. TypingIndicator struct {
  28. Send bool `toml:"send"`
  29. Receive bool `toml:"receive"`
  30. }
  31. Icons struct {
  32. GuildCategory string `toml:"guild_category"`
  33. GuildText string `toml:"guild_text"`
  34. GuildVoice string `toml:"guild_voice"`
  35. GuildStageVoice string `toml:"guild_stage_voice"`
  36. GuildAnnouncementThread string `toml:"guild_announcement_thread"`
  37. GuildPublicThread string `toml:"guild_public_thread"`
  38. GuildPrivateThread string `toml:"guild_private_thread"`
  39. GuildAnnouncement string `toml:"guild_announcement"`
  40. GuildForum string `toml:"guild_forum"`
  41. GuildStore string `toml:"guild_store"`
  42. }
  43. Config struct {
  44. AutoFocus bool `toml:"auto_focus"`
  45. Mouse bool `toml:"mouse"`
  46. Editor string `toml:"editor"`
  47. Status discord.Status `toml:"status"`
  48. Markdown bool `toml:"markdown"`
  49. HideBlockedUsers bool `toml:"hide_blocked_users"`
  50. ShowAttachmentLinks bool `toml:"show_attachment_links"`
  51. // Use 0 to disable
  52. AutocompleteLimit uint8 `toml:"autocomplete_limit"`
  53. MessagesLimit uint8 `toml:"messages_limit"`
  54. Timestamps Timestamps `toml:"timestamps"`
  55. Notifications Notifications `toml:"notifications"`
  56. TypingIndicator TypingIndicator `toml:"typing_indicator"`
  57. Icons Icons `toml:"icons"`
  58. Keys Keys `toml:"keys"`
  59. Theme Theme `toml:"theme"`
  60. }
  61. )
  62. //go:embed config.toml
  63. var defaultCfg []byte
  64. func DefaultPath() string {
  65. path, err := os.UserConfigDir()
  66. if err != nil {
  67. slog.Info(
  68. "user config dir cannot be determined; falling back to the current dir",
  69. "err", err,
  70. )
  71. path = "."
  72. }
  73. return filepath.Join(path, consts.Name, fileName)
  74. }
  75. // Load reads the configuration file and parses it.
  76. func Load(path string) (*Config, error) {
  77. var cfg Config
  78. if err := toml.Unmarshal(defaultCfg, &cfg); err != nil {
  79. return nil, fmt.Errorf("failed to unmarshal default config: %w", err)
  80. }
  81. file, err := os.Open(path)
  82. if os.IsNotExist(err) {
  83. slog.Info(
  84. "config file does not exist, falling back to the default config",
  85. "path",
  86. path,
  87. "err",
  88. err,
  89. )
  90. } else {
  91. if err != nil {
  92. return nil, fmt.Errorf("failed to open config file: %w", err)
  93. }
  94. defer file.Close()
  95. if _, err := toml.NewDecoder(file).Decode(&cfg); err != nil {
  96. return nil, fmt.Errorf("failed to decode config: %w", err)
  97. }
  98. }
  99. applyDefaults(&cfg)
  100. return &cfg, nil
  101. }
  102. func applyDefaults(cfg *Config) {
  103. if cfg.Editor == "default" {
  104. cfg.Editor = os.Getenv("EDITOR")
  105. }
  106. if cfg.Status == "default" {
  107. cfg.Status = discord.UnknownStatus
  108. }
  109. }