config.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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. Config struct {
  32. AutoFocus bool `toml:"auto_focus"`
  33. Mouse bool `toml:"mouse"`
  34. Editor string `toml:"editor"`
  35. Status discord.Status `toml:"status"`
  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. Notifications Notifications `toml:"notifications"`
  44. TypingIndicator TypingIndicator `toml:"typing_indicator"`
  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 config dir cannot be determined; falling back to the current dir",
  56. "err", err,
  57. )
  58. path = "."
  59. }
  60. return filepath.Join(path, consts.Name, fileName)
  61. }
  62. // Load reads the configuration file and parses it.
  63. func Load(path string) (*Config, error) {
  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. file, err := os.Open(path)
  69. if os.IsNotExist(err) {
  70. slog.Info(
  71. "config file does not exist, falling back to the default config",
  72. "path",
  73. path,
  74. "err",
  75. err,
  76. )
  77. } else {
  78. if err != nil {
  79. return nil, fmt.Errorf("failed to open config file: %w", err)
  80. }
  81. defer file.Close()
  82. if _, err := toml.NewDecoder(file).Decode(&cfg); err != nil {
  83. return nil, fmt.Errorf("failed to decode config: %w", err)
  84. }
  85. }
  86. applyDefaults(&cfg)
  87. return &cfg, nil
  88. }
  89. func applyDefaults(cfg *Config) {
  90. if cfg.Editor == "default" {
  91. cfg.Editor = os.Getenv("EDITOR")
  92. }
  93. if cfg.Status == "default" {
  94. cfg.Status = discord.UnknownStatus
  95. }
  96. }