config.go 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  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 for properties, such as theme, that may be customized by the user.
  13. type Config struct {
  14. GetMessagesLimit uint `json:"getMessagesLimit,omitempty"`
  15. Theme *Theme `json:"theme,omitempty"`
  16. }
  17. // NewConfig reads the configuration file (if exists) and returns a new config.
  18. func NewConfig() *Config {
  19. userHomeDir, err := os.UserHomeDir()
  20. if err != nil {
  21. panic(err)
  22. }
  23. var c Config = Config{
  24. GetMessagesLimit: 50,
  25. Theme: &Theme{
  26. Borders: true,
  27. },
  28. }
  29. configPath := userHomeDir + "/.config/discordo/config.json"
  30. if _, err := os.Stat(configPath); os.IsNotExist(err) {
  31. return &c
  32. }
  33. d, err := os.ReadFile(configPath)
  34. if err != nil {
  35. panic(err)
  36. }
  37. if err = json.Unmarshal(d, &c); err != nil {
  38. panic(err)
  39. }
  40. return &c
  41. }