object.go 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. package config
  2. import "github.com/spf13/viper"
  3. type config struct {
  4. viper *viper.Viper
  5. }
  6. func NewConfig(name, suffix, path string) *config {
  7. var c config
  8. c.viper = viper.New()
  9. c.viper.SetConfigName(name) //defaultName := lib.AppName()
  10. c.viper.SetConfigType(suffix)
  11. c.viper.AddConfigPath(path)
  12. return &c
  13. }
  14. func (c *config) Load() error {
  15. return c.viper.ReadInConfig()
  16. }
  17. func (c *config) Store() error {
  18. return c.viper.SafeWriteConfig()
  19. }
  20. func (c *config) Register(module, key string, value any) {
  21. c.viper.SetDefault(module+"."+key, value)
  22. }
  23. func (c *config) GetBool(module, key string) bool {
  24. return c.viper.GetBool(module + "." + key)
  25. }
  26. func (c *config) GetString(module, key string) string {
  27. return c.viper.GetString(module + "." + key)
  28. }
  29. func (c *config) GetInt(module, key string) int {
  30. return c.viper.GetInt(module + "." + key)
  31. }
  32. func (c *config) GetFloat(module, key string) float64 {
  33. return c.viper.GetFloat64(module + "." + key)
  34. }
  35. func (c *config) GetStringSlice(module string, key string) []string {
  36. return viper.GetStringSlice(module + "." + key)
  37. }
  38. func (c *config) Set(module, key string, value any) {
  39. viper.Set(module+"."+key, value)
  40. }