| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596 |
- package config
- import (
- "fmt"
- "github.com/go-kratos/kratos/v2/config"
- "github.com/go-kratos/kratos/v2/config/file"
- clientv3 "go.etcd.io/etcd/client/v3"
- )
- type Source interface {
- NewSource() (config.Source, error)
- Validate() bool
- String() string
- }
- type Format string
- const (
- Yaml Format = "yaml"
- )
- var formatMap = map[Format]string{
- Yaml: "yaml",
- }
- func (f Format) String() string {
- return string(f)
- }
- func (f Format) Validate() bool {
- _, ok := formatMap[f]
- return ok
- }
- // =======================================
- type EtcdSource struct {
- Format Format
- Client *clientv3.Client
- Namespace string
- Name string
- }
- func (s *EtcdSource) NewSource() (config.Source, error) {
- source, err := New(s.Client, WithPath(fmt.Sprintf("/%s/config/%s", s.Namespace, s.Name)))
- if err != nil {
- return nil, err
- }
- return source, nil
- }
- func (s *EtcdSource) Validate() bool {
- if !s.Format.Validate() {
- return false
- }
- if s.Client == nil {
- return false
- }
- if s.Namespace == "" {
- return false
- }
- if s.Name == "" {
- return false
- }
- return true
- }
- func (s *EtcdSource) String() string {
- return fmt.Sprintf("etcd source format:%v, namespace:%v, name:%v", s.Format, s.Namespace, s.Name)
- }
- // =======================================
- type FileSource struct {
- Format Format
- Path string
- }
- func (s *FileSource) NewSource() (config.Source, error) {
- source := file.NewSource(s.Path)
- return source, nil
- }
- func (s *FileSource) Validate() bool {
- if !s.Format.Validate() {
- return false
- }
- if s.Path == "" {
- return false
- }
- return true
- }
- func (s *FileSource) String() string {
- return fmt.Sprintf("file source format:%v, path:%v", s.Format, s.Path)
- }
|