mod.go 861 B

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