Files
terraform-docs/cmd/root.go
Khosrow Moossavi 1450ee9a10 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>
2021-08-10 18:22:40 -04:00

100 lines
4.4 KiB
Go

/*
Copyright 2021 The terraform-docs Authors.
Licensed under the MIT license (the "License"); you may not
use this file except in compliance with the License.
You may obtain a copy of the License at the LICENSE file in
the root directory of this source tree.
*/
package cmd
import (
"fmt"
"os"
"github.com/spf13/cobra"
"github.com/terraform-docs/terraform-docs/cmd/asciidoc"
"github.com/terraform-docs/terraform-docs/cmd/completion"
"github.com/terraform-docs/terraform-docs/cmd/json"
"github.com/terraform-docs/terraform-docs/cmd/markdown"
"github.com/terraform-docs/terraform-docs/cmd/pretty"
"github.com/terraform-docs/terraform-docs/cmd/tfvars"
"github.com/terraform-docs/terraform-docs/cmd/toml"
versioncmd "github.com/terraform-docs/terraform-docs/cmd/version"
"github.com/terraform-docs/terraform-docs/cmd/xml"
"github.com/terraform-docs/terraform-docs/cmd/yaml"
"github.com/terraform-docs/terraform-docs/internal/cli"
"github.com/terraform-docs/terraform-docs/internal/version"
)
// Execute adds all child commands to the root command and sets flags appropriately.
// This is called by main.main(). It only needs to happen once to the rootCmd.
func Execute() error {
if err := NewCommand().Execute(); err != nil {
fmt.Fprintf(os.Stderr, "Error: %s\n", err.Error())
return err
}
return nil
}
// NewCommand returns a new cobra.Command for 'root' command
func NewCommand() *cobra.Command {
config := cli.DefaultConfig()
runtime := cli.NewRuntime(config)
cmd := &cobra.Command{
Args: cobra.MaximumNArgs(1),
Use: "terraform-docs [PATH]",
Short: "A utility to generate documentation from Terraform modules in various output formats",
Long: "A utility to generate documentation from Terraform modules in various output formats",
Version: version.Full(),
SilenceUsage: true,
SilenceErrors: true,
Annotations: cli.Annotations("root"),
PreRunE: runtime.PreRunEFunc,
RunE: runtime.RunEFunc,
}
// flags
cmd.PersistentFlags().StringVarP(&config.File, "config", "c", ".terraform-docs.yml", "config file name")
cmd.PersistentFlags().BoolVar(&config.Recursive, "recursive", false, "update submodules recursively (default false)")
cmd.PersistentFlags().StringVar(&config.RecursivePath, "recursive-path", "modules", "submodules path to recursively update")
cmd.PersistentFlags().StringSliceVar(&config.Sections.Show, "show", []string{}, "show section ["+cli.AllSections+"]")
cmd.PersistentFlags().StringSliceVar(&config.Sections.Hide, "hide", []string{}, "hide section ["+cli.AllSections+"]")
cmd.PersistentFlags().StringVar(&config.Output.File, "output-file", "", "file path to insert output into (default \"\")")
cmd.PersistentFlags().StringVar(&config.Output.Mode, "output-mode", "inject", "output to file method ["+cli.OutputModes+"]")
cmd.PersistentFlags().StringVar(&config.Output.Template, "output-template", cli.OutputTemplate, "output template")
cmd.PersistentFlags().BoolVar(&config.Output.Check, "output-check", false, "check if content of output file is up to date (default false)")
cmd.PersistentFlags().BoolVar(&config.Sort.Enabled, "sort", true, "sort items")
cmd.PersistentFlags().StringVar(&config.Sort.By, "sort-by", "name", "sort items by criteria ["+cli.SortTypes+"]")
cmd.PersistentFlags().StringVar(&config.HeaderFrom, "header-from", "main.tf", "relative path of a file to read header from")
cmd.PersistentFlags().StringVar(&config.FooterFrom, "footer-from", "", "relative path of a file to read footer from (default \"\")")
cmd.PersistentFlags().BoolVar(&config.Settings.LockFile, "lockfile", true, "read .terraform.lock.hcl if exist")
cmd.PersistentFlags().BoolVar(&config.OutputValues.Enabled, "output-values", false, "inject output values into outputs (default false)")
cmd.PersistentFlags().StringVar(&config.OutputValues.From, "output-values-from", "", "inject output values from file into outputs (default \"\")")
// formatter subcommands
cmd.AddCommand(asciidoc.NewCommand(runtime, config))
cmd.AddCommand(json.NewCommand(runtime, config))
cmd.AddCommand(markdown.NewCommand(runtime, config))
cmd.AddCommand(pretty.NewCommand(runtime, config))
cmd.AddCommand(tfvars.NewCommand(runtime, config))
cmd.AddCommand(toml.NewCommand(runtime, config))
cmd.AddCommand(xml.NewCommand(runtime, config))
cmd.AddCommand(yaml.NewCommand(runtime, config))
// other subcommands
cmd.AddCommand(completion.NewCommand())
cmd.AddCommand(versioncmd.NewCommand())
return cmd
}