Skip to content

Commit

Permalink
Make main module doc optional when in recursive generation
Browse files Browse the repository at this point in the history
Signed-off-by: Blake Gong <blakegong@gmail.com>
  • Loading branch information
blakegong authored and khos2ow committed May 30, 2024
1 parent e919275 commit 59eb90f
Show file tree
Hide file tree
Showing 7 changed files with 26 additions and 7 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,7 @@ footer-from: ""
recursive:
enabled: false
path: modules
include-main: true

sections:
hide: []
Expand Down
1 change: 1 addition & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ func NewCommand() *cobra.Command {
cmd.PersistentFlags().StringVarP(&config.File, "config", "c", ".terraform-docs.yml", "config file name")
cmd.PersistentFlags().BoolVar(&config.Recursive.Enabled, "recursive", false, "update submodules recursively (default false)")
cmd.PersistentFlags().StringVar(&config.Recursive.Path, "recursive-path", "modules", "submodules path to recursively update")
cmd.PersistentFlags().BoolVar(&config.Recursive.IncludeMain, "recursive-include-main", true, "include the main module (default true)")

cmd.PersistentFlags().StringSliceVar(&config.Sections.Show, "show", []string{}, "show section ["+print.AllSections+"]")
cmd.PersistentFlags().StringSliceVar(&config.Sections.Hide, "hide", []string{}, "hide section ["+print.AllSections+"]")
Expand Down
2 changes: 2 additions & 0 deletions docs/how-to/recursive-submodules.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ set.
Path to find submodules can be configured with `--recursive-path` (defaults to
`modules`).

The main module document is generated by default, which can be configured with `--recursive-include-main`. Should the main module document be excluded from document generation, use `--recursive-include-main=false`.

Each submodule can also have their own `.terraform-docs.yml` config file, to
override configuration from root module.

Expand Down
1 change: 1 addition & 0 deletions docs/user-guide/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ footer-from: ""
recursive:
enabled: false
path: modules
include-main: true

sections:
hide: []
Expand Down
10 changes: 10 additions & 0 deletions docs/user-guide/configuration/recursive.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ Available options with their default values.
recursive:
enabled: false
path: modules
include-main: true
```

## Examples
Expand All @@ -50,3 +51,12 @@ recursive:
enabled: true
path: submodules-folder
```

Skip the main module document, and only generate documents for submodules.

```yaml
recursive:
enabled: true
path: submodules-folder
include-main: false
```
8 changes: 5 additions & 3 deletions internal/cli/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,9 +94,11 @@ type module struct {
// RunEFunc is the 'cobra.Command#RunE' function for 'formatter' commands. It attempts
// to discover submodules, on `--recursive` flag, and generates the content for them
// as well as the root module.
func (r *Runtime) RunEFunc(cmd *cobra.Command, args []string) error {
modules := []module{
{rootDir: r.rootDir, config: r.config},
func (r *Runtime) RunEFunc(cmd *cobra.Command, args []string) error { //nolint:gocyclo
modules := []module{}

if r.config.Recursive.IncludeMain {
modules = append(modules, module{r.rootDir, r.config})
}

// Generating content recursively is only allowed when `config.Output.File`
Expand Down
10 changes: 6 additions & 4 deletions print/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,14 +73,16 @@ func DefaultConfig() *Config {
}

type recursive struct {
Enabled bool `mapstructure:"enabled"`
Path string `mapstructure:"path"`
Enabled bool `mapstructure:"enabled"`
Path string `mapstructure:"path"`
IncludeMain bool `mapstructure:"include-main"`
}

func defaultRecursive() recursive {
return recursive{
Enabled: false,
Path: "modules",
Enabled: false,
Path: "modules",
IncludeMain: true,
}
}

Expand Down

0 comments on commit 59eb90f

Please sign in to comment.