@@ -71,8 +71,15 @@ func (r *Runtime) PreRunEFunc(cmd *cobra.Command, args []string) error {
71
71
return fmt .Errorf ("value of '--config' can't be empty" )
72
72
}
73
73
74
- // attempt to read config file and override them with corresponding flags
75
- if err := r .readConfig (r .config , "" ); err != nil {
74
+ // read config file and override them with corresponding flags
75
+ v := viper .New ()
76
+
77
+ if err := r .readConfig (v , r .config .File , "" ); err != nil {
78
+ return err
79
+ }
80
+
81
+ // and override them with corresponding flags
82
+ if err := r .unmarshalConfig (v , r .config ); err != nil {
76
83
return err
77
84
}
78
85
@@ -135,11 +142,9 @@ func (r *Runtime) RunEFunc(cmd *cobra.Command, args []string) error {
135
142
// readConfig attempts to read config file, either default `.terraform-docs.yml`
136
143
// or provided file with `-c, --config` flag. It will then attempt to override
137
144
// them with corresponding flags (if set).
138
- func (r * Runtime ) readConfig (config * print.Config , submoduleDir string ) error {
139
- v := viper .New ()
140
-
145
+ func (r * Runtime ) readConfig (v * viper.Viper , file string , submoduleDir string ) error {
141
146
if r .isFlagChanged ("config" ) {
142
- v .SetConfigFile (config . File )
147
+ v .SetConfigFile (file )
143
148
} else {
144
149
v .SetConfigName (".terraform-docs" )
145
150
v .SetConfigType ("yml" )
@@ -159,7 +164,7 @@ func (r *Runtime) readConfig(config *print.Config, submoduleDir string) error {
159
164
if err := v .ReadInConfig (); err != nil {
160
165
var perr * os.PathError
161
166
if errors .As (err , & perr ) {
162
- return fmt .Errorf ("config file %s not found" , config . File )
167
+ return fmt .Errorf ("config file %s not found" , file )
163
168
}
164
169
165
170
var cerr viper.ConfigFileNotFoundError
@@ -174,6 +179,10 @@ func (r *Runtime) readConfig(config *print.Config, submoduleDir string) error {
174
179
}
175
180
}
176
181
182
+ return nil
183
+ }
184
+
185
+ func (r * Runtime ) unmarshalConfig (v * viper.Viper , config * print.Config ) error {
177
186
r .bindFlags (v )
178
187
179
188
if err := v .Unmarshal (config ); err != nil {
@@ -188,7 +197,6 @@ func (r *Runtime) readConfig(config *print.Config, submoduleDir string) error {
188
197
config .Formatter = r .formatter
189
198
}
190
199
191
- // TODO
192
200
config .Parse ()
193
201
194
202
return nil
@@ -229,6 +237,22 @@ func (r *Runtime) bindFlags(v *viper.Viper) {
229
237
})
230
238
}
231
239
240
+ func (r * Runtime ) mergeConfig (v * viper.Viper ) (* print.Config , error ) {
241
+ copy := * r .config
242
+ merged := & copy
243
+
244
+ if v .IsSet ("sections.show" ) || v .IsSet ("sections.hide" ) {
245
+ merged .Sections .Show = []string {}
246
+ merged .Sections .Hide = []string {}
247
+ }
248
+
249
+ if err := r .unmarshalConfig (v , merged ); err != nil {
250
+ return nil , err
251
+ }
252
+
253
+ return merged , nil
254
+ }
255
+
232
256
// findSubmodules generates list of submodules in `rootDir/RecursivePath` if
233
257
// `--recursive` flag is set. This keeps track of `.terraform-docs.yml` in any
234
258
// of the submodules (if exists) to override the root configuration.
@@ -257,9 +281,13 @@ func (r *Runtime) findSubmodules() ([]module, error) {
257
281
cfgfile := filepath .Join (path , r .config .File )
258
282
259
283
if _ , err := os .Stat (cfgfile ); ! os .IsNotExist (err ) {
260
- cfg = print .DefaultConfig ()
284
+ v := viper .New ()
285
+
286
+ if err = r .readConfig (v , cfgfile , path ); err != nil {
287
+ return nil , err
288
+ }
261
289
262
- if err : = r .readConfig ( cfg , path ); err != nil {
290
+ if cfg , err = r .mergeConfig ( v ); err != nil {
263
291
return nil , err
264
292
}
265
293
}
0 commit comments