config.go 899 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. package util
  2. import (
  3. "encoding/json"
  4. "os"
  5. )
  6. type Theme struct {
  7. Background string `json:"background,omitempty"`
  8. Foreground string `json:"foreground,omitempty"`
  9. Borders bool `json:"borders,omitempty"`
  10. }
  11. type Config struct {
  12. GetMessagesLimit uint `json:"getMessagesLimit,omitempty"`
  13. Theme *Theme `json:"theme,omitempty"`
  14. }
  15. // NewConfig reads the configuration file (if exists) and returns a new config.
  16. func NewConfig() *Config {
  17. userHomeDir, err := os.UserHomeDir()
  18. if err != nil {
  19. panic(err)
  20. }
  21. var c Config = Config{
  22. GetMessagesLimit: 50,
  23. Theme: &Theme{
  24. Borders: true,
  25. },
  26. }
  27. configPath := userHomeDir + "/.config/discordo/config.json"
  28. if _, err := os.Stat(configPath); os.IsNotExist(err) {
  29. return &c
  30. }
  31. d, err := os.ReadFile(configPath)
  32. if err != nil {
  33. panic(err)
  34. }
  35. if err = json.Unmarshal(d, &c); err != nil {
  36. panic(err)
  37. }
  38. return &c
  39. }