config.go 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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. Timestamps bool `toml:"timestamps"`
  12. TimestampsBeforeAuthor bool `toml:"timestamps_before_author"`
  13. TimestampsFormat string `toml:"timestamps_format"`
  14. MessagesLimit uint8 `toml:"messages_limit"`
  15. Editor string `toml:"editor"`
  16. Keys Keys `toml:"keys"`
  17. Theme Theme `toml:"theme"`
  18. }
  19. func DefaultConfig() Config {
  20. return Config{
  21. Mouse: true,
  22. Timestamps: false,
  23. TimestampsBeforeAuthor: false,
  24. TimestampsFormat: time.Kitchen,
  25. MessagesLimit: 50,
  26. Editor: "default",
  27. Keys: defaultKeys(),
  28. Theme: defaultTheme(),
  29. }
  30. }
  31. // Reads the configuration file and parses it.
  32. func Load() (*Config, error) {
  33. path, err := os.UserConfigDir()
  34. if err != nil {
  35. return nil, err
  36. }
  37. cfg := DefaultConfig()
  38. path = filepath.Join(path, constants.Name, "config.toml")
  39. f, err := os.Open(path)
  40. if os.IsNotExist(err) {
  41. return &cfg, nil
  42. }
  43. if err != nil {
  44. return nil, err
  45. }
  46. defer f.Close()
  47. if _, err := toml.NewDecoder(f).Decode(&cfg); err != nil {
  48. return nil, err
  49. }
  50. return &cfg, nil
  51. }