config.go 953 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  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 customized by the user.
  8. type Config struct {
  9. Token string `json:"token,omitempty"`
  10. Mouse bool `json:"mouse,omitempty"`
  11. GetMessagesLimit int `json:"getMessagesLimit,omitempty"`
  12. Theme *tview.Theme `json:"theme,omitempty"`
  13. }
  14. // NewConfig reads the configuration file (if exists) and returns a new config.
  15. func NewConfig() *Config {
  16. userHomeDir, err := os.UserHomeDir()
  17. if err != nil {
  18. panic(err)
  19. }
  20. c := Config{
  21. Mouse: true,
  22. GetMessagesLimit: 50,
  23. }
  24. configPath := userHomeDir + "/.config/discordo/config.json"
  25. if _, err := os.Stat(configPath); os.IsNotExist(err) {
  26. return &c
  27. }
  28. d, err := os.ReadFile(configPath)
  29. if err != nil {
  30. panic(err)
  31. }
  32. if err = json.Unmarshal(d, &c); err != nil {
  33. panic(err)
  34. }
  35. return &c
  36. }