source.go 899 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. package config
  2. import (
  3. "fmt"
  4. "git.ikuban.com/server/kratos-utils/v2/config"
  5. kconfig "github.com/go-kratos/kratos/v2/config"
  6. clientv3 "go.etcd.io/etcd/client/v3"
  7. )
  8. var _ config.Source = (*EtcdSource)(nil)
  9. type EtcdSource struct {
  10. Format config.Format
  11. Client *clientv3.Client
  12. Namespace string
  13. Name string
  14. }
  15. func (s *EtcdSource) NewSource() (kconfig.Source, error) {
  16. source, err := New(s.Client, WithPath(fmt.Sprintf("/%s/config/%s", s.Namespace, s.Name)))
  17. if err != nil {
  18. return nil, err
  19. }
  20. return source, nil
  21. }
  22. func (s *EtcdSource) Validate() bool {
  23. if !s.Format.Validate() {
  24. return false
  25. }
  26. if s.Client == nil {
  27. return false
  28. }
  29. if s.Namespace == "" {
  30. return false
  31. }
  32. if s.Name == "" {
  33. return false
  34. }
  35. return true
  36. }
  37. func (s *EtcdSource) String() string {
  38. return fmt.Sprintf("etcd source format:%v, namespace:%v, name:%v", s.Format, s.Namespace, s.Name)
  39. }