config.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. package config
  2. import (
  3. "bytes"
  4. _ "embed"
  5. "io"
  6. "os"
  7. "path/filepath"
  8. "github.com/ayn2op/discordo/internal/constants"
  9. "gopkg.in/yaml.v3"
  10. )
  11. //go:embed config.yml
  12. var defaultConfig []byte
  13. type Config struct {
  14. Mouse bool `yaml:"mouse"`
  15. Timestamps bool `yaml:"timestamps"`
  16. TimestampsBeforeAuthor bool `yaml:"timestamps_before_author"`
  17. MessagesLimit uint8 `yaml:"messages_limit"`
  18. Editor string `yaml:"editor"`
  19. Keys struct {
  20. Cancel string `yaml:"cancel"`
  21. GuildsTree struct {
  22. Focus string `yaml:"focus"`
  23. Toggle string `yaml:"toggle"`
  24. } `yaml:"guilds_tree"`
  25. MessagesText struct {
  26. Focus string `yaml:"focus"`
  27. ShowImage string `yaml:"show_image"`
  28. CopyContent string `yaml:"copy_content"`
  29. Reply string `yaml:"reply"`
  30. ReplyMention string `yaml:"reply_mention"`
  31. Delete string `yaml:"delete"`
  32. SelectPrevious string `yaml:"select_previous"`
  33. SelectNext string `yaml:"select_next"`
  34. SelectFirst string `yaml:"select_first"`
  35. SelectLast string `yaml:"select_last"`
  36. SelectReply string `yaml:"select_reply"`
  37. } `yaml:"messages_text"`
  38. MessageInput struct {
  39. Focus string `yaml:"focus"`
  40. Send string `yaml:"send"`
  41. LaunchEditor string `yaml:"launch_editor"`
  42. } `yaml:"message_input"`
  43. } `yaml:"keys"`
  44. Theme struct {
  45. Border bool `yaml:"border"`
  46. BorderColor string `yaml:"border_color"`
  47. BorderPadding [4]int `yaml:"border_padding,flow"`
  48. TitleColor string `yaml:"title_color"`
  49. BackgroundColor string `yaml:"background_color"`
  50. GuildsTree struct {
  51. Graphics bool `yaml:"graphics"`
  52. } `yaml:"guilds_tree"`
  53. MessagesText struct {
  54. AuthorColor string `yaml:"author_color"`
  55. ReplyIndicator string `yaml:"reply_indicator"`
  56. } `yaml:"messages_text"`
  57. } `yaml:"theme"`
  58. }
  59. // Recursively creates the configuration directory if it does not exist already and returns the path to the configuration file.
  60. func initialize() (string, error) {
  61. path, err := os.UserConfigDir()
  62. if err != nil {
  63. return "", err
  64. }
  65. path = filepath.Join(path, constants.Name)
  66. if err := os.MkdirAll(path, os.ModePerm); err != nil {
  67. return "", err
  68. }
  69. return filepath.Join(path, "config.yml"), nil
  70. }
  71. // Recursively creates the configuration file if it does not exist already and writes the default configuration to it; otherwise, the existing configuration file is read, and returns the parsed configuration.
  72. func Load() (*Config, error) {
  73. path, err := initialize()
  74. if err != nil {
  75. return nil, err
  76. }
  77. f, err := os.Open(path)
  78. reader := io.Reader(f)
  79. if os.IsNotExist(err) {
  80. err = os.WriteFile(path, defaultConfig, os.ModePerm)
  81. reader = bytes.NewReader(defaultConfig)
  82. }
  83. if err != nil {
  84. return nil, err
  85. }
  86. defer f.Close()
  87. var cfg Config
  88. if err := yaml.NewDecoder(reader).Decode(&cfg); err != nil {
  89. return nil, err
  90. }
  91. return &cfg, nil
  92. }