pkg.go 946 B

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