Skip to content

Commit 1450ee9

Browse files
committed
Generate submodules documents with '--resurcive' flag
Considering the file strucutre below of main module and its submodules, now it is possible to generate documentation for them main and all its submodules in one execution, with `--recursive` flag. Note that generating documentation recursively is allowed only with `--output-file` set. Path to find submodules can be configured with `--recursive-path` (defaults to `modules`). Each submodule can also have their own `.terraform-docs.yml` confi file, to override configuration from root module. ``` . ├── README.md ├── main.tf ├── modules │   └── my-sub-module │   ├── README.md │   ├── main.tf │   ├── variables.tf │   └── versions.tf ├── outputs.tf ├── variables.tf └── versions.tf ``` Signed-off-by: Khosrow Moossavi <khos2ow@gmail.com>
1 parent 45bf4f6 commit 1450ee9

35 files changed

+388
-184
lines changed

cmd/asciidoc/asciidoc.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,15 @@ import (
1919
)
2020

2121
// NewCommand returns a new cobra.Command for 'asciidoc' formatter
22-
func NewCommand(config *cli.Config) *cobra.Command {
22+
func NewCommand(runtime *cli.Runtime, config *cli.Config) *cobra.Command {
2323
cmd := &cobra.Command{
2424
Args: cobra.ExactArgs(1),
2525
Use: "asciidoc [PATH]",
2626
Aliases: []string{"adoc"},
2727
Short: "Generate AsciiDoc of inputs and outputs",
2828
Annotations: cli.Annotations("asciidoc"),
29-
PreRunE: cli.PreRunEFunc(config),
30-
RunE: cli.RunEFunc(config),
29+
PreRunE: runtime.PreRunEFunc,
30+
RunE: runtime.RunEFunc,
3131
}
3232

3333
// flags
@@ -39,8 +39,8 @@ func NewCommand(config *cli.Config) *cobra.Command {
3939
cmd.PersistentFlags().BoolVar(&config.Settings.Type, "type", true, "show Type column or section")
4040

4141
// subcommands
42-
cmd.AddCommand(document.NewCommand(config))
43-
cmd.AddCommand(table.NewCommand(config))
42+
cmd.AddCommand(document.NewCommand(runtime, config))
43+
cmd.AddCommand(table.NewCommand(runtime, config))
4444

4545
return cmd
4646
}

cmd/asciidoc/document/document.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,15 @@ import (
1717
)
1818

1919
// NewCommand returns a new cobra.Command for 'asciidoc document' formatter
20-
func NewCommand(config *cli.Config) *cobra.Command {
20+
func NewCommand(runtime *cli.Runtime, config *cli.Config) *cobra.Command {
2121
cmd := &cobra.Command{
2222
Args: cobra.ExactArgs(1),
2323
Use: "document [PATH]",
2424
Aliases: []string{"doc"},
2525
Short: "Generate AsciiDoc document of inputs and outputs",
2626
Annotations: cli.Annotations("asciidoc document"),
27-
PreRunE: cli.PreRunEFunc(config),
28-
RunE: cli.RunEFunc(config),
27+
PreRunE: runtime.PreRunEFunc,
28+
RunE: runtime.RunEFunc,
2929
}
3030
return cmd
3131
}

cmd/asciidoc/table/table.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,15 @@ import (
1717
)
1818

1919
// NewCommand returns a new cobra.Command for 'asciidoc table' formatter
20-
func NewCommand(config *cli.Config) *cobra.Command {
20+
func NewCommand(runtime *cli.Runtime, config *cli.Config) *cobra.Command {
2121
cmd := &cobra.Command{
2222
Args: cobra.ExactArgs(1),
2323
Use: "table [PATH]",
2424
Aliases: []string{"tbl"},
2525
Short: "Generate AsciiDoc tables of inputs and outputs",
2626
Annotations: cli.Annotations("asciidoc table"),
27-
PreRunE: cli.PreRunEFunc(config),
28-
RunE: cli.RunEFunc(config),
27+
PreRunE: runtime.PreRunEFunc,
28+
RunE: runtime.RunEFunc,
2929
}
3030
return cmd
3131
}

cmd/json/json.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,14 @@ import (
1717
)
1818

1919
// NewCommand returns a new cobra.Command for 'json' formatter
20-
func NewCommand(config *cli.Config) *cobra.Command {
20+
func NewCommand(runtime *cli.Runtime, config *cli.Config) *cobra.Command {
2121
cmd := &cobra.Command{
2222
Args: cobra.ExactArgs(1),
2323
Use: "json [PATH]",
2424
Short: "Generate JSON of inputs and outputs",
2525
Annotations: cli.Annotations("json"),
26-
PreRunE: cli.PreRunEFunc(config),
27-
RunE: cli.RunEFunc(config),
26+
PreRunE: runtime.PreRunEFunc,
27+
RunE: runtime.RunEFunc,
2828
}
2929

3030
// flags

cmd/markdown/document/document.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,15 @@ import (
1717
)
1818

1919
// NewCommand returns a new cobra.Command for 'markdown document' formatter
20-
func NewCommand(config *cli.Config) *cobra.Command {
20+
func NewCommand(runtime *cli.Runtime, config *cli.Config) *cobra.Command {
2121
cmd := &cobra.Command{
2222
Args: cobra.ExactArgs(1),
2323
Use: "document [PATH]",
2424
Aliases: []string{"doc"},
2525
Short: "Generate Markdown document of inputs and outputs",
2626
Annotations: cli.Annotations("markdown document"),
27-
PreRunE: cli.PreRunEFunc(config),
28-
RunE: cli.RunEFunc(config),
27+
PreRunE: runtime.PreRunEFunc,
28+
RunE: runtime.RunEFunc,
2929
}
3030
return cmd
3131
}

cmd/markdown/markdown.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,15 @@ import (
1919
)
2020

2121
// NewCommand returns a new cobra.Command for 'markdown' formatter
22-
func NewCommand(config *cli.Config) *cobra.Command {
22+
func NewCommand(runtime *cli.Runtime, config *cli.Config) *cobra.Command {
2323
cmd := &cobra.Command{
2424
Args: cobra.ExactArgs(1),
2525
Use: "markdown [PATH]",
2626
Aliases: []string{"md"},
2727
Short: "Generate Markdown of inputs and outputs",
2828
Annotations: cli.Annotations("markdown"),
29-
PreRunE: cli.PreRunEFunc(config),
30-
RunE: cli.RunEFunc(config),
29+
PreRunE: runtime.PreRunEFunc,
30+
RunE: runtime.RunEFunc,
3131
}
3232

3333
// flags
@@ -41,8 +41,8 @@ func NewCommand(config *cli.Config) *cobra.Command {
4141
cmd.PersistentFlags().BoolVar(&config.Settings.Type, "type", true, "show Type column or section")
4242

4343
// subcommands
44-
cmd.AddCommand(document.NewCommand(config))
45-
cmd.AddCommand(table.NewCommand(config))
44+
cmd.AddCommand(document.NewCommand(runtime, config))
45+
cmd.AddCommand(table.NewCommand(runtime, config))
4646

4747
return cmd
4848
}

cmd/markdown/table/table.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,15 @@ import (
1717
)
1818

1919
// NewCommand returns a new cobra.Command for 'markdown table' formatter
20-
func NewCommand(config *cli.Config) *cobra.Command {
20+
func NewCommand(runtime *cli.Runtime, config *cli.Config) *cobra.Command {
2121
cmd := &cobra.Command{
2222
Args: cobra.ExactArgs(1),
2323
Use: "table [PATH]",
2424
Aliases: []string{"tbl"},
2525
Short: "Generate Markdown tables of inputs and outputs",
2626
Annotations: cli.Annotations("markdown table"),
27-
PreRunE: cli.PreRunEFunc(config),
28-
RunE: cli.RunEFunc(config),
27+
PreRunE: runtime.PreRunEFunc,
28+
RunE: runtime.RunEFunc,
2929
}
3030
return cmd
3131
}

cmd/pretty/pretty.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,14 @@ import (
1717
)
1818

1919
// NewCommand returns a new cobra.Command for pretty formatter
20-
func NewCommand(config *cli.Config) *cobra.Command {
20+
func NewCommand(runtime *cli.Runtime, config *cli.Config) *cobra.Command {
2121
cmd := &cobra.Command{
2222
Args: cobra.ExactArgs(1),
2323
Use: "pretty [PATH]",
2424
Short: "Generate colorized pretty of inputs and outputs",
2525
Annotations: cli.Annotations("pretty"),
26-
PreRunE: cli.PreRunEFunc(config),
27-
RunE: cli.RunEFunc(config),
26+
PreRunE: runtime.PreRunEFunc,
27+
RunE: runtime.RunEFunc,
2828
}
2929

3030
// flags

cmd/root.go

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ func Execute() error {
4343
// NewCommand returns a new cobra.Command for 'root' command
4444
func NewCommand() *cobra.Command {
4545
config := cli.DefaultConfig()
46+
runtime := cli.NewRuntime(config)
4647
cmd := &cobra.Command{
4748
Args: cobra.MaximumNArgs(1),
4849
Use: "terraform-docs [PATH]",
@@ -52,12 +53,14 @@ func NewCommand() *cobra.Command {
5253
SilenceUsage: true,
5354
SilenceErrors: true,
5455
Annotations: cli.Annotations("root"),
55-
PreRunE: cli.PreRunEFunc(config),
56-
RunE: cli.RunEFunc(config),
56+
PreRunE: runtime.PreRunEFunc,
57+
RunE: runtime.RunEFunc,
5758
}
5859

5960
// flags
6061
cmd.PersistentFlags().StringVarP(&config.File, "config", "c", ".terraform-docs.yml", "config file name")
62+
cmd.PersistentFlags().BoolVar(&config.Recursive, "recursive", false, "update submodules recursively (default false)")
63+
cmd.PersistentFlags().StringVar(&config.RecursivePath, "recursive-path", "modules", "submodules path to recursively update")
6164

6265
cmd.PersistentFlags().StringSliceVar(&config.Sections.Show, "show", []string{}, "show section ["+cli.AllSections+"]")
6366
cmd.PersistentFlags().StringSliceVar(&config.Sections.Hide, "hide", []string{}, "hide section ["+cli.AllSections+"]")
@@ -79,14 +82,14 @@ func NewCommand() *cobra.Command {
7982
cmd.PersistentFlags().StringVar(&config.OutputValues.From, "output-values-from", "", "inject output values from file into outputs (default \"\")")
8083

8184
// formatter subcommands
82-
cmd.AddCommand(asciidoc.NewCommand(config))
83-
cmd.AddCommand(json.NewCommand(config))
84-
cmd.AddCommand(markdown.NewCommand(config))
85-
cmd.AddCommand(pretty.NewCommand(config))
86-
cmd.AddCommand(tfvars.NewCommand(config))
87-
cmd.AddCommand(toml.NewCommand(config))
88-
cmd.AddCommand(xml.NewCommand(config))
89-
cmd.AddCommand(yaml.NewCommand(config))
85+
cmd.AddCommand(asciidoc.NewCommand(runtime, config))
86+
cmd.AddCommand(json.NewCommand(runtime, config))
87+
cmd.AddCommand(markdown.NewCommand(runtime, config))
88+
cmd.AddCommand(pretty.NewCommand(runtime, config))
89+
cmd.AddCommand(tfvars.NewCommand(runtime, config))
90+
cmd.AddCommand(toml.NewCommand(runtime, config))
91+
cmd.AddCommand(xml.NewCommand(runtime, config))
92+
cmd.AddCommand(yaml.NewCommand(runtime, config))
9093

9194
// other subcommands
9295
cmd.AddCommand(completion.NewCommand())

cmd/tfvars/hcl/hcl.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,14 @@ import (
1717
)
1818

1919
// NewCommand returns a new cobra.Command for 'tfvars hcl' formatter
20-
func NewCommand(config *cli.Config) *cobra.Command {
20+
func NewCommand(runtime *cli.Runtime, config *cli.Config) *cobra.Command {
2121
cmd := &cobra.Command{
2222
Args: cobra.ExactArgs(1),
2323
Use: "hcl [PATH]",
2424
Short: "Generate HCL format of terraform.tfvars of inputs",
2525
Annotations: cli.Annotations("tfvars hcl"),
26-
PreRunE: cli.PreRunEFunc(config),
27-
RunE: cli.RunEFunc(config),
26+
PreRunE: runtime.PreRunEFunc,
27+
RunE: runtime.RunEFunc,
2828
}
2929
cmd.PersistentFlags().BoolVar(&config.Settings.Description, "description", false, "show Descriptions on variables")
3030
return cmd

0 commit comments

Comments
 (0)