44 Commits

Author SHA1 Message Date
Khosrow Moossavi
051be1f1de Make main module doc optional when in recursive generation
Signed-off-by: Khosrow Moossavi <khos2ow@gmail.com>
2024-05-30 18:37:23 -04:00
Khosrow Moossavi
8f74fd4453 feat: ignore outputs, providers, resources with comments
Prepend any type of resource, including `resource`, `data`, `module`,
`variable`, and `output` with a comment including "terraform-docs-ignore"
to exclude it from the generated output.

Signed-off-by: Khosrow Moossavi <khos2ow@gmail.com>
2024-05-30 12:33:14 -04:00
Khosrow Moossavi
5727f8b412 Preserve whitespaces of provided content and template
Signed-off-by: Khosrow Moossavi <khos2ow@gmail.com>
2022-01-07 18:14:21 -05:00
Khosrow Moossavi
4a9ffe7c33 Move plugin-sdk to in-tree in core repository
Moving terraform-docs/plugin-sdk standalone module to in-tree, because
maintaining both of them, specifically if anything needs to be added to
Config, or terraform will required dual effort on both repository. As
such now everything is consolidated under one repository. Example usage
for plugin developer after this move is as follow:

```go
package main

import (
	_ "embed" //nolint

	"github.com/terraform-docs/terraform-docs/plugin"
	"github.com/terraform-docs/terraform-docs/print"
	"github.com/terraform-docs/terraform-docs/template"
	"github.com/terraform-docs/terraform-docs/terraform"
)

func main() {
	plugin.Serve(&plugin.ServeOpts{
		Name:    "template",
		Version: "0.1.0",
		Printer: printer,
	})
}

//go:embed sections.tmpl
var tplCustom []byte

// Print the custom format template. You have all the flexibility to generate
// the output however you choose to.
func printer(config *print.Config, module *terraform.Module) (string, error) {
	tpl := template.New(config,
		&template.Item{Name: "custom", Text: string(tplCustom)},
	)

	rendered, err := tpl.Render("custom", module)
	if err != nil {
		return "", err
	}

	return rendered, nil
}
```

Signed-off-by: Khosrow Moossavi <khos2ow@gmail.com>
2021-10-05 18:54:49 -04:00
Khosrow Moossavi
465dd14cff Make terraform.Module available in content
Add one extra special variable the `content`:

- `{{ .Module }}`

As opposed to the other variables, which are generated sections based on
a selected formatter, the `{{ .Module }}` variable is just a `struct`
representing a Terraform module.

It can be used to build highly complex and highly customized content:

```yaml
content: |-
  ## Resources

  {{ range .Module.Resources }}
  - {{ .GetMode }}.{{ .Spec }} ({{ .Position.Filename }}#{{ .Position.Line }})
  {{- end }}
```

Signed-off-by: Khosrow Moossavi <khos2ow@gmail.com>
2021-10-04 20:01:44 -04:00
Khosrow Moossavi
54dc0f5a7c Add recursive config to .terraform-docs.yml file
Up to now there was only one way to enable recursive execution and that
was with `--recursive` CLI flag. This enables the same behavior but
within config file (i.e. `.terraform-docs.yml`)

Example:

```yaml
recursive:
  enabled: false
  path: modules
```

Signed-off-by: Khosrow Moossavi <khos2ow@gmail.com>
2021-09-30 15:53:22 -04:00
Khosrow Moossavi
f3c92384a1 Extract leading comments for resources and modules
Signed-off-by: Khosrow Moossavi <khos2ow@gmail.com>
2021-09-29 13:55:50 -04:00
Khosrow Moossavi
61cc005548 Render resources without URL correctly in Markdown and Asciidoc
Signed-off-by: Khosrow Moossavi <khos2ow@gmail.com>
2021-06-17 18:17:43 -04:00
Edgar R. Sandi
0284283bd1 Separate the module version from module source when using ?ref=
Signed-off-by: Edgar R. Sandi <edgar.r.sandi@gmail.com>
2021-05-18 20:51:50 -03:00
Khosrow Moossavi
ea34bce326 Include relative files into generated content
Local relative files can be included automatically in the generated
content with `{{ include "..." }} function. This can be very useful
for:

- inject arbitrary markdown files in between sections
- automatically include example usage

````yaml
content: |-
  include any relative files

  {{ include "relative/path/to/file" }}

  or examples

  ```hcl
  {{ include "examples/foo/main.tf" }}
  ```
````

Signed-off-by: Khosrow Moossavi <khos2ow@gmail.com>
2021-05-13 12:53:25 -04:00
Khosrow Moossavi
6777364257 Customize content with individual sections output
Generated content can be customized further away with `content` in configuration.
If the `content` is empty the default orders of section is used. `content` is a
Go template with following additional variables:

- `{{ .Header }}`
- `{{ .Footer }}`
- `{{ .Inputs }}`
- `{{ .Modules }}`
- `{{ .Outputs }}`
- `{{ .Providers }}`
- `{{ .Requirements }}`
- `{{ .Resources }}`

```yaml
content: |-
  Any arbitrary text can be placed anywhere in the content

  {{ .Header }}

  and even in between sections

  {{ .Providers }}

  and they don't even need to be in the default order

  {{ .Outputs }}

  {{ .Inputs }}
```

These variables are the generated output of individual sections in the selected
formatter. For example `{{ .Inputs }}` is Markdown Table representation of inputs
when formatter is set to `markdown table` and AsciiDoc Document representation
when formatter is set to `asciidoc document` and so on.

Compatible formats for customized content are:

- `asciidoc document`
- `asciidoc table`
- `markdown document`
- `markdown table`

Signed-off-by: Khosrow Moossavi <khos2ow@gmail.com>
2021-05-12 16:12:38 -04:00
Khosrow Moossavi
dabf54f29f Version constraints configuration
terraform-docs version constraints is almost identical to the syntax
used by Terraform. A version constraint is a string literal containing
one or more condition, which are separated by commas.

```yaml
version: ">= 0.13.0, < 1.0.0"
```

Each condition consists of an operator and a version number. A version
number is a series of numbers separated by dots (e.g. `0.13.0`). Note
that version number should not have leading `v` in it.

Valid operators are as follow:

- `=` (or no operator): allows for exact version number.
- `!=`: exclude an exact version number.
- `>`, `>=`, `<`, and `<=`: comparisons against a specific version.
- `~>`: only the rightmost version component to increment.

Signed-off-by: Khosrow Moossavi <khos2ow@gmail.com>
2021-04-26 19:45:34 -04:00
Khosrow Moossavi
8c531b6544 Deprecate --show-all and --hide-all flags
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 <section>` is used, only `<section>` will be shown. If you want
to show multiple sections and hide the rest you can specify multiple
`--show` flags. The same logic is also applied to `--hide`.

    # show 'inputs' and hide everything else
    $ terraform-docs --show inputs <formatter>

    # show 'inputs' and show 'outputs' and hide everything else
    $ terraform-docs --show inputs --show outputs <formatter>

    # hide 'header' and show everything else
    $ terraform-docs --hide header <formatter>

    # hide 'header' and hide 'providers' and show everything else
    $ terraform-docs --hide header --hide providers <formatter>

Note: Using `--show` or `--hide` CLI flag will completely override the
values from `.terraform-docs.yml`. Example:

    $ 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 .

Signed-off-by: Khosrow Moossavi <khos2ow@gmail.com>
2021-04-20 11:50:58 -04:00
Khosrow Moossavi
557d53dd1e Deprecate '--sort-by-XX' in favor of '--sort-by XX'
This deprecates sort by flags in favor of their corresponding dynamic
valued ones. Affected flags are:

- `--sort-by-required`
- `--sort-by-type`

In return new `--sort-by string` is added with following values:

- `name` (default)
- `required`
- `type`

Note that the behavior of `--sort bool` was not changed and to disable
sorting altogether you can run `--sort false`.

Signed-off-by: Khosrow Moossavi <khos2ow@gmail.com>
2021-04-12 18:34:42 -04:00
Khosrow Moossavi
ac5a29c6f6 Implement module sort by position (no sort)
Signed-off-by: Khosrow Moossavi <khos2ow@gmail.com>
2021-03-15 18:26:54 -04:00
Simon Clifford
aa1e6bbf59 Add support for footer docs
Enables a footer to be appended to the end of a generated document
sourced from tf files or documents in the same way as the header

Adds the `footer-from` field to the config yml
Adds the `--footer-from` flag to the cli

Signed-off-by: Simon Clifford <siclifford@gmail.com>
Signed-off-by: Khosrow Moossavi <khos2ow@gmail.com>
2021-03-15 14:51:00 -04:00
Khosrow Moossavi
7e489d2bbb Do not modify header lines and trailing whitespaces
This is going to preserve line-ending of extracted header text as they
are provided by users. In other words we are going to always assume the
header text is formatted in the "intended" way by users. So we're never
going to modify line-endings and won't append `<SPACE><SPACE>` at the
end of paragraph lines.

Signed-off-by: Khosrow Moossavi <khos2ow@gmail.com>
2021-03-13 13:43:03 -05:00
Khosrow Moossavi
a3bafd6018 Save generated content directly into a file
There are two modes for saving into file:

- inject: partially replace the target file with generated output
- replace: completely replace the target file with generated output

The output generated by formatters (markdown, asciidoc, etc) will be
inserted into a template before getting saved into the file. This
template can be customized with '--output-template' CLI flag or
corresponding item in '.terraform-docs.yml' config file. Its default
value is:

```
<!-- BEGIN_TF_DOCS -->
{{ .Content }}
<!-- END_TF_DOCS -->
```

This consists of three items, all of them are mandatory:

- begin comment
- content variable
- end comment

You may change the wording of comment as you wish, but the comment must
be present in the template.

Signed-off-by: Khosrow Moossavi <khos2ow@gmail.com>
2021-03-09 16:40:22 -05:00
Khosrow Moossavi
3d39a94e39 Add option to hide Type and Default columns
Two new flags are added: '--default bool' and '--type bool' to
control the visibility of Default and Type columns and section
respectively in Markdown and AsciiDoc.

Signed-off-by: Khosrow Moossavi <khos2ow@gmail.com>
2021-03-02 14:17:03 -05:00
Heiko Neblung
2575a40e78 added output section for modulecalls
Signed-off-by: Heiko Neblung <Heiko.Neblung@t-systems.com>
2021-02-10 05:40:50 +01:00
Julien Duchesne
52653b5107 fix: Render special chars in variables' default value properly (#284)
* Fix: No longer crashes when there are special chars in a variable default

* Do not escape chars everywhere

* Cleanup go.mod
2020-07-17 13:13:50 -04:00
Khosrow Moossavi
fd97ec5930 feat: Add support for .terraform-docs.yml config file (#272)
* Add support for .terraform-docs.yml config file

* add config reader

* add usage documentation and reference guide

* typo

* update docs
2020-07-13 22:06:30 -04:00
Khosrow Moossavi
fbbf01e451 Read header from .adoc, .md and .txt file as well (#252)
* Allow reading header from a markdown file

* wording

* add supports for .adoc and .txt as well

* wording

* revert docs

Signed-off-by: Khosrow Moossavi <khos2ow@gmail.com>
2020-04-29 15:30:48 -04:00
Khosrow Moossavi
8f930437fa fix: Make sure requirements section is sorted (#233)
Signed-off-by: Khosrow Moossavi <khos2ow@gmail.com>
2020-04-01 17:25:52 -04:00
Khosrow Moossavi
a3e0a56ce6 fix: Mark variables not required if default set to null (#221)
* Mark variables not required if default set to null

* Move github.com/hashicorp/terraform-config-inspect to internal/tfconfig

Internally we depend on terraform-config-inspect, but at the moment
they state that they consider the project is feature-complete and
they do not accept any enhancement pull requests, as such we've
decided to bring over the project as internal package here rather than
being a vendor dependency and apply the fix from @jstewmon from
https://github.com/hashicorp/terraform-config-inspect/pull/24 directly
here.

Since the terraform-config-inspect is considered to be feature-complete
we don't expect to have any more changes on the package, and if there
was a change on upstream we're going to bring it down in the
corresponding package.

* Add notice to the new package

* add license of terraform-config-inspect code

* fix tests after merging master

* fix test after merge master

* show 'required' attribute in JSON, XML, YAML

* update docs
2020-03-30 16:16:55 -04:00
Khosrow Moossavi
b6241751d9 feat: Add section for module requirements (#222)
* Add section for module requirements

* cleanup

* reorder items logically
2020-03-24 14:20:13 -04:00
Khosrow Moossavi
01c8fa1c61 feat: Add support for fetching the module header from any file (#217)
* Add support for fetching the module header from any file

* fix the failing test because of lines reader
2020-03-12 13:58:35 -04:00
Khosrow Moossavi
4ff4582dff feat: Show sensitivity of the output value in rendered result (#207) 2020-02-26 12:08:24 -05:00
Khosrow Moossavi
743d4f3ebc refactor: Add Default value types for better marshalling (#196)
* Add Default value types for better marshalling

* fix output-values tests
2020-02-19 15:37:48 -05:00
Gretchen Shelby-Dormer
31cdef0f67 feat: Extract and render output values from Terraform (#191)
* Allow users to pass '--output-values'

Pass empty string to CreateModule.outputValuePath

* Fix bug causing 'pretty' tests to fail

* Link formats documentation in README (#181)

Co-authored-by: Khosrow Moossavi <khos2ow@gmail.com>

* docs: Auto generate formats document from examples (#192)

* Auto generate formats document from examples

* fix lint issues

* refactor: Add tfconf.Options to load Module with (#193)

* Update Changelog

* Allow users to pass '--output-values'

Read the outputValuesPath from an env variable

Use an env var with a path for '--output-values'

Update Changelog

Use an env var with a path for '--output-values'

Update Changelog

properly write output values for evrythng but yaml

* Fix failing json+yaml tests

* Remove unneeded code block from output.go

* Remove unused import statement from output.go

* Fix some noob mistakes

* Create two flags to use for output value injection

* Fix bug vanilla commands+build test to fail

* Modify all tests and add new for outputvalues

* Modify to include many output types

* Optimize imports to appease checkfmt

* Create loadOutputValues function

* Fix linter issue

* Code review fixes. Hopefully the final commit!

* appease linter

* Not allow sensitive output values to be injected

* Remove trailing slash from tests

* Remove default values from `--output-values-from`

Co-authored-by: Martyn Ranyard <iMartyn@users.noreply.github.com>
Co-authored-by: Khosrow Moossavi <khos2ow@gmail.com>
2020-02-19 11:46:43 -05:00
Khosrow Moossavi
bf07706947 fix: Do not escape markdown table inside module header (#186) 2020-02-01 14:21:54 -05:00
Khosrow Moossavi
43f69d337f fix: Preserve asterisk list in header and fix escaping (#179) 2020-01-30 18:47:32 -05:00
Khosrow Moossavi
bf38a75fe3 fix: Add newline between code block and trailing lines (#184) 2020-01-30 18:09:45 -05:00
Khosrow Moossavi
6ff46a26b1 fix: Do not escape any characters of a URL (#170) 2020-01-17 13:45:48 -05:00
Khosrow Moossavi
82e87aa264 fix: Do not escape strings inside code blocks (#155) 2020-01-12 15:20:12 -05:00
Khosrow Moossavi
b3112d135a fix: Read leading module header from main.tf (#154)
Supported comment formats is:
- jsdoc, c or java-like multi-line comment `/** **/`

BREAKING CHANGE: - In the JSON format respone, module "Comment" has been renamed to module `header`.
2020-01-10 16:40:48 -05:00
Khosrow Moossavi
a6193849c4 fix: Read leading comment lines if description is not provided (#151)
Supported comment formats are:

- multi-lines start with `#` comment format
- multi-lines start with `//` comment format

Unsupported comments formats is:

- jsdoc, c or java-like multi-line comment `/** **/`
2020-01-07 21:49:56 -05:00
Khosrow Moossavi
ce32bf10a9 feat: Show 'providers' information (#140)
* Show 'providers' information

* Fix for unsorted providers list

BREAKING CHANGE: - With Terraform 0.12 the information about `providers` being used in the module will be generated by default. This will cause the first generation of documents with the latest release of `terraform-docs` binary be slightly different than before, now there will be `Providers` section in Markdown and `providers` block in JSON. You can ignore this by using new `--no-providers` flag if you choose to.

Originally-Authored-By: Thomas Alton <thomas.alton@gmail.com>
Original-Pull-Request: #113
2020-01-03 18:33:08 -05:00
Thomas Alton
b8da8c5020 feat: Support Terraform 0.12.x configuration (#113)
* Support 0.12 configuration

* Move code out of GOPATH for CircleCI

* cleanup

* Rename 'variables' back to 'input' just for now

* Normalizing 'print.s.Settings' usage

* Normalize settings continued

* Remove '--providers' functionality for now

* Adjust 'commands' based on moarta's changes

* Adjust 'pkg/doc' based on moarta's changes

* Adjust 'pkg/print/json' based on moarta's changes

* Fix json tests

* don't trim whitespaces from .tf files

* Adjust 'pkg/print/pretty' based on moarta's changes

* Fix pretty tests

* don't trim whitespaces from .golden files

* Show 'n/a' for empty description for 'pretty'

* Show 'any' if type of input variable is missing

* Adjust 'pkg/print/markdown/document' based on moarta's changes

* Fix document tests

* move back vendor files

* Adjust 'pkg/print/markdown/table' based on moarta's changes

* fix lint issue

* Fix table tests

* figure out input type based on default value if tpye not explicitly defined

* don't escape ( ) [ ] { }, as they don't cause any issue

Authored-by: Thomas Alton <thomas.alton@gmail.com>
Co-authored-by: Khosrow Moossavi <khos2ow@gmail.com>
2019-12-20 15:07:29 -05:00
Hugues Malphettes
ab42f0d097 Code blocks support for all formats. Single line break support (#123)
*  HTML-ize code blocks inside a markdown table. Single line break.

 Add support for embedding markdown code blocks inside markdown tables

 Add support for single-line breaks: when the line ends with 2 spaces, place
 a single <br>.

* Fix the testsuite, todo improve the escape code for document

* Add the WithIndent testdata
2019-12-11 14:48:49 -05:00
Khosrow Moossavi
f0a7d374a5 Enhance Makefile and add editorconfig (#115) 2019-09-23 20:27:11 +02:00
Khosrow Moossavi
9f63bdf484 Escape pipe character when generating Markdown (#114) 2019-06-28 19:19:08 +02:00
Martin Etmajer
18939caf6d Unify default values of inputs (#97) 2018-12-13 08:59:30 +01:00
Martin Etmajer
750d500622 Move Terraform test configuration to folder 'examples'. 2018-12-02 19:44:21 +01:00