config.go 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  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"`
  11. Mouse bool `json:"mouse"`
  12. UserAgent string `json:"userAgent"`
  13. GetMessagesLimit int `json:"getMessagesLimit"`
  14. Theme tview.Theme `json:"theme"`
  15. }
  16. // NewConfig reads the configuration file (if exists) and returns a new config.
  17. func NewConfig() *Config {
  18. c := Config{
  19. Token: "",
  20. Mouse: true,
  21. UserAgent: "" +
  22. "Mozilla/5.0 (X11; Linux x86_64) " +
  23. "AppleWebKit/537.36 (KHTML, like Gecko) " +
  24. "Chrome/92.0.4515.131 Safari/537.36",
  25. GetMessagesLimit: 50,
  26. Theme: tview.Styles,
  27. }
  28. userHomeDir, err := os.UserHomeDir()
  29. if err != nil {
  30. return &c
  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. }