pkg.go 953 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. package config
  2. import (
  3. "os"
  4. "path/filepath"
  5. "github.com/BurntSushi/toml"
  6. )
  7. type Config struct {
  8. Path string `toml:"-"`
  9. General GeneralConfig `toml:"general"`
  10. Theme ThemeConfig `toml:"theme"`
  11. Keys KeysConfig `toml:"keys"`
  12. }
  13. func New() *Config {
  14. return &Config{
  15. Path: DefaultPath(),
  16. General: newGeneralConfig(),
  17. Theme: newThemeConfig(),
  18. Keys: newKeysConfig(),
  19. }
  20. }
  21. func (c *Config) Load() {
  22. err := os.MkdirAll(filepath.Dir(c.Path), os.ModePerm)
  23. if err != nil {
  24. panic(err)
  25. }
  26. if _, err = os.Stat(c.Path); os.IsNotExist(err) {
  27. f, err := os.Create(c.Path)
  28. if err != nil {
  29. panic(err)
  30. }
  31. err = toml.NewEncoder(f).Encode(c)
  32. if err != nil {
  33. panic(err)
  34. }
  35. } else {
  36. _, err = toml.DecodeFile(c.Path, &c)
  37. if err != nil {
  38. panic(err)
  39. }
  40. }
  41. }
  42. func DefaultPath() string {
  43. path, err := os.UserConfigDir()
  44. if err != nil {
  45. panic(err)
  46. }
  47. path += "/discordo/config.toml"
  48. return path
  49. }