config.go 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. package config
  2. import (
  3. "log"
  4. "os"
  5. "path/filepath"
  6. "github.com/BurntSushi/toml"
  7. "github.com/ayn2op/discordo/internal/constants"
  8. )
  9. type Config struct {
  10. Mouse bool `toml:"mouse"`
  11. Timestamps bool `toml:"timestamps"`
  12. TimestampsBeforeAuthor bool `toml:"timestamps_before_author"`
  13. MessagesLimit uint8 `toml:"messages_limit"`
  14. Editor string `toml:"editor"`
  15. Keys Keys `toml:"keys"`
  16. Theme Theme `toml:"theme"`
  17. }
  18. func defaultConfig() Config {
  19. return Config{
  20. Mouse: true,
  21. Timestamps: false,
  22. TimestampsBeforeAuthor: false,
  23. MessagesLimit: 50,
  24. Editor: "default",
  25. Keys: defaultKeys(),
  26. Theme: defaultTheme(),
  27. }
  28. }
  29. // Reads the configuration file and parses it.
  30. func Load() (*Config, error) {
  31. path, err := os.UserConfigDir()
  32. if err != nil {
  33. return nil, err
  34. }
  35. cfg := defaultConfig()
  36. path = filepath.Join(path, constants.Name, "config.toml")
  37. f, err := os.Open(path)
  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. log.Println(cfg.Theme.MessagesText.ReplyIndicator)
  49. return &cfg, nil
  50. }