config.go 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. package config
  2. import (
  3. "os"
  4. "path/filepath"
  5. "time"
  6. "github.com/BurntSushi/toml"
  7. "github.com/ayn2op/discordo/internal/constants"
  8. )
  9. type Config struct {
  10. Mouse bool `toml:"mouse"`
  11. HideBlockedUsers bool `toml:"hide_blocked_users"`
  12. MessagesLimit uint8 `toml:"messages_limit"`
  13. Editor string `toml:"editor"`
  14. Timestamps bool `toml:"timestamps"`
  15. TimestampsBeforeAuthor bool `toml:"timestamps_before_author"`
  16. TimestampsFormat string `toml:"timestamps_format"`
  17. Keys Keys `toml:"keys"`
  18. Theme Theme `toml:"theme"`
  19. }
  20. func defaultConfig() *Config {
  21. return &Config{
  22. Mouse: true,
  23. HideBlockedUsers: true,
  24. MessagesLimit: 50,
  25. Editor: "default",
  26. Timestamps: false,
  27. TimestampsBeforeAuthor: false,
  28. TimestampsFormat: time.Kitchen,
  29. Keys: defaultKeys(),
  30. Theme: defaultTheme(),
  31. }
  32. }
  33. // Reads the configuration file and parses it.
  34. func Load() (*Config, error) {
  35. path := filepath.Join(constants.ConfigDirPath, "config.toml")
  36. f, err := os.Open(path)
  37. cfg := defaultConfig()
  38. if os.IsNotExist(err) {
  39. return cfg, nil
  40. }
  41. if err != nil {
  42. return nil, err
  43. }
  44. defer f.Close()
  45. if _, err := toml.NewDecoder(f).Decode(&cfg); err != nil {
  46. return nil, err
  47. }
  48. return cfg, nil
  49. }