config.go 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. package util
  2. import (
  3. "encoding/json"
  4. "os"
  5. )
  6. // Theme defines the theme for the application.
  7. type Theme struct {
  8. Background string `json:"background,omitempty"`
  9. Foreground string `json:"foreground,omitempty"`
  10. Borders bool `json:"borders,omitempty"`
  11. }
  12. // Config consists of fields, such as theme, mouse, so on, that may be customized by the user.
  13. type Config struct {
  14. Token string `json:"token,omitempty"`
  15. Mouse bool `json:"mouse,omitempty"`
  16. GetMessagesLimit int `json:"getMessagesLimit,omitempty"`
  17. Theme *Theme `json:"theme,omitempty"`
  18. }
  19. // NewConfig reads the configuration file (if exists) and returns a new config.
  20. func NewConfig() *Config {
  21. userHomeDir, err := os.UserHomeDir()
  22. if err != nil {
  23. panic(err)
  24. }
  25. var c Config = Config{
  26. Mouse: true,
  27. GetMessagesLimit: 50,
  28. Theme: &Theme{
  29. Borders: true,
  30. },
  31. }
  32. configPath := userHomeDir + "/.config/discordo/config.json"
  33. if _, err := os.Stat(configPath); os.IsNotExist(err) {
  34. return &c
  35. }
  36. d, err := os.ReadFile(configPath)
  37. if err != nil {
  38. panic(err)
  39. }
  40. if err = json.Unmarshal(d, &c); err != nil {
  41. panic(err)
  42. }
  43. return &c
  44. }