config.go 1.1 KB

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