config.go 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  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. cfg := DefaultConfig()
  34. path := filepath.Join(constants.ConfigDirPath, "config.toml")
  35. f, err := os.Open(path)
  36. if os.IsNotExist(err) {
  37. return &cfg, nil
  38. }
  39. if err != nil {
  40. return nil, err
  41. }
  42. defer f.Close()
  43. if _, err := toml.NewDecoder(f).Decode(&cfg); err != nil {
  44. return nil, err
  45. }
  46. return &cfg, nil
  47. }