Skip to content

Commit 845469c

Browse files
committed
Support outputing to file for absolute path
`--output-file` or corresponding `output.file` config can now support absolute path as well as relative path to root of module foler. For example all of the followings are valid: $ cd /path/to/module $ tree . . ├── docs │   └── README.md ├── ... └── main.tf # this works, relative path $ terraform-docs markdown table --output-file ../docs/README.md . # so does this, absolute path $ terraform-docs markdown table --output-file /path/to/module/docs/README.md . Signed-off-by: Khosrow Moossavi <khos2ow@gmail.com>
1 parent a9f9782 commit 845469c

20 files changed

+77
-18
lines changed

cmd/root.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ func NewCommand() *cobra.Command {
6161
cmd.PersistentFlags().StringSliceVar(&config.Sections.Show, "show", []string{}, "show section ["+cli.AllSections+"]")
6262
cmd.PersistentFlags().StringSliceVar(&config.Sections.Hide, "hide", []string{}, "hide section ["+cli.AllSections+"]")
6363

64-
cmd.PersistentFlags().StringVar(&config.Output.File, "output-file", "", "File in module directory to insert output into (default \"\")")
64+
cmd.PersistentFlags().StringVar(&config.Output.File, "output-file", "", "File path to insert output into (default \"\")")
6565
cmd.PersistentFlags().StringVar(&config.Output.Mode, "output-mode", "inject", "Output to file method ["+cli.OutputModes+"]")
6666
cmd.PersistentFlags().StringVar(&config.Output.Template, "output-template", cli.OutputTemplate, "Output template")
6767

docs/reference/asciidoc-document.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ terraform-docs asciidoc document [PATH] [flags]
3232
--header-from string relative path of a file to read header from (default "main.tf")
3333
--hide strings hide section [data-sources, footer, header, inputs, modules, outputs, providers, requirements, resources]
3434
--indent int indention level of AsciiDoc sections [1, 2, 3, 4, 5] (default 2)
35-
--output-file string File in module directory to insert output into (default "")
35+
--output-file string File path to insert output into (default "")
3636
--output-mode string Output to file method [inject, replace] (default "inject")
3737
--output-template string Output template (default "<!-- BEGIN_TF_DOCS -->\n{{ .Content }}\n<!-- END_TF_DOCS -->")
3838
--output-values inject output values into outputs (default false)

docs/reference/asciidoc-table.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ terraform-docs asciidoc table [PATH] [flags]
3232
--header-from string relative path of a file to read header from (default "main.tf")
3333
--hide strings hide section [data-sources, footer, header, inputs, modules, outputs, providers, requirements, resources]
3434
--indent int indention level of AsciiDoc sections [1, 2, 3, 4, 5] (default 2)
35-
--output-file string File in module directory to insert output into (default "")
35+
--output-file string File path to insert output into (default "")
3636
--output-mode string Output to file method [inject, replace] (default "inject")
3737
--output-template string Output template (default "<!-- BEGIN_TF_DOCS -->\n{{ .Content }}\n<!-- END_TF_DOCS -->")
3838
--output-values inject output values into outputs (default false)

docs/reference/asciidoc.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ terraform-docs asciidoc [PATH] [flags]
3535
--footer-from string relative path of a file to read footer from (default "")
3636
--header-from string relative path of a file to read header from (default "main.tf")
3737
--hide strings hide section [data-sources, footer, header, inputs, modules, outputs, providers, requirements, resources]
38-
--output-file string File in module directory to insert output into (default "")
38+
--output-file string File path to insert output into (default "")
3939
--output-mode string Output to file method [inject, replace] (default "inject")
4040
--output-template string Output template (default "<!-- BEGIN_TF_DOCS -->\n{{ .Content }}\n<!-- END_TF_DOCS -->")
4141
--output-values inject output values into outputs (default false)

docs/reference/json.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ terraform-docs json [PATH] [flags]
3030
--footer-from string relative path of a file to read footer from (default "")
3131
--header-from string relative path of a file to read header from (default "main.tf")
3232
--hide strings hide section [data-sources, footer, header, inputs, modules, outputs, providers, requirements, resources]
33-
--output-file string File in module directory to insert output into (default "")
33+
--output-file string File path to insert output into (default "")
3434
--output-mode string Output to file method [inject, replace] (default "inject")
3535
--output-template string Output template (default "<!-- BEGIN_TF_DOCS -->\n{{ .Content }}\n<!-- END_TF_DOCS -->")
3636
--output-values inject output values into outputs (default false)

docs/reference/markdown-document.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ terraform-docs markdown document [PATH] [flags]
3333
--header-from string relative path of a file to read header from (default "main.tf")
3434
--hide strings hide section [data-sources, footer, header, inputs, modules, outputs, providers, requirements, resources]
3535
--indent int indention level of Markdown sections [1, 2, 3, 4, 5] (default 2)
36-
--output-file string File in module directory to insert output into (default "")
36+
--output-file string File path to insert output into (default "")
3737
--output-mode string Output to file method [inject, replace] (default "inject")
3838
--output-template string Output template (default "<!-- BEGIN_TF_DOCS -->\n{{ .Content }}\n<!-- END_TF_DOCS -->")
3939
--output-values inject output values into outputs (default false)

docs/reference/markdown-table.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ terraform-docs markdown table [PATH] [flags]
3333
--header-from string relative path of a file to read header from (default "main.tf")
3434
--hide strings hide section [data-sources, footer, header, inputs, modules, outputs, providers, requirements, resources]
3535
--indent int indention level of Markdown sections [1, 2, 3, 4, 5] (default 2)
36-
--output-file string File in module directory to insert output into (default "")
36+
--output-file string File path to insert output into (default "")
3737
--output-mode string Output to file method [inject, replace] (default "inject")
3838
--output-template string Output template (default "<!-- BEGIN_TF_DOCS -->\n{{ .Content }}\n<!-- END_TF_DOCS -->")
3939
--output-values inject output values into outputs (default false)

docs/reference/markdown.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ terraform-docs markdown [PATH] [flags]
3636
--footer-from string relative path of a file to read footer from (default "")
3737
--header-from string relative path of a file to read header from (default "main.tf")
3838
--hide strings hide section [data-sources, footer, header, inputs, modules, outputs, providers, requirements, resources]
39-
--output-file string File in module directory to insert output into (default "")
39+
--output-file string File path to insert output into (default "")
4040
--output-mode string Output to file method [inject, replace] (default "inject")
4141
--output-template string Output template (default "<!-- BEGIN_TF_DOCS -->\n{{ .Content }}\n<!-- END_TF_DOCS -->")
4242
--output-values inject output values into outputs (default false)

docs/reference/pretty.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ terraform-docs pretty [PATH] [flags]
3030
--footer-from string relative path of a file to read footer from (default "")
3131
--header-from string relative path of a file to read header from (default "main.tf")
3232
--hide strings hide section [data-sources, footer, header, inputs, modules, outputs, providers, requirements, resources]
33-
--output-file string File in module directory to insert output into (default "")
33+
--output-file string File path to insert output into (default "")
3434
--output-mode string Output to file method [inject, replace] (default "inject")
3535
--output-template string Output template (default "<!-- BEGIN_TF_DOCS -->\n{{ .Content }}\n<!-- END_TF_DOCS -->")
3636
--output-values inject output values into outputs (default false)

docs/reference/terraform-docs.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ terraform-docs [PATH] [flags]
2424
--header-from string relative path of a file to read header from (default "main.tf")
2525
-h, --help help for terraform-docs
2626
--hide strings hide section [data-sources, footer, header, inputs, modules, outputs, providers, requirements, resources]
27-
--output-file string File in module directory to insert output into (default "")
27+
--output-file string File path to insert output into (default "")
2828
--output-mode string Output to file method [inject, replace] (default "inject")
2929
--output-template string Output template (default "<!-- BEGIN_TF_DOCS -->\n{{ .Content }}\n<!-- END_TF_DOCS -->")
3030
--output-values inject output values into outputs (default false)

docs/reference/tfvars-hcl.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ terraform-docs tfvars hcl [PATH] [flags]
3030
--footer-from string relative path of a file to read footer from (default "")
3131
--header-from string relative path of a file to read header from (default "main.tf")
3232
--hide strings hide section [data-sources, footer, header, inputs, modules, outputs, providers, requirements, resources]
33-
--output-file string File in module directory to insert output into (default "")
33+
--output-file string File path to insert output into (default "")
3434
--output-mode string Output to file method [inject, replace] (default "inject")
3535
--output-template string Output template (default "<!-- BEGIN_TF_DOCS -->\n{{ .Content }}\n<!-- END_TF_DOCS -->")
3636
--output-values inject output values into outputs (default false)

docs/reference/tfvars-json.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ terraform-docs tfvars json [PATH] [flags]
2929
--footer-from string relative path of a file to read footer from (default "")
3030
--header-from string relative path of a file to read header from (default "main.tf")
3131
--hide strings hide section [data-sources, footer, header, inputs, modules, outputs, providers, requirements, resources]
32-
--output-file string File in module directory to insert output into (default "")
32+
--output-file string File path to insert output into (default "")
3333
--output-mode string Output to file method [inject, replace] (default "inject")
3434
--output-template string Output template (default "<!-- BEGIN_TF_DOCS -->\n{{ .Content }}\n<!-- END_TF_DOCS -->")
3535
--output-values inject output values into outputs (default false)

docs/reference/tfvars.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ Generate terraform.tfvars of inputs.
2525
--footer-from string relative path of a file to read footer from (default "")
2626
--header-from string relative path of a file to read header from (default "main.tf")
2727
--hide strings hide section [data-sources, footer, header, inputs, modules, outputs, providers, requirements, resources]
28-
--output-file string File in module directory to insert output into (default "")
28+
--output-file string File path to insert output into (default "")
2929
--output-mode string Output to file method [inject, replace] (default "inject")
3030
--output-template string Output template (default "<!-- BEGIN_TF_DOCS -->\n{{ .Content }}\n<!-- END_TF_DOCS -->")
3131
--output-values inject output values into outputs (default false)

docs/reference/toml.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ terraform-docs toml [PATH] [flags]
2929
--footer-from string relative path of a file to read footer from (default "")
3030
--header-from string relative path of a file to read header from (default "main.tf")
3131
--hide strings hide section [data-sources, footer, header, inputs, modules, outputs, providers, requirements, resources]
32-
--output-file string File in module directory to insert output into (default "")
32+
--output-file string File path to insert output into (default "")
3333
--output-mode string Output to file method [inject, replace] (default "inject")
3434
--output-template string Output template (default "<!-- BEGIN_TF_DOCS -->\n{{ .Content }}\n<!-- END_TF_DOCS -->")
3535
--output-values inject output values into outputs (default false)

docs/reference/xml.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ terraform-docs xml [PATH] [flags]
2929
--footer-from string relative path of a file to read footer from (default "")
3030
--header-from string relative path of a file to read header from (default "main.tf")
3131
--hide strings hide section [data-sources, footer, header, inputs, modules, outputs, providers, requirements, resources]
32-
--output-file string File in module directory to insert output into (default "")
32+
--output-file string File path to insert output into (default "")
3333
--output-mode string Output to file method [inject, replace] (default "inject")
3434
--output-template string Output template (default "<!-- BEGIN_TF_DOCS -->\n{{ .Content }}\n<!-- END_TF_DOCS -->")
3535
--output-values inject output values into outputs (default false)

docs/reference/yaml.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ terraform-docs yaml [PATH] [flags]
2929
--footer-from string relative path of a file to read footer from (default "")
3030
--header-from string relative path of a file to read header from (default "main.tf")
3131
--hide strings hide section [data-sources, footer, header, inputs, modules, outputs, providers, requirements, resources]
32-
--output-file string File in module directory to insert output into (default "")
32+
--output-file string File path to insert output into (default "")
3333
--output-mode string Output to file method [inject, replace] (default "inject")
3434
--output-template string Output template (default "<!-- BEGIN_TF_DOCS -->\n{{ .Content }}\n<!-- END_TF_DOCS -->")
3535
--output-values inject output values into outputs (default false)

docs/user-guide/configuration.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,7 @@ inserted into a template, if provided, before getting saved into the file. This
199199
template can be customized with `output.template` or `--output-template string`
200200
CLI flag.
201201

202+
**Note:** `output.file` can be relative to module root or an absolute path.
202203
**Note:** `output.template` is optional for mode `replace`.
203204

204205
The default template value is:

docs/user-guide/how-to.md

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,10 +125,29 @@ Generated output can be insterted directly into the file. There are two modes of
125125
insersion: `inject` (default) or `replace`. Take a look at [output] configuration
126126
for all the details.
127127

128-
```console
128+
```bash
129129
terraform-docs markdown table --output-file README.md --output-mode inject /path/to/module
130130
```
131131

132+
Note that `--output-file` can be relative to module path or an absolute path in
133+
filesystem.
134+
135+
```bash
136+
$ cd /path/to/module
137+
$ tree .
138+
.
139+
├── docs
140+
│   └── README.md
141+
├── ...
142+
└── main.tf
143+
144+
# this works, relative path
145+
$ terraform-docs markdown table --output-file ../docs/README.md .
146+
147+
# so does this, absolute path
148+
$ terraform-docs markdown table --output-file /path/to/module/docs/README.md .
149+
```
150+
132151
## Generate terraform.tfvars
133152

134153
Since `v0.9.0`

internal/cli/writer.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ type fileWriter struct {
6161
}
6262

6363
func (fw *fileWriter) Write(p []byte) (int, error) {
64-
filename := filepath.Join(fw.dir, fw.file)
64+
filename := fw.fullFilePath()
6565

6666
var buf bytes.Buffer
6767

@@ -124,3 +124,10 @@ func (fw *fileWriter) write(filename string, p []byte) (int, error) {
124124
}
125125
return len(p), os.WriteFile(filename, p, 0644)
126126
}
127+
128+
func (fw *fileWriter) fullFilePath() string {
129+
if filepath.IsAbs(fw.file) {
130+
return fw.file
131+
}
132+
return filepath.Join(fw.dir, fw.file)
133+
}

internal/cli/writer_test.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,38 @@ import (
2121
"github.com/terraform-docs/terraform-docs/internal/testutil"
2222
)
2323

24+
func TestFileWriterFullPath(t *testing.T) {
25+
tests := map[string]struct {
26+
file string
27+
dir string
28+
expected string
29+
}{
30+
"Relative": {
31+
file: "file.md",
32+
dir: "/path/to/module",
33+
expected: "/path/to/module/file.md",
34+
},
35+
"Absolute": {
36+
file: "/path/to/module/file.md",
37+
dir: ".",
38+
expected: "/path/to/module/file.md",
39+
},
40+
}
41+
for name, tt := range tests {
42+
t.Run(name, func(t *testing.T) {
43+
assert := assert.New(t)
44+
45+
writer := &fileWriter{
46+
file: tt.file,
47+
dir: tt.dir,
48+
}
49+
50+
actual := writer.fullFilePath()
51+
assert.Equal(tt.expected, actual)
52+
})
53+
}
54+
}
55+
2456
func TestFileWriter(t *testing.T) {
2557
content := "Lorem ipsum dolor sit amet, consectetur adipiscing elit"
2658
tests := map[string]struct {

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy