Skip to content

Commit 3d2b878

Browse files
committed
Overhaul README and docs improvements
Signed-off-by: Khosrow Moossavi <khos2ow@gmail.com>
1 parent 3d84cad commit 3d2b878

File tree

7 files changed

+264
-58
lines changed

7 files changed

+264
-58
lines changed

README.md

Lines changed: 234 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -12,42 +12,31 @@ Sponsored by [Scalr - Terraform Automation & Collaboration Software](https://sca
1212

1313
A utility to generate documentation from Terraform modules in various output formats.
1414

15-
## Documentation
16-
17-
- **Users**
18-
- Read the [User Guide] to learn how to use terraform-docs
19-
- Read the [Formats Guide] to learn about different output formats of terraform-docs
20-
- Refer to [Config File Reference] for all the available configuration options
21-
- **Developers**
22-
- Read [Contributing Guide] before submitting a pull request
23-
24-
Visit [our website] for all documentation.
25-
2615
## Installation
2716

28-
The latest version can be installed using `go get`:
17+
macOS users can install using [Homebrew]:
2918

3019
```bash
31-
GO111MODULE="on" go get github.com/terraform-docs/terraform-docs@v0.15.0
20+
brew install terraform-docs
3221
```
3322

34-
**NOTE:** to download any version **before** `v0.9.1` (inclusive) you need to use to
35-
old module namespace (`segmentio`):
23+
or
3624

3725
```bash
38-
# only for v0.9.1 and before
39-
GO111MODULE="on" go get github.com/segmentio/terraform-docs@v0.9.1
26+
brew install terraform-docs/tap/terraform-docs
4027
```
4128

42-
**NOTE:** please use the latest Go to do this, minimum `go1.16` or greater.
29+
Windows users can install using [Scoop]:
4330

44-
This will put `terraform-docs` in `$(go env GOPATH)/bin`. If you encounter the error
45-
`terraform-docs: command not found` after installation then you may need to either add
46-
that directory to your `$PATH` as shown [here] or do a manual installation by cloning
47-
the repo and run `make build` from the repository which will put `terraform-docs` in:
31+
```bash
32+
scoop bucket add terraform-docs https://github.com/terraform-docs/scoop-bucket
33+
scoop install terraform-docs
34+
```
35+
36+
or [Chocolatey]:
4837

4938
```bash
50-
$(go env GOPATH)/src/github.com/terraform-docs/terraform-docs/bin/$(uname | tr '[:upper:]' '[:lower:]')-amd64/terraform-docs
39+
choco install terraform-docs
5140
```
5241

5342
Stable binaries are also available on the [releases] page. To install, download the
@@ -57,45 +46,246 @@ binary for your platform from "Assets" and place this into your `$PATH`:
5746
curl -Lo ./terraform-docs.tar.gz https://github.com/terraform-docs/terraform-docs/releases/download/v0.15.0/terraform-docs-v0.15.0-$(uname)-amd64.tar.gz
5847
tar -xzf terraform-docs.tar.gz
5948
chmod +x terraform-docs
60-
mv terraform-docs /some-dir-in-your-PATH/terraform-docs
49+
mv terraform-docs /usr/local/terraform-docs
6150
```
6251

6352
**NOTE:** Windows releases are in `ZIP` format.
6453

65-
If you are a Mac OS X user, you can use [Homebrew]:
54+
The latest version can be installed using `go install` or `go get`:
6655

6756
```bash
68-
brew install terraform-docs
57+
# go1.17+
58+
go install github.com/terraform-docs/terraform-docs@v0.15.0
6959
```
7060

71-
or
61+
```bash
62+
# go1.16
63+
GO111MODULE="on" go get github.com/terraform-docs/terraform-docs@v0.15.0
64+
```
65+
66+
**NOTE:** please use the latest Go to do this, minimum `go1.16` is required.
67+
68+
This will put `terraform-docs` in `$(go env GOPATH)/bin`. If you encounter the error
69+
`terraform-docs: command not found` after installation then you may need to either add
70+
that directory to your `$PATH` as shown [here] or do a manual installation by cloning
71+
the repo and run `make build` from the repository which will put `terraform-docs` in:
7272

7373
```bash
74-
brew install terraform-docs/tap/terraform-docs
74+
$(go env GOPATH)/src/github.com/terraform-docs/terraform-docs/bin/$(uname | tr '[:upper:]' '[:lower:]')-amd64/terraform-docs
7575
```
7676

77-
Windows users can install using [Scoop]:
77+
## Usage
78+
79+
### Running the binary directly
80+
81+
To run and generate documentation into README within a directory:
7882

7983
```bash
80-
scoop bucket add terraform-docs https://github.com/terraform-docs/scoop-bucket
81-
scoop install terraform-docs
84+
terraform-docs markdown table --output-file README.md --output-mode inject /path/to/module
8285
```
8386

84-
or [Chocolatey]:
87+
Check [`output`] configuration for more details and examples.
88+
89+
### Using docker
90+
91+
terraform-docs can be run as a container by mounting a directory with `.tf`
92+
files in it and run the following command:
8593

8694
```bash
87-
choco install terraform-docs
95+
docker run --rm --volume "$(pwd):/terraform-docs" -u $(id -u) quay.io/terraform-docs/terraform-docs:0.15.0 markdown /terraform-docs
8896
```
8997

90-
Alternatively you also can run `terraform-docs` as a container:
98+
If `output.file` is not enabled for this module, generated output can be redirected
99+
back to a file:
91100

92101
```bash
93-
docker run quay.io/terraform-docs/terraform-docs:0.15.0
102+
docker run --rm --volume "$(pwd):/terraform-docs" -u $(id -u) quay.io/terraform-docs/terraform-docs:0.15.0 markdown /terraform-docs > doc.md
94103
```
95104

96105
**NOTE:** Docker tag `latest` refers to _latest_ stable released version and `edge`
97106
refers to HEAD of `master` at any given point in time.
98107

108+
### Using GitHub Actions
109+
110+
To use terraform-docs GitHub Action, configure a YAML workflow file (e.g.
111+
`.github/workflows/documentation.yml`) with the following:
112+
113+
```yaml
114+
name: Generate terraform docs
115+
on:
116+
- pull_request
117+
118+
jobs:
119+
docs:
120+
runs-on: ubuntu-latest
121+
steps:
122+
- uses: actions/checkout@v2
123+
with:
124+
ref: ${{ github.event.pull_request.head.ref }}
125+
126+
- name: Render terraform docs and push changes back to PR
127+
uses: terraform-docs/gh-actions@main
128+
with:
129+
working-dir: .
130+
output-file: README.md
131+
output-method: inject
132+
git-push: "true"
133+
```
134+
135+
Read more about [terraform-docs GitHub Action] and its configuration and
136+
examples.
137+
138+
### pre-commit hook
139+
140+
With pre-commit, you can ensure your Terraform module documentation is kept
141+
up-to-date each time you make a commit.
142+
143+
First [install pre-commit] and then create or update a `.pre-commit-config.yaml`
144+
in the root of your Git repo with at least the following content:
145+
146+
```yaml
147+
repos:
148+
- repo: https://github.com/terraform-docs/terraform-docs
149+
rev: "v0.15.0"
150+
hooks:
151+
- id: terraform-docs-go
152+
args: ["markdown", "table", "--output-file", "README.md", "./mymodule/path"]
153+
```
154+
155+
Then run:
156+
157+
```bash
158+
pre-commit install
159+
pre-commit install-hooks
160+
```
161+
162+
Further changes to your module's `.tf` files will cause an update to documentation
163+
when you make a commit.
164+
165+
## Configuration
166+
167+
terraform-docs can be configured with a yaml file. Default name of this file is
168+
`.terraform-docs.yml` and the order for lookig for it is:
169+
170+
1. root of module directory
171+
1. `.config/` folder at root of module directory
172+
1. current directory
173+
1. `.config/` folder at current directory
174+
1. `$HOME/.tfdocs.d/`
175+
176+
```yaml
177+
formatter: "" # this is required
178+
179+
version: ""
180+
181+
header-from: main.tf
182+
footer-from: ""
183+
184+
sections:
185+
hide: []
186+
show: []
187+
188+
content: ""
189+
190+
output:
191+
file: ""
192+
mode: inject
193+
template: |-
194+
<!-- BEGIN_TF_DOCS -->
195+
{{ .Content }}
196+
<!-- END_TF_DOCS -->
197+
198+
output-values:
199+
enabled: false
200+
from: ""
201+
202+
sort:
203+
enabled: true
204+
by: name
205+
206+
settings:
207+
anchor: true
208+
color: true
209+
default: true
210+
description: false
211+
escape: true
212+
hide-empty: false
213+
html: true
214+
indent: 2
215+
lockfile: true
216+
required: true
217+
sensitive: true
218+
type: true
219+
```
220+
221+
## Content Template
222+
223+
Generated content can be customized further away with `content` in configuration.
224+
If the `content` is empty the default order of sections is used.
225+
226+
Compatible formatters for customized content are `asciidoc` and `markdown`. `content`
227+
will be ignored for other formatters.
228+
229+
`content` is a Go template with following additional variables:
230+
231+
- `{{ .Header }}`
232+
- `{{ .Footer }}`
233+
- `{{ .Inputs }}`
234+
- `{{ .Modules }}`
235+
- `{{ .Outputs }}`
236+
- `{{ .Providers }}`
237+
- `{{ .Requirements }}`
238+
- `{{ .Resources }}`
239+
240+
and following functions:
241+
242+
- `{{ include "relative/path/to/file" }}`
243+
244+
These variables are the generated output of individual sections in the selected
245+
formatter. For example `{{ .Inputs }}` is Markdown Table representation of _inputs_
246+
when formatter is set to `markdown table`.
247+
248+
Note that sections visibility (i.e. `sections.show` and `sections.hide`) takes
249+
precedence over the `content`.
250+
251+
````yaml
252+
content: |-
253+
Any arbitrary text can be placed anywhere in the content
254+
255+
{{ .Header }}
256+
257+
and even in between sections
258+
259+
{{ .Providers }}
260+
261+
and they don't even need to be in the default order
262+
263+
{{ .Outputs }}
264+
265+
include any relative files
266+
267+
{{ include "relative/path/to/file" }}
268+
269+
{{ .Inputs }}
270+
271+
# Examples
272+
273+
```hcl
274+
{{ include "examples/foo/main.tf" }}
275+
```
276+
````
277+
278+
## Documentation
279+
280+
- **Users**
281+
- Read the [User Guide] to learn how to use terraform-docs
282+
- Read the [Formats Guide] to learn about different output formats of terraform-docs
283+
- Refer to [Config File Reference] for all the available configuration options
284+
- **Developers**
285+
- Read [Contributing Guide] before submitting a pull request
286+
287+
Visit [our website] for all documentation.
288+
99289
## Community
100290

101291
- Discuss terraform-docs on [Slack]
@@ -104,14 +294,17 @@ refers to HEAD of `master` at any given point in time.
104294

105295
MIT License - Copyright (c) 2021 The terraform-docs Authors.
106296

107-
[User Guide]: ./docs/user-guide/introduction.md
108-
[Formats Guide]: ./docs/reference/terraform-docs.md
109-
[Config File Reference]: ./docs/user-guide/configuration.md
297+
[Chocolatey]: https://www.chocolatey.org
298+
[Config File Reference]: https://terraform-docs.io/user-guide/configuration/
110299
[Contributing Guide]: CONTRIBUTING.md
111-
[our website]: https://terraform-docs.io/
300+
[Formats Guide]: https://terraform-docs.io/reference/terraform-docs/
112301
[here]: https://golang.org/doc/code.html#GOPATH
113-
[releases]: https://github.com/terraform-docs/terraform-docs/releases
114302
[Homebrew]: https://brew.sh
303+
[install pre-commit]: https://pre-commit.com/#install
304+
[`output`]: https://terraform-docs.io/user-guide/configuration/output/
305+
[releases]: https://github.com/terraform-docs/terraform-docs/releases
115306
[Scoop]: https://scoop.sh/
116-
[Chocolatey]: https://www.chocolatey.org
117307
[Slack]: https://slack.terraform-docs.io/
308+
[terraform-docs GitHub Action]: https://github.com/terraform-docs/gh-actions
309+
[our website]: https://terraform-docs.io/
310+
[User Guide]: https://terraform-docs.io/user-guide/introduction/

docs/developer-guide/plugins.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,5 +16,5 @@ If you want to create a new plugin, please refer to [tfdocs-format-template]. Th
1616
plugin can use [plugin-sdk] to communicate with the host process. You can create a
1717
new repository from `Use this template`.
1818

19-
[tfdocs-format-template]: https://github.com/terraform-docs/tfdocs-format-template
2019
[plugin-sdk]: https://github.com/terraform-docs/plugin-sdk
20+
[tfdocs-format-template]: https://github.com/terraform-docs/tfdocs-format-template

docs/how-to/pre-commit-hooks.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,5 +80,5 @@ This is very basic and higly simplified version of [pre-commit-terraform](https:
8080
Please refer to it for complete examples and guides.
8181
{{< /alert >}}
8282

83-
[`pre-commit`]: https://pre-commit.com/
8483
[git hooks]: https://git-scm.com/book/en/v2/Customizing-Git-Git-Hooks
84+
[`pre-commit`]: https://pre-commit.com/

docs/user-guide/configuration.md

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -42,13 +42,12 @@ $ tree
4242
$ terraform-docs -c .tfdocs-config.yml .
4343
```
4444

45-
As of `v0.13.0`, the order for looking for config file is *(2 and 4 were added
46-
in `v0.15.0`)*:
45+
As of `v0.13.0`, the order for looking for config file is:
4746

4847
1. root of module directory
49-
1. `.config/` folder at root of module directory
48+
1. `.config/` folder at root of module directory <sup class="no-top">(since v0.15.0)</sup>
5049
1. current directory
51-
1. `.config/` folder at current directory
50+
1. `.config/` folder at current directory <sup class="no-top">(since v0.15.0)</sup>
5251
1. `$HOME/.tfdocs.d/`
5352

5453
if `.terraform-docs.yml` is found in any of the folders above, that will take
@@ -66,9 +65,9 @@ Below is a complete list of options that can be used with `terraform-docs`, with
6665
default values.
6766

6867
```yaml
69-
version: ""
68+
formatter: "" # this is required
7069

71-
formatter: <FORMATTER_NAME>
70+
version: ""
7271

7372
header-from: main.tf
7473
footer-from: ""
@@ -114,7 +113,7 @@ settings:
114113
```
115114
116115
{{< alert type="info" >}}
117-
Only `formatter` is required, the rest of the options are optional.
116+
`formatter` is the only required option.
118117
{{< /alert >}}
119118

120119
## Usage

docs/user-guide/configuration/content.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ and following functions:
3535

3636
These variables are the generated output of individual sections in the selected
3737
formatter. For example `{{ .Inputs }}` is Markdown Table representation of _inputs_
38-
when formatter is set to `markdown table` and so on.
38+
when formatter is set to `markdown table`.
3939

4040
{{< alert type="info" >}}
4141
Sections visibility (i.e. `sections.show` and `sections.hide`) takes

0 commit comments

Comments
 (0)