mod.go 953 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  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. f, err := os.Open(path)
  28. if err != nil {
  29. panic(err)
  30. }
  31. defer f.Close()
  32. var c Config
  33. if _, err = f.Stat(); os.IsNotExist(err) {
  34. f, err = os.Create(path)
  35. if err != nil {
  36. panic(err)
  37. }
  38. defer f.Close()
  39. c = newConfig()
  40. err = toml.NewEncoder(f).Encode(c)
  41. if err != nil {
  42. panic(err)
  43. }
  44. } else {
  45. _, err = toml.NewDecoder(f).Decode(&c)
  46. if err != nil {
  47. panic(err)
  48. }
  49. }
  50. return &c
  51. }