config.go 1.2 KB

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