config.go 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. package config
  2. import (
  3. "log/slog"
  4. "os"
  5. "path/filepath"
  6. "time"
  7. "github.com/BurntSushi/toml"
  8. )
  9. const Name = "discordo"
  10. type Config struct {
  11. Mouse bool `toml:"mouse"`
  12. HideBlockedUsers bool `toml:"hide_blocked_users"`
  13. MessagesLimit uint8 `toml:"messages_limit"`
  14. Editor string `toml:"editor"`
  15. Timestamps bool `toml:"timestamps"`
  16. TimestampsFormat string `toml:"timestamps_format"`
  17. ShowAttachmentLinks bool `toml:"show_attachment_links"`
  18. Keys Keys `toml:"keys"`
  19. Theme Theme `toml:"theme"`
  20. }
  21. func defaultConfig() *Config {
  22. return &Config{
  23. Mouse: true,
  24. HideBlockedUsers: true,
  25. MessagesLimit: 50,
  26. Editor: "default",
  27. Timestamps: false,
  28. TimestampsFormat: time.Kitchen,
  29. ShowAttachmentLinks: true,
  30. Keys: defaultKeys(),
  31. Theme: defaultTheme(),
  32. }
  33. }
  34. // Reads the configuration file and parses it.
  35. func Load() (*Config, error) {
  36. path, err := os.UserConfigDir()
  37. if err != nil {
  38. slog.Info("user configuration directory path cannot be determined; falling back to the current directory path")
  39. path = "."
  40. }
  41. path = filepath.Join(path, Name, "config.toml")
  42. f, err := os.Open(path)
  43. cfg := defaultConfig()
  44. if os.IsNotExist(err) {
  45. slog.Info("the configuration file does not exist, falling back to the default configuration", "path", path, "err", err)
  46. return cfg, nil
  47. }
  48. if err != nil {
  49. return nil, err
  50. }
  51. defer f.Close()
  52. if _, err := toml.NewDecoder(f).Decode(&cfg); err != nil {
  53. return nil, err
  54. }
  55. return cfg, nil
  56. }