@@ -24,6 +24,8 @@ package source
24
24
import (
25
25
"errors"
26
26
"fmt"
27
+ "gopkg.in/yaml.v3"
28
+ "io"
27
29
"reflect"
28
30
"regexp"
29
31
"sync"
@@ -36,6 +38,7 @@ import (
36
38
var (
37
39
ErrKeyNotExist = errors .New ("key does not exist" )
38
40
ErrIgnoreChange = errors .New ("ignore key changed" )
41
+ ErrWriterInvalid = errors .New ("writer is invalid" )
39
42
)
40
43
41
44
//const
@@ -82,7 +85,7 @@ func (m *Manager) Cleanup() error {
82
85
//Set call set of all sources
83
86
func (m * Manager ) Set (k string , v interface {}) error {
84
87
m .sourceMapMux .RLock ()
85
- defer m .sourceMapMux .RLock ()
88
+ defer m .sourceMapMux .RUnlock ()
86
89
var err error
87
90
for _ , s := range m .Sources {
88
91
err = s .Set (k , v )
@@ -128,6 +131,28 @@ func (m *Manager) Unmarshal(obj interface{}) error {
128
131
return m .unmarshal (rv , doNotConsiderTag )
129
132
}
130
133
134
+ // Marshal function is used to write all configuration by yaml
135
+ func (m * Manager ) Marshal (w io.Writer ) error {
136
+ if w == nil {
137
+ openlog .Error ("invalid writer" )
138
+ return ErrWriterInvalid
139
+ }
140
+ allConfig := make (map [string ]map [string ]interface {})
141
+ for name , source := range m .Sources {
142
+ config , err := source .GetConfigurations ()
143
+ if err != nil {
144
+ openlog .Error ("get source " + name + " error " + err .Error ())
145
+ continue
146
+ }
147
+ if len (config ) == 0 {
148
+ continue
149
+ }
150
+ allConfig [name ] = config
151
+ }
152
+ encode := yaml .NewEncoder (w )
153
+ return encode .Encode (allConfig )
154
+ }
155
+
131
156
// AddSource adds a source to configurationManager
132
157
func (m * Manager ) AddSource (source ConfigSource ) error {
133
158
if source == nil || source .GetSourceName () == "" {
0 commit comments