config.go 955 B

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