pkg.go 1.1 KB

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