-
Notifications
You must be signed in to change notification settings - Fork 6
/
module.go
229 lines (211 loc) · 5.22 KB
/
module.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
package config
import (
"context"
"encoding/json"
"fmt"
"github.com/DoNewsCode/core/di"
"io/ioutil"
"os"
"path/filepath"
"github.com/DoNewsCode/core/contract"
"github.com/ghodss/yaml"
"github.com/oklog/run"
"github.com/pkg/errors"
"github.com/spf13/cobra"
)
// Module is the configuration module that bundles the reload watcher and exportConfig commands.
type Module struct {
conf *KoanfAdapter
exportedConfigs []ExportedConfig
}
// ConfigIn is the injection parameter for config.New.
type ConfigIn struct {
di.In
Conf contract.ConfigAccessor
ExportedConfigs []ExportedConfig `group:"config"`
}
// New creates a new config module. It contains the init command.
func New(p ConfigIn) (Module, error) {
var (
ok bool
adapter *KoanfAdapter
)
if adapter, ok = p.Conf.(*KoanfAdapter); !ok {
return Module{}, fmt.Errorf("expects a *config.KoanfAdapter instance, but %T given", p.Conf)
}
return Module{
conf: adapter,
exportedConfigs: p.ExportedConfigs,
}, nil
}
// ProvideRunGroup runs the configuration watcher.
func (m Module) ProvideRunGroup(group *run.Group) {
ctx, cancel := context.WithCancel(context.Background())
group.Add(func() error {
return m.conf.Watch(ctx)
}, func(err error) {
cancel()
})
}
// ProvideCommand provides the config related command.
func (m Module) ProvideCommand(command *cobra.Command) {
var (
outputFile string
style string
)
initCmd := &cobra.Command{
Use: "init [module]",
Short: "export a copy of default config.",
Long: "export a default config for currently installed modules.",
RunE: func(cmd *cobra.Command, args []string) error {
var (
handler handler
targetFile *os.File
exportedConfigs []ExportedConfig
confMap map[string]interface{}
err error
)
handler, err = getHandler(style)
if err != nil {
return err
}
if len(args) == 0 {
exportedConfigs = m.exportedConfigs
}
if len(args) >= 1 {
var copy = make([]ExportedConfig, 0)
for i := range m.exportedConfigs {
for j := 0; j < len(args); j++ {
if args[j] == m.exportedConfigs[i].Owner {
copy = append(copy, m.exportedConfigs[i])
break
}
}
}
exportedConfigs = copy
}
os.MkdirAll(filepath.Dir(outputFile), 0644)
targetFile, err = os.OpenFile(outputFile,
handler.flags(), 0644)
if err != nil {
return errors.Wrap(err, "failed to open config file")
}
defer targetFile.Close()
bytes, err := ioutil.ReadAll(targetFile)
if err != nil {
return errors.Wrap(err, "failed to read config file")
}
err = handler.unmarshal(bytes, &confMap)
if err != nil {
return errors.Wrap(err, "failed to unmarshal config file")
}
err = handler.write(targetFile, exportedConfigs, confMap)
if err != nil {
return errors.Wrap(err, "failed to write config file")
}
return nil
},
}
initCmd.Flags().StringVarP(
&outputFile,
"outputFile",
"o",
"./config/config.yaml",
"The output file of exported config",
)
initCmd.Flags().StringVarP(
&style,
"style",
"s",
"yaml",
"The output file style",
)
configCmd := &cobra.Command{
Use: "config",
Short: "manage configuration",
Long: "manage configuration, such as export a copy of default config.",
}
configCmd.AddCommand(initCmd)
command.AddCommand(configCmd)
}
func getHandler(style string) (handler, error) {
switch style {
case "json":
return jsonHandler{}, nil
case "yaml":
return yamlHandler{}, nil
default:
return nil, fmt.Errorf("unsupported config style %s", style)
}
}
type yamlHandler struct {
}
func (y yamlHandler) flags() int {
return os.O_APPEND | os.O_CREATE | os.O_RDWR
}
func (y yamlHandler) unmarshal(bytes []byte, o interface{}) error {
return yaml.Unmarshal(bytes, o)
}
func (y yamlHandler) write(file *os.File, configs []ExportedConfig, confMap map[string]interface{}) error {
out:
for _, config := range configs {
for k := range config.Data {
if _, ok := confMap[k]; ok {
continue out
}
}
bytes, err := yaml.Marshal(config.Data)
if err != nil {
return err
}
if config.Comment != "" {
_, err = fmt.Fprintln(file, "# "+config.Comment)
if err != nil {
return err
}
}
_, err = file.Write(bytes)
if err != nil {
return err
}
_, err = fmt.Fprintln(file)
if err != nil {
return err
}
}
return nil
}
type handler interface {
flags() int
unmarshal(bytes []byte, o interface{}) error
write(file *os.File, configs []ExportedConfig, confMap map[string]interface{}) error
}
type jsonHandler struct {
}
func (y jsonHandler) flags() int {
return os.O_CREATE | os.O_RDWR
}
func (y jsonHandler) unmarshal(bytes []byte, o interface{}) error {
if len(bytes) == 0 {
return nil
}
return json.Unmarshal(bytes, o)
}
func (y jsonHandler) write(file *os.File, configs []ExportedConfig, confMap map[string]interface{}) error {
if confMap == nil {
confMap = make(map[string]interface{})
}
for _, exportedConfig := range configs {
if _, ok := confMap[exportedConfig.Owner]; ok {
continue
}
for k := range exportedConfig.Data {
confMap[k] = exportedConfig.Data[k]
}
}
file.Seek(0, 0)
encoder := json.NewEncoder(file)
encoder.SetIndent("", " ")
err := encoder.Encode(confMap)
return err
}