pkg.go 1.1 KB

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