Skip to content

Commit 6f97f67

Browse files
committed
Add abitlity to partially override config from submodules
In the original implementation, when a submodule provided its configuration it would completely override any config was provided by its parent module. However with this approach the child module's configuration will merge into its parent config and override any item defined by both. Example: ``` $ cat .terraform-docs.yml formatter: markdown table recursive: enabled: true sections: show: - header - inputs - outputs output: file: README.md mode: inject settings: default: true required: true type: true $ cat modules/module-b/.terraform-docs.yml formatter: yaml sections: show: - providers settings: default: false escape: false ``` The computed values for module-b configuration will be: ``` formatter: yaml recursive: enabled: true sections: show: - providers output: file: README.md mode: inject settings: default: false escape: false required: true type: true ``` Signed-off-by: Khosrow Moossavi <khos2ow@gmail.com>
1 parent 54dc0f5 commit 6f97f67

File tree

1 file changed

+38
-10
lines changed

1 file changed

+38
-10
lines changed

internal/cli/run.go

Lines changed: 38 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,15 @@ func (r *Runtime) PreRunEFunc(cmd *cobra.Command, args []string) error {
7171
return fmt.Errorf("value of '--config' can't be empty")
7272
}
7373

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 {
7683
return err
7784
}
7885

@@ -135,11 +142,9 @@ func (r *Runtime) RunEFunc(cmd *cobra.Command, args []string) error {
135142
// readConfig attempts to read config file, either default `.terraform-docs.yml`
136143
// or provided file with `-c, --config` flag. It will then attempt to override
137144
// 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 {
141146
if r.isFlagChanged("config") {
142-
v.SetConfigFile(config.File)
147+
v.SetConfigFile(file)
143148
} else {
144149
v.SetConfigName(".terraform-docs")
145150
v.SetConfigType("yml")
@@ -159,7 +164,7 @@ func (r *Runtime) readConfig(config *print.Config, submoduleDir string) error {
159164
if err := v.ReadInConfig(); err != nil {
160165
var perr *os.PathError
161166
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)
163168
}
164169

165170
var cerr viper.ConfigFileNotFoundError
@@ -174,6 +179,10 @@ func (r *Runtime) readConfig(config *print.Config, submoduleDir string) error {
174179
}
175180
}
176181

182+
return nil
183+
}
184+
185+
func (r *Runtime) unmarshalConfig(v *viper.Viper, config *print.Config) error {
177186
r.bindFlags(v)
178187

179188
if err := v.Unmarshal(config); err != nil {
@@ -188,7 +197,6 @@ func (r *Runtime) readConfig(config *print.Config, submoduleDir string) error {
188197
config.Formatter = r.formatter
189198
}
190199

191-
// TODO
192200
config.Parse()
193201

194202
return nil
@@ -229,6 +237,22 @@ func (r *Runtime) bindFlags(v *viper.Viper) {
229237
})
230238
}
231239

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+
232256
// findSubmodules generates list of submodules in `rootDir/RecursivePath` if
233257
// `--recursive` flag is set. This keeps track of `.terraform-docs.yml` in any
234258
// of the submodules (if exists) to override the root configuration.
@@ -257,9 +281,13 @@ func (r *Runtime) findSubmodules() ([]module, error) {
257281
cfgfile := filepath.Join(path, r.config.File)
258282

259283
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+
}
261289

262-
if err := r.readConfig(cfg, path); err != nil {
290+
if cfg, err = r.mergeConfig(v); err != nil {
263291
return nil, err
264292
}
265293
}

0 commit comments

Comments
 (0)