diff --git a/cmd/root.go b/cmd/root.go index a1ef306..a33800e 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -60,8 +60,6 @@ func NewCommand() *cobra.Command { 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().BoolVar(&config.Sections.ShowAll, "show-all", true, "show all sections") - cmd.PersistentFlags().BoolVar(&config.Sections.HideAll, "hide-all", false, "hide all sections (default false)") cmd.PersistentFlags().StringVar(&config.Output.File, "output-file", "", "File in module directory to insert output into (default \"\")") cmd.PersistentFlags().StringVar(&config.Output.Mode, "output-mode", "inject", "Output to file method ["+cli.OutputModes+"]") @@ -71,10 +69,15 @@ func NewCommand() *cobra.Command { cmd.PersistentFlags().StringVar(&config.Sort.By, "sort-by", "name", "sort items by criteria ["+cli.SortTypes+"]") // deprecated flags ==> + cmd.PersistentFlags().BoolVar(new(bool), "show-all", true, "show all sections") + cmd.PersistentFlags().BoolVar(new(bool), "hide-all", false, "hide all sections (default false)") + cmd.PersistentFlags().MarkDeprecated("show-all", "more information: https://terraform-docs.io/user-guide/how-to/#visibility-of-sections\n\n") //nolint:errcheck,gosec + cmd.PersistentFlags().MarkDeprecated("hide-all", "more information: https://terraform-docs.io/user-guide/how-to/#visibility-of-sections\n\n") //nolint:errcheck,gosec + cmd.PersistentFlags().BoolVar(&config.Sort.Criteria.Required, "sort-by-required", false, "sort items by name and print required ones first (default false)") cmd.PersistentFlags().BoolVar(&config.Sort.Criteria.Type, "sort-by-type", false, "sort items by type of them (default false)") - cmd.PersistentFlags().MarkDeprecated("sort-by-required", "use '--sort-by required' instead") //nolint:errcheck,gosec - cmd.PersistentFlags().MarkDeprecated("sort-by-type", "use '--sort-by type' instead") //nolint:errcheck,gosec + cmd.PersistentFlags().MarkDeprecated("sort-by-required", "use '--sort-by required' instead\n\n") //nolint:errcheck,gosec + cmd.PersistentFlags().MarkDeprecated("sort-by-type", "use '--sort-by type' instead\n\n") //nolint:errcheck,gosec // <== cmd.PersistentFlags().StringVar(&config.HeaderFrom, "header-from", "main.tf", "relative path of a file to read header from") diff --git a/docs/reference/asciidoc-document.md b/docs/reference/asciidoc-document.md index 1897b48..dc0fc21 100644 --- a/docs/reference/asciidoc-document.md +++ b/docs/reference/asciidoc-document.md @@ -31,7 +31,6 @@ terraform-docs asciidoc document [PATH] [flags] --footer-from string relative path of a file to read footer from (default "") --header-from string relative path of a file to read header from (default "main.tf") --hide strings hide section [data-sources, footer, header, inputs, modules, outputs, providers, requirements, resources] - --hide-all hide all sections (default false) --indent int indention level of AsciiDoc sections [1, 2, 3, 4, 5] (default 2) --output-file string File in module directory to insert output into (default "") --output-mode string Output to file method [inject, replace] (default "inject") @@ -41,7 +40,6 @@ terraform-docs asciidoc document [PATH] [flags] --required show Required column or section (default true) --sensitive show Sensitive column or section (default true) --show strings show section [data-sources, footer, header, inputs, modules, outputs, providers, requirements, resources] - --show-all show all sections (default true) --sort sort items (default true) --sort-by string sort items by criteria [name, required, type] (default "name") --type show Type column or section (default true) diff --git a/docs/reference/asciidoc-table.md b/docs/reference/asciidoc-table.md index 2fe4a45..def87da 100644 --- a/docs/reference/asciidoc-table.md +++ b/docs/reference/asciidoc-table.md @@ -31,7 +31,6 @@ terraform-docs asciidoc table [PATH] [flags] --footer-from string relative path of a file to read footer from (default "") --header-from string relative path of a file to read header from (default "main.tf") --hide strings hide section [data-sources, footer, header, inputs, modules, outputs, providers, requirements, resources] - --hide-all hide all sections (default false) --indent int indention level of AsciiDoc sections [1, 2, 3, 4, 5] (default 2) --output-file string File in module directory to insert output into (default "") --output-mode string Output to file method [inject, replace] (default "inject") @@ -41,7 +40,6 @@ terraform-docs asciidoc table [PATH] [flags] --required show Required column or section (default true) --sensitive show Sensitive column or section (default true) --show strings show section [data-sources, footer, header, inputs, modules, outputs, providers, requirements, resources] - --show-all show all sections (default true) --sort sort items (default true) --sort-by string sort items by criteria [name, required, type] (default "name") --type show Type column or section (default true) diff --git a/docs/reference/asciidoc.md b/docs/reference/asciidoc.md index 95518e0..7989f91 100644 --- a/docs/reference/asciidoc.md +++ b/docs/reference/asciidoc.md @@ -35,14 +35,12 @@ terraform-docs asciidoc [PATH] [flags] --footer-from string relative path of a file to read footer from (default "") --header-from string relative path of a file to read header from (default "main.tf") --hide strings hide section [data-sources, footer, header, inputs, modules, outputs, providers, requirements, resources] - --hide-all hide all sections (default false) --output-file string File in module directory to insert output into (default "") --output-mode string Output to file method [inject, replace] (default "inject") --output-template string Output template (default "\n{{ .Content }}\n") --output-values inject output values into outputs (default false) --output-values-from string inject output values from file into outputs (default "") --show strings show section [data-sources, footer, header, inputs, modules, outputs, providers, requirements, resources] - --show-all show all sections (default true) --sort sort items (default true) --sort-by string sort items by criteria [name, required, type] (default "name") ``` diff --git a/docs/reference/json.md b/docs/reference/json.md index 4664c9d..9ad9fef 100644 --- a/docs/reference/json.md +++ b/docs/reference/json.md @@ -30,14 +30,12 @@ terraform-docs json [PATH] [flags] --footer-from string relative path of a file to read footer from (default "") --header-from string relative path of a file to read header from (default "main.tf") --hide strings hide section [data-sources, footer, header, inputs, modules, outputs, providers, requirements, resources] - --hide-all hide all sections (default false) --output-file string File in module directory to insert output into (default "") --output-mode string Output to file method [inject, replace] (default "inject") --output-template string Output template (default "\n{{ .Content }}\n") --output-values inject output values into outputs (default false) --output-values-from string inject output values from file into outputs (default "") --show strings show section [data-sources, footer, header, inputs, modules, outputs, providers, requirements, resources] - --show-all show all sections (default true) --sort sort items (default true) --sort-by string sort items by criteria [name, required, type] (default "name") ``` diff --git a/docs/reference/markdown-document.md b/docs/reference/markdown-document.md index 7830419..e9e8610 100644 --- a/docs/reference/markdown-document.md +++ b/docs/reference/markdown-document.md @@ -32,7 +32,6 @@ terraform-docs markdown document [PATH] [flags] --footer-from string relative path of a file to read footer from (default "") --header-from string relative path of a file to read header from (default "main.tf") --hide strings hide section [data-sources, footer, header, inputs, modules, outputs, providers, requirements, resources] - --hide-all hide all sections (default false) --indent int indention level of Markdown sections [1, 2, 3, 4, 5] (default 2) --output-file string File in module directory to insert output into (default "") --output-mode string Output to file method [inject, replace] (default "inject") @@ -42,7 +41,6 @@ terraform-docs markdown document [PATH] [flags] --required show Required column or section (default true) --sensitive show Sensitive column or section (default true) --show strings show section [data-sources, footer, header, inputs, modules, outputs, providers, requirements, resources] - --show-all show all sections (default true) --sort sort items (default true) --sort-by string sort items by criteria [name, required, type] (default "name") --type show Type column or section (default true) diff --git a/docs/reference/markdown-table.md b/docs/reference/markdown-table.md index fd8fb32..4322745 100644 --- a/docs/reference/markdown-table.md +++ b/docs/reference/markdown-table.md @@ -32,7 +32,6 @@ terraform-docs markdown table [PATH] [flags] --footer-from string relative path of a file to read footer from (default "") --header-from string relative path of a file to read header from (default "main.tf") --hide strings hide section [data-sources, footer, header, inputs, modules, outputs, providers, requirements, resources] - --hide-all hide all sections (default false) --indent int indention level of Markdown sections [1, 2, 3, 4, 5] (default 2) --output-file string File in module directory to insert output into (default "") --output-mode string Output to file method [inject, replace] (default "inject") @@ -42,7 +41,6 @@ terraform-docs markdown table [PATH] [flags] --required show Required column or section (default true) --sensitive show Sensitive column or section (default true) --show strings show section [data-sources, footer, header, inputs, modules, outputs, providers, requirements, resources] - --show-all show all sections (default true) --sort sort items (default true) --sort-by string sort items by criteria [name, required, type] (default "name") --type show Type column or section (default true) diff --git a/docs/reference/markdown.md b/docs/reference/markdown.md index 451ec53..c448e27 100644 --- a/docs/reference/markdown.md +++ b/docs/reference/markdown.md @@ -36,14 +36,12 @@ terraform-docs markdown [PATH] [flags] --footer-from string relative path of a file to read footer from (default "") --header-from string relative path of a file to read header from (default "main.tf") --hide strings hide section [data-sources, footer, header, inputs, modules, outputs, providers, requirements, resources] - --hide-all hide all sections (default false) --output-file string File in module directory to insert output into (default "") --output-mode string Output to file method [inject, replace] (default "inject") --output-template string Output template (default "\n{{ .Content }}\n") --output-values inject output values into outputs (default false) --output-values-from string inject output values from file into outputs (default "") --show strings show section [data-sources, footer, header, inputs, modules, outputs, providers, requirements, resources] - --show-all show all sections (default true) --sort sort items (default true) --sort-by string sort items by criteria [name, required, type] (default "name") ``` diff --git a/docs/reference/pretty.md b/docs/reference/pretty.md index 8cf0474..a42294c 100644 --- a/docs/reference/pretty.md +++ b/docs/reference/pretty.md @@ -30,14 +30,12 @@ terraform-docs pretty [PATH] [flags] --footer-from string relative path of a file to read footer from (default "") --header-from string relative path of a file to read header from (default "main.tf") --hide strings hide section [data-sources, footer, header, inputs, modules, outputs, providers, requirements, resources] - --hide-all hide all sections (default false) --output-file string File in module directory to insert output into (default "") --output-mode string Output to file method [inject, replace] (default "inject") --output-template string Output template (default "\n{{ .Content }}\n") --output-values inject output values into outputs (default false) --output-values-from string inject output values from file into outputs (default "") --show strings show section [data-sources, footer, header, inputs, modules, outputs, providers, requirements, resources] - --show-all show all sections (default true) --sort sort items (default true) --sort-by string sort items by criteria [name, required, type] (default "name") ``` diff --git a/docs/reference/terraform-docs.md b/docs/reference/terraform-docs.md index 43f4427..17de9b8 100644 --- a/docs/reference/terraform-docs.md +++ b/docs/reference/terraform-docs.md @@ -24,14 +24,12 @@ terraform-docs [PATH] [flags] --header-from string relative path of a file to read header from (default "main.tf") -h, --help help for terraform-docs --hide strings hide section [data-sources, footer, header, inputs, modules, outputs, providers, requirements, resources] - --hide-all hide all sections (default false) --output-file string File in module directory to insert output into (default "") --output-mode string Output to file method [inject, replace] (default "inject") --output-template string Output template (default "\n{{ .Content }}\n") --output-values inject output values into outputs (default false) --output-values-from string inject output values from file into outputs (default "") --show strings show section [data-sources, footer, header, inputs, modules, outputs, providers, requirements, resources] - --show-all show all sections (default true) --sort sort items (default true) --sort-by string sort items by criteria [name, required, type] (default "name") ``` diff --git a/docs/reference/tfvars-hcl.md b/docs/reference/tfvars-hcl.md index 59b9015..8ef0c45 100644 --- a/docs/reference/tfvars-hcl.md +++ b/docs/reference/tfvars-hcl.md @@ -30,14 +30,12 @@ terraform-docs tfvars hcl [PATH] [flags] --footer-from string relative path of a file to read footer from (default "") --header-from string relative path of a file to read header from (default "main.tf") --hide strings hide section [data-sources, footer, header, inputs, modules, outputs, providers, requirements, resources] - --hide-all hide all sections (default false) --output-file string File in module directory to insert output into (default "") --output-mode string Output to file method [inject, replace] (default "inject") --output-template string Output template (default "\n{{ .Content }}\n") --output-values inject output values into outputs (default false) --output-values-from string inject output values from file into outputs (default "") --show strings show section [data-sources, footer, header, inputs, modules, outputs, providers, requirements, resources] - --show-all show all sections (default true) --sort sort items (default true) --sort-by string sort items by criteria [name, required, type] (default "name") ``` diff --git a/docs/reference/tfvars-json.md b/docs/reference/tfvars-json.md index 8beed40..a2b29b4 100644 --- a/docs/reference/tfvars-json.md +++ b/docs/reference/tfvars-json.md @@ -29,14 +29,12 @@ terraform-docs tfvars json [PATH] [flags] --footer-from string relative path of a file to read footer from (default "") --header-from string relative path of a file to read header from (default "main.tf") --hide strings hide section [data-sources, footer, header, inputs, modules, outputs, providers, requirements, resources] - --hide-all hide all sections (default false) --output-file string File in module directory to insert output into (default "") --output-mode string Output to file method [inject, replace] (default "inject") --output-template string Output template (default "\n{{ .Content }}\n") --output-values inject output values into outputs (default false) --output-values-from string inject output values from file into outputs (default "") --show strings show section [data-sources, footer, header, inputs, modules, outputs, providers, requirements, resources] - --show-all show all sections (default true) --sort sort items (default true) --sort-by string sort items by criteria [name, required, type] (default "name") ``` diff --git a/docs/reference/tfvars.md b/docs/reference/tfvars.md index 4d8e763..27a2684 100644 --- a/docs/reference/tfvars.md +++ b/docs/reference/tfvars.md @@ -25,14 +25,12 @@ Generate terraform.tfvars of inputs. --footer-from string relative path of a file to read footer from (default "") --header-from string relative path of a file to read header from (default "main.tf") --hide strings hide section [data-sources, footer, header, inputs, modules, outputs, providers, requirements, resources] - --hide-all hide all sections (default false) --output-file string File in module directory to insert output into (default "") --output-mode string Output to file method [inject, replace] (default "inject") --output-template string Output template (default "\n{{ .Content }}\n") --output-values inject output values into outputs (default false) --output-values-from string inject output values from file into outputs (default "") --show strings show section [data-sources, footer, header, inputs, modules, outputs, providers, requirements, resources] - --show-all show all sections (default true) --sort sort items (default true) --sort-by string sort items by criteria [name, required, type] (default "name") ``` diff --git a/docs/reference/toml.md b/docs/reference/toml.md index 73e76c7..655bfe0 100644 --- a/docs/reference/toml.md +++ b/docs/reference/toml.md @@ -29,14 +29,12 @@ terraform-docs toml [PATH] [flags] --footer-from string relative path of a file to read footer from (default "") --header-from string relative path of a file to read header from (default "main.tf") --hide strings hide section [data-sources, footer, header, inputs, modules, outputs, providers, requirements, resources] - --hide-all hide all sections (default false) --output-file string File in module directory to insert output into (default "") --output-mode string Output to file method [inject, replace] (default "inject") --output-template string Output template (default "\n{{ .Content }}\n") --output-values inject output values into outputs (default false) --output-values-from string inject output values from file into outputs (default "") --show strings show section [data-sources, footer, header, inputs, modules, outputs, providers, requirements, resources] - --show-all show all sections (default true) --sort sort items (default true) --sort-by string sort items by criteria [name, required, type] (default "name") ``` diff --git a/docs/reference/xml.md b/docs/reference/xml.md index bd4faad..039da5f 100644 --- a/docs/reference/xml.md +++ b/docs/reference/xml.md @@ -29,14 +29,12 @@ terraform-docs xml [PATH] [flags] --footer-from string relative path of a file to read footer from (default "") --header-from string relative path of a file to read header from (default "main.tf") --hide strings hide section [data-sources, footer, header, inputs, modules, outputs, providers, requirements, resources] - --hide-all hide all sections (default false) --output-file string File in module directory to insert output into (default "") --output-mode string Output to file method [inject, replace] (default "inject") --output-template string Output template (default "\n{{ .Content }}\n") --output-values inject output values into outputs (default false) --output-values-from string inject output values from file into outputs (default "") --show strings show section [data-sources, footer, header, inputs, modules, outputs, providers, requirements, resources] - --show-all show all sections (default true) --sort sort items (default true) --sort-by string sort items by criteria [name, required, type] (default "name") ``` diff --git a/docs/reference/yaml.md b/docs/reference/yaml.md index a36ff53..7a49c06 100644 --- a/docs/reference/yaml.md +++ b/docs/reference/yaml.md @@ -29,14 +29,12 @@ terraform-docs yaml [PATH] [flags] --footer-from string relative path of a file to read footer from (default "") --header-from string relative path of a file to read header from (default "main.tf") --hide strings hide section [data-sources, footer, header, inputs, modules, outputs, providers, requirements, resources] - --hide-all hide all sections (default false) --output-file string File in module directory to insert output into (default "") --output-mode string Output to file method [inject, replace] (default "inject") --output-template string Output template (default "\n{{ .Content }}\n") --output-values inject output values into outputs (default false) --output-values-from string inject output values from file into outputs (default "") --show strings show section [data-sources, footer, header, inputs, modules, outputs, providers, requirements, resources] - --show-all show all sections (default true) --sort sort items (default true) --sort-by string sort items by criteria [name, required, type] (default "name") ``` diff --git a/docs/user-guide/configuration.md b/docs/user-guide/configuration.md index b3284a3..b6355e9 100644 --- a/docs/user-guide/configuration.md +++ b/docs/user-guide/configuration.md @@ -49,11 +49,12 @@ header-from: main.tf footer-from: "" sections: - hide-all: false hide: [] - show-all: true show: [] + hide-all: false # deprecated in v0.13.0 + show-all: true # deprecated in v0.13.0 + output: file: "" mode: inject @@ -88,6 +89,9 @@ settings: - `sections.hide-all` and `sections.hide` - `sections.show-all` and `sections.show` +**Note:** As of `v0.13.0`, `sections.hide-all` and `settings.show-all` are deprecated +and removed in favor of explicit use of `settings.hide` and `settings.show`. + ## Formatters The following options are supported out of the box by terraform-docs and can be diff --git a/docs/user-guide/how-to.md b/docs/user-guide/how-to.md index d37549b..1f572d7 100644 --- a/docs/user-guide/how-to.md +++ b/docs/user-guide/how-to.md @@ -21,17 +21,49 @@ Error: accepts 1 arg(s), received 2 ## Visibility of Sections Output generated by `terraform-docs` consists of different [sections] which are -visible by default. The visibility of these can be controlled by one or combination -of: +visible by default. The visibility of these can be controlled by one of the following +options: -- `--show-all` -- `--hide-all` - `--show ` -- and `--hide ` +- `--hide ` +- `--show-all` (deprecated in `v0.13.0`) +- `--hide-all` (deprecated in `v0.13.0`) -```console -terraform-docs --show-all --hide header ... # show all sections except 'header' -terraform-docs --hide-all --show inputs --show outputs ... # hide all sections except 'inputs' and 'outputs' +As of `v0.13.0` flags `--show-all` and `--hide-all` are deprecated in favor of +explicit use of `--show` and `--hide`. In other words when `--show
` is +used, only `
` will be shown. If you want to show multiple sections and +hide the rest you can specify `--show` flag multiple times. The same logic is also +applied to `--hide`. + +```bash +# show 'inputs' and hide everything else +$ terraform-docs --show inputs + +# show 'inputs' and show 'outputs' and hide everything else +$ terraform-docs --show inputs --show outputs + +# hide 'header' and show everything else +$ terraform-docs --hide header + +# hide 'header' and hide 'providers' and show everything else +$ terraform-docs --hide header --hide providers +``` + +**Note:** Using `--show` or `--hide` CLI flag will completely override the values +from `.terraform-docs.yml`. + +```bash +$ cat .terraform-docs.yml +sections: + show: + - inputs + - outputs + +# example 1: this will only show 'providers' +$ terraform-docs --show providers . + +# example 2: this will hide 'inputs' and hide 'providers' and show everything else +$ terraform-docs --hide inputs --hide providers . ``` ## Module Header diff --git a/examples/.terraform-docs.yml b/examples/.terraform-docs.yml index 6c71a18..be37563 100644 --- a/examples/.terraform-docs.yml +++ b/examples/.terraform-docs.yml @@ -3,7 +3,6 @@ header-from: doc.txt footer-from: footer.md sections: - hide-all: true show: - header - inputs diff --git a/internal/cli/config.go b/internal/cli/config.go index b0d98e3..7712cb4 100644 --- a/internal/cli/config.go +++ b/internal/cli/config.go @@ -46,10 +46,8 @@ var allSections = []string{ var AllSections = strings.Join(allSections, ", ") type sections struct { - Show []string `yaml:"show"` - Hide []string `yaml:"hide"` - ShowAll bool `yaml:"show-all"` - HideAll bool `yaml:"hide-all"` + Show []string `yaml:"show"` + Hide []string `yaml:"hide"` dataSources bool `yaml:"-"` header bool `yaml:"-"` @@ -64,10 +62,8 @@ type sections struct { func defaultSections() sections { return sections{ - Show: []string{}, - Hide: []string{}, - ShowAll: true, - HideAll: false, + Show: []string{}, + Hide: []string{}, dataSources: false, header: false, @@ -81,44 +77,25 @@ func defaultSections() sections { } } -func (s *sections) validate() error { //nolint:gocyclo - // NOTE(khos2ow): this function is over our cyclomatic complexity goal. - // Be wary when adding branches, and look for functionality that could - // be reasonably moved into an injected dependency. - +func (s *sections) validate() error { + if len(s.Show) > 0 && len(s.Hide) > 0 { + return fmt.Errorf("'--show' and '--hide' can't be used together") + } for _, item := range s.Show { - switch item { - case allSections[0], allSections[1], allSections[2], allSections[3], allSections[4], allSections[5], allSections[6], allSections[7], allSections[8]: - default: + if !contains(allSections, item) { return fmt.Errorf("'%s' is not a valid section", item) } } for _, item := range s.Hide { - switch item { - case allSections[0], allSections[1], allSections[2], allSections[3], allSections[4], allSections[5], allSections[6], allSections[7], allSections[8]: - default: + if !contains(allSections, item) { return fmt.Errorf("'%s' is not a valid section", item) } } - if s.ShowAll && s.HideAll { - return fmt.Errorf("'--show-all' and '--hide-all' can't be used together") - } - if s.ShowAll && len(s.Show) != 0 { - return fmt.Errorf("'--show-all' and '--show' can't be used together") - } - if s.HideAll && len(s.Hide) != 0 { - return fmt.Errorf("'--hide-all' and '--hide' can't be used together") - } return nil } func (s *sections) visibility(section string) bool { - if s.ShowAll && !s.HideAll { - for _, n := range s.Hide { - if n == section { - return false - } - } + if len(s.Show) == 0 && len(s.Hide) == 0 { return true } for _, n := range s.Show { @@ -131,7 +108,9 @@ func (s *sections) visibility(section string) bool { return false } } - return false + // hidden : if s.Show NOT empty AND s.Show does NOT contain section + // visible: if s.Hide NOT empty AND s.Hide does NOT contain section + return len(s.Hide) > 0 } const ( @@ -277,14 +256,7 @@ func defaultSort() sort { } func (s *sort) validate() error { - found := false - for _, item := range allSorts { - if item == s.By { - found = true - break - } - } - if !found { + if !contains(allSorts, s.By) { return fmt.Errorf("'%s' is not a valid sort type", s.By) } if s.Criteria.Required && s.Criteria.Type { @@ -356,13 +328,6 @@ func DefaultConfig() *Config { // process provided Config func (c *Config) process() { // sections - if c.Sections.HideAll && !changedfs["show-all"] { - c.Sections.ShowAll = false - } - if !c.Sections.ShowAll && !changedfs["hide-all"] { - c.Sections.HideAll = true - } - c.Sections.dataSources = c.Sections.visibility("data-sources") c.Sections.header = c.Sections.visibility("header") c.Sections.footer = c.Sections.visibility("footer") @@ -401,8 +366,8 @@ func (c *Config) validate() error { //nolint:gocyclo return fmt.Errorf("value of '--header-from' can't be empty") } - // footer-from, not a 'default' section so can be empty even if show-all enabled - if c.Sections.footer && !c.Sections.ShowAll && c.FooterFrom == "" { + // footer-from, not a 'default' section so can be empty + if c.Sections.footer && c.FooterFrom == "" { return fmt.Errorf("value of '--footer-from' can't be empty") } diff --git a/internal/cli/reader.go b/internal/cli/reader.go index 39e48ca..60d8a65 100644 --- a/internal/cli/reader.go +++ b/internal/cli/reader.go @@ -54,11 +54,121 @@ func (c *cfgreader) parse() error { //nolint:gocyclo return err } - if c.config.Sections.HideAll && !changedfs["show-all"] { - c.config.Sections.ShowAll = false + mappings := map[string]struct { + flag string + from interface{} + to interface{} + }{ + "header-from": { + flag: "header-from", + from: &c.overrides, + to: c.config, + }, + "footer-from": { + flag: "footer-from", + from: &c.overrides, + to: c.config, + }, + + // sort + "sort": { + flag: "enabled", + from: &c.overrides.Sort, + to: &c.config.Sort, + }, + "sort-by": { + flag: "by", + from: &c.overrides.Sort, + to: &c.config.Sort, + }, + "sort-by-required": { + flag: "required", + from: nil, + to: nil, + }, + "sort-by-type": { + flag: "type", + from: nil, + to: nil, + }, + + // output + "output-file": { + flag: "file", + from: &c.overrides.Output, + to: &c.config.Output, + }, + "output-mode": { + flag: "mode", + from: &c.overrides.Output, + to: &c.config.Output, + }, + "output-template": { + flag: "template", + from: &c.overrides.Output, + to: &c.config.Output, + }, + + // output-values + "output-values": { + flag: "enabled", + from: &c.overrides.OutputValues, + to: &c.config.OutputValues, + }, + "output-values-from": { + flag: "from", + from: &c.overrides.OutputValues, + to: &c.config.OutputValues, + }, + + // settings + "anchor": { + flag: "anchor", + from: &c.overrides.Settings, + to: &c.config.Settings, + }, + "color": { + flag: "color", + from: &c.overrides.Settings, + to: &c.config.Settings, + }, + "default": { + flag: "default", + from: &c.overrides.Settings, + to: &c.config.Settings, + }, + "escape": { + flag: "escape", + from: &c.overrides.Settings, + to: &c.config.Settings, + }, + "indent": { + flag: "indent", + from: &c.overrides.Settings, + to: &c.config.Settings, + }, + "required": { + flag: "required", + from: &c.overrides.Settings, + to: &c.config.Settings, + }, + "sensitive": { + flag: "sensitive", + from: &c.overrides.Settings, + to: &c.config.Settings, + }, + "type": { + flag: "type", + from: &c.overrides.Settings, + to: &c.config.Settings, + }, } - if !c.config.Sections.ShowAll && !changedfs["hide-all"] { - c.config.Sections.HideAll = true + + // If '--show' or '--hide' CLI flag is used, explicitly override and remove + // all items from 'show' and 'hide' set in '.terraform-doc.yml'. + if changedfs["show"] || changedfs["hide"] { + c.config.Sections.Show = []string{} + c.config.Sections.Hide = []string{} } for flag, enabled := range changedfs { @@ -67,34 +177,17 @@ func (c *cfgreader) parse() error { //nolint:gocyclo } switch flag { - case "header-from", "footer-from": - if err := c.overrideValue(flag, c.config, &c.overrides); err != nil { - return err - } case "show": c.overrideShow() case "hide": c.overrideHide() - case "sort", "sort-by": - mapping := map[string]string{"sort": "enabled", "sort-by": "by"} - if err := c.overrideValue(mapping[flag], &c.config.Sort, &c.overrides.Sort); err != nil { - return err - } case "sort-by-required", "sort-by-type": - mapping := map[string]string{"sort-by-required": "required", "sort-by-type": "type"} - c.config.Sort.By = mapping[flag] - case "output-file", "output-mode", "output-template": - mapping := map[string]string{"output-file": "file", "output-mode": "mode", "output-template": "template"} - if err := c.overrideValue(mapping[flag], &c.config.Output, &c.overrides.Output); err != nil { - return err + c.config.Sort.By = mappings[flag].flag + default: + if _, ok := mappings[flag]; !ok { + continue } - case "output-values", "output-values-from": - mapping := map[string]string{"output-values": "enabled", "output-values-from": "from"} - if err := c.overrideValue(mapping[flag], &c.config.OutputValues, &c.overrides.OutputValues); err != nil { - return err - } - case "anchor", "color", "default", "escape", "indent", "required", "sensitive", "type": - if err := c.overrideValue(flag, &c.config.Settings, &c.overrides.Settings); err != nil { + if err := c.overrideValue(mappings[flag].flag, mappings[flag].to, mappings[flag].from); err != nil { return err } } @@ -119,30 +212,16 @@ func (c *cfgreader) overrideValue(name string, to interface{}, from interface{}) func (c *cfgreader) overrideShow() { for _, item := range c.overrides.Sections.Show { - if c.config.Sections.ShowAll { - if contains(c.config.Sections.Hide, item) { - c.config.Sections.Hide = remove(c.config.Sections.Hide, item) - c.config.Sections.Show = remove(c.config.Sections.Show, item) - } - } else { - if !contains(c.config.Sections.Show, item) { - c.config.Sections.Show = append(c.config.Sections.Show, item) - } + if !contains(c.config.Sections.Show, item) { + c.config.Sections.Show = append(c.config.Sections.Show, item) } } } func (c *cfgreader) overrideHide() { for _, item := range c.overrides.Sections.Hide { - if c.config.Sections.HideAll { - if contains(c.config.Sections.Show, item) { - c.config.Sections.Show = remove(c.config.Sections.Show, item) - c.config.Sections.Hide = remove(c.config.Sections.Hide, item) - } - } else { - if !contains(c.config.Sections.Hide, item) { - c.config.Sections.Hide = append(c.config.Sections.Hide, item) - } + if !contains(c.config.Sections.Hide, item) { + c.config.Sections.Hide = append(c.config.Sections.Hide, item) } } } diff --git a/internal/cli/reader_test.go b/internal/cli/reader_test.go index 8d6c013..5e38540 100644 --- a/internal/cli/reader_test.go +++ b/internal/cli/reader_test.go @@ -189,34 +189,30 @@ func TestOverrideShow(t *testing.T) { name string show []string hide []string - showall bool overrideShow []string expectedShow []string expectedHide []string }{ { name: "override section show", - show: []string{""}, + show: []string{}, hide: []string{"inputs", "outputs"}, - showall: true, overrideShow: []string{"inputs"}, - expectedShow: []string{""}, - expectedHide: []string{"outputs"}, + expectedShow: []string{"inputs"}, + expectedHide: []string{"inputs", "outputs"}, }, { name: "override section show", show: []string{"providers"}, hide: []string{"inputs"}, - showall: true, overrideShow: []string{"outputs"}, - expectedShow: []string{"providers"}, + expectedShow: []string{"providers", "outputs"}, expectedHide: []string{"inputs"}, }, { name: "override section show", show: []string{"inputs"}, hide: []string{"providers"}, - showall: false, overrideShow: []string{"outputs"}, expectedShow: []string{"inputs", "outputs"}, expectedHide: []string{"providers"}, @@ -225,7 +221,6 @@ func TestOverrideShow(t *testing.T) { name: "override section show", show: []string{"inputs"}, hide: []string{"inputs"}, - showall: false, overrideShow: []string{"inputs"}, expectedShow: []string{"inputs"}, expectedHide: []string{"inputs"}, @@ -240,7 +235,6 @@ func TestOverrideShow(t *testing.T) { c.config.Sections.Show = tt.show c.config.Sections.Hide = tt.hide - c.config.Sections.ShowAll = tt.showall c.overrides.Sections.Show = tt.overrideShow c.overrideShow() @@ -256,7 +250,6 @@ func TestOverrideHide(t *testing.T) { name string show []string hide []string - hideall bool overrideHide []string expectedShow []string expectedHide []string @@ -264,26 +257,23 @@ func TestOverrideHide(t *testing.T) { { name: "override section hide", show: []string{"inputs", "outputs"}, - hide: []string{""}, - hideall: true, + hide: []string{}, overrideHide: []string{"inputs"}, - expectedShow: []string{"outputs"}, - expectedHide: []string{""}, + expectedShow: []string{"inputs", "outputs"}, + expectedHide: []string{"inputs"}, }, { name: "override section hide", show: []string{"inputs"}, hide: []string{"providers"}, - hideall: true, overrideHide: []string{"outputs"}, expectedShow: []string{"inputs"}, - expectedHide: []string{"providers"}, + expectedHide: []string{"providers", "outputs"}, }, { name: "override section hide", show: []string{"providers"}, hide: []string{"inputs"}, - hideall: false, overrideHide: []string{"outputs"}, expectedShow: []string{"providers"}, expectedHide: []string{"inputs", "outputs"}, @@ -292,7 +282,6 @@ func TestOverrideHide(t *testing.T) { name: "override section hide", show: []string{"inputs"}, hide: []string{"inputs"}, - hideall: false, overrideHide: []string{"inputs"}, expectedShow: []string{"inputs"}, expectedHide: []string{"inputs"}, @@ -307,7 +296,6 @@ func TestOverrideHide(t *testing.T) { c.config.Sections.Show = tt.show c.config.Sections.Hide = tt.hide - c.config.Sections.HideAll = tt.hideall c.overrides.Sections.Hide = tt.overrideHide c.overrideHide()