mod.go 940 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  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 newConfig() Config {
  13. return Config{
  14. General: newGeneralConfig(),
  15. Theme: newThemeConfig(),
  16. Keybindings: newKeybindingsConfig(),
  17. }
  18. }
  19. func NewConfig() *Config {
  20. path, err := os.UserConfigDir()
  21. if err != nil {
  22. panic(err)
  23. }
  24. path += "/discordo/config.toml"
  25. err = os.MkdirAll(filepath.Dir(path), os.ModePerm)
  26. if err != nil {
  27. panic(err)
  28. }
  29. var c Config
  30. if _, err = os.Stat(path); os.IsNotExist(err) {
  31. f, err := os.Create(path)
  32. if err != nil {
  33. panic(err)
  34. }
  35. c = newConfig()
  36. err = toml.NewEncoder(f).Encode(c)
  37. if err != nil {
  38. panic(err)
  39. }
  40. } else {
  41. _, err = toml.DecodeFile(path, &c)
  42. if err != nil {
  43. panic(err)
  44. }
  45. }
  46. return &c
  47. }