Skip to content

Commit d2fe2b1

Browse files
committed
Move print package from internal to public
Signed-off-by: Khosrow Moossavi <khos2ow@gmail.com>
1 parent 90942f7 commit d2fe2b1

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

78 files changed

+1139
-946
lines changed

README.md

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -276,6 +276,48 @@ content: |-
276276
```
277277
````
278278

279+
## Build on top of terraform-docs
280+
281+
terraform-docs primary use-case is to be utilized as a standalone binary, but
282+
some parts of it is also available publicly and can be imported in your project
283+
as a library.
284+
285+
```go
286+
import (
287+
"github.com/terraform-docs/terraform-docs/format"
288+
"github.com/terraform-docs/terraform-docs/print"
289+
"github.com/terraform-docs/terraform-docs/terraform"
290+
)
291+
292+
// buildTerraformDocs for module root `path` and provided content `tmpl`.
293+
func buildTerraformDocs(path string, tmpl string) (string, error) {
294+
config := print.DefaultConfig()
295+
config.ModuleRoot = path // module root path (can be relative or absolute)
296+
297+
_, options := config.Extract()
298+
299+
module, err := terraform.LoadWithOptions(options)
300+
if err != nil {
301+
return "", err
302+
}
303+
304+
// Generate in Markdown Table format
305+
formatter := format.NewMarkdownTable(config)
306+
307+
if err := formatter.Generate(module); err != nil {
308+
return "", err
309+
}
310+
311+
// // Note: if you don't intend to provide additional template for the generated
312+
// // content, or the target format doesn't provide templating (e.g. json, yaml,
313+
// // xml, or toml) you can use `Content()` function instead of `ExecuteTemplate()`.
314+
// // `Content()` returns all the sections combined with predefined order.
315+
// return formatter.Content(), nil
316+
317+
return formatter.ExecuteTemplate(tmpl)
318+
}
319+
```
320+
279321
## Documentation
280322

281323
- **Users**

cmd/asciidoc/asciidoc.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,11 @@ import (
1616
"github.com/terraform-docs/terraform-docs/cmd/asciidoc/document"
1717
"github.com/terraform-docs/terraform-docs/cmd/asciidoc/table"
1818
"github.com/terraform-docs/terraform-docs/internal/cli"
19+
"github.com/terraform-docs/terraform-docs/print"
1920
)
2021

2122
// NewCommand returns a new cobra.Command for 'asciidoc' formatter
22-
func NewCommand(runtime *cli.Runtime, config *cli.Config) *cobra.Command {
23+
func NewCommand(runtime *cli.Runtime, config *print.Config) *cobra.Command {
2324
cmd := &cobra.Command{
2425
Args: cobra.ExactArgs(1),
2526
Use: "asciidoc [PATH]",

cmd/asciidoc/document/document.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,11 @@ import (
1414
"github.com/spf13/cobra"
1515

1616
"github.com/terraform-docs/terraform-docs/internal/cli"
17+
"github.com/terraform-docs/terraform-docs/print"
1718
)
1819

1920
// NewCommand returns a new cobra.Command for 'asciidoc document' formatter
20-
func NewCommand(runtime *cli.Runtime, config *cli.Config) *cobra.Command {
21+
func NewCommand(runtime *cli.Runtime, config *print.Config) *cobra.Command {
2122
cmd := &cobra.Command{
2223
Args: cobra.ExactArgs(1),
2324
Use: "document [PATH]",

cmd/asciidoc/table/table.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,11 @@ import (
1414
"github.com/spf13/cobra"
1515

1616
"github.com/terraform-docs/terraform-docs/internal/cli"
17+
"github.com/terraform-docs/terraform-docs/print"
1718
)
1819

1920
// NewCommand returns a new cobra.Command for 'asciidoc table' formatter
20-
func NewCommand(runtime *cli.Runtime, config *cli.Config) *cobra.Command {
21+
func NewCommand(runtime *cli.Runtime, config *print.Config) *cobra.Command {
2122
cmd := &cobra.Command{
2223
Args: cobra.ExactArgs(1),
2324
Use: "table [PATH]",

cmd/json/json.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,11 @@ import (
1414
"github.com/spf13/cobra"
1515

1616
"github.com/terraform-docs/terraform-docs/internal/cli"
17+
"github.com/terraform-docs/terraform-docs/print"
1718
)
1819

1920
// NewCommand returns a new cobra.Command for 'json' formatter
20-
func NewCommand(runtime *cli.Runtime, config *cli.Config) *cobra.Command {
21+
func NewCommand(runtime *cli.Runtime, config *print.Config) *cobra.Command {
2122
cmd := &cobra.Command{
2223
Args: cobra.ExactArgs(1),
2324
Use: "json [PATH]",

cmd/markdown/document/document.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,11 @@ import (
1414
"github.com/spf13/cobra"
1515

1616
"github.com/terraform-docs/terraform-docs/internal/cli"
17+
"github.com/terraform-docs/terraform-docs/print"
1718
)
1819

1920
// NewCommand returns a new cobra.Command for 'markdown document' formatter
20-
func NewCommand(runtime *cli.Runtime, config *cli.Config) *cobra.Command {
21+
func NewCommand(runtime *cli.Runtime, config *print.Config) *cobra.Command {
2122
cmd := &cobra.Command{
2223
Args: cobra.ExactArgs(1),
2324
Use: "document [PATH]",

cmd/markdown/markdown.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,11 @@ import (
1616
"github.com/terraform-docs/terraform-docs/cmd/markdown/document"
1717
"github.com/terraform-docs/terraform-docs/cmd/markdown/table"
1818
"github.com/terraform-docs/terraform-docs/internal/cli"
19+
"github.com/terraform-docs/terraform-docs/print"
1920
)
2021

2122
// NewCommand returns a new cobra.Command for 'markdown' formatter
22-
func NewCommand(runtime *cli.Runtime, config *cli.Config) *cobra.Command {
23+
func NewCommand(runtime *cli.Runtime, config *print.Config) *cobra.Command {
2324
cmd := &cobra.Command{
2425
Args: cobra.ExactArgs(1),
2526
Use: "markdown [PATH]",

cmd/markdown/table/table.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,11 @@ import (
1414
"github.com/spf13/cobra"
1515

1616
"github.com/terraform-docs/terraform-docs/internal/cli"
17+
"github.com/terraform-docs/terraform-docs/print"
1718
)
1819

1920
// NewCommand returns a new cobra.Command for 'markdown table' formatter
20-
func NewCommand(runtime *cli.Runtime, config *cli.Config) *cobra.Command {
21+
func NewCommand(runtime *cli.Runtime, config *print.Config) *cobra.Command {
2122
cmd := &cobra.Command{
2223
Args: cobra.ExactArgs(1),
2324
Use: "table [PATH]",

cmd/pretty/pretty.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,11 @@ import (
1414
"github.com/spf13/cobra"
1515

1616
"github.com/terraform-docs/terraform-docs/internal/cli"
17+
"github.com/terraform-docs/terraform-docs/print"
1718
)
1819

1920
// NewCommand returns a new cobra.Command for pretty formatter
20-
func NewCommand(runtime *cli.Runtime, config *cli.Config) *cobra.Command {
21+
func NewCommand(runtime *cli.Runtime, config *print.Config) *cobra.Command {
2122
cmd := &cobra.Command{
2223
Args: cobra.ExactArgs(1),
2324
Use: "pretty [PATH]",

cmd/root.go

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import (
2828
"github.com/terraform-docs/terraform-docs/cmd/yaml"
2929
"github.com/terraform-docs/terraform-docs/internal/cli"
3030
"github.com/terraform-docs/terraform-docs/internal/version"
31+
"github.com/terraform-docs/terraform-docs/print"
3132
)
3233

3334
// Execute adds all child commands to the root command and sets flags appropriately.
@@ -42,7 +43,7 @@ func Execute() error {
4243

4344
// NewCommand returns a new cobra.Command for 'root' command
4445
func NewCommand() *cobra.Command {
45-
config := cli.DefaultConfig()
46+
config := print.DefaultConfig()
4647
runtime := cli.NewRuntime(config)
4748
cmd := &cobra.Command{
4849
Args: cobra.MaximumNArgs(1),
@@ -62,16 +63,16 @@ func NewCommand() *cobra.Command {
6263
cmd.PersistentFlags().BoolVar(&config.Recursive, "recursive", false, "update submodules recursively (default false)")
6364
cmd.PersistentFlags().StringVar(&config.RecursivePath, "recursive-path", "modules", "submodules path to recursively update")
6465

65-
cmd.PersistentFlags().StringSliceVar(&config.Sections.Show, "show", []string{}, "show section ["+cli.AllSections+"]")
66-
cmd.PersistentFlags().StringSliceVar(&config.Sections.Hide, "hide", []string{}, "hide section ["+cli.AllSections+"]")
66+
cmd.PersistentFlags().StringSliceVar(&config.Sections.Show, "show", []string{}, "show section ["+print.AllSections+"]")
67+
cmd.PersistentFlags().StringSliceVar(&config.Sections.Hide, "hide", []string{}, "hide section ["+print.AllSections+"]")
6768

6869
cmd.PersistentFlags().StringVar(&config.Output.File, "output-file", "", "file path to insert output into (default \"\")")
69-
cmd.PersistentFlags().StringVar(&config.Output.Mode, "output-mode", "inject", "output to file method ["+cli.OutputModes+"]")
70-
cmd.PersistentFlags().StringVar(&config.Output.Template, "output-template", cli.OutputTemplate, "output template")
70+
cmd.PersistentFlags().StringVar(&config.Output.Mode, "output-mode", "inject", "output to file method ["+print.OutputModes+"]")
71+
cmd.PersistentFlags().StringVar(&config.Output.Template, "output-template", print.OutputTemplate, "output template")
7172
cmd.PersistentFlags().BoolVar(&config.Output.Check, "output-check", false, "check if content of output file is up to date (default false)")
7273

7374
cmd.PersistentFlags().BoolVar(&config.Sort.Enabled, "sort", true, "sort items")
74-
cmd.PersistentFlags().StringVar(&config.Sort.By, "sort-by", "name", "sort items by criteria ["+cli.SortTypes+"]")
75+
cmd.PersistentFlags().StringVar(&config.Sort.By, "sort-by", "name", "sort items by criteria ["+print.SortTypes+"]")
7576

7677
cmd.PersistentFlags().StringVar(&config.HeaderFrom, "header-from", "main.tf", "relative path of a file to read header from")
7778
cmd.PersistentFlags().StringVar(&config.FooterFrom, "footer-from", "", "relative path of a file to read footer from (default \"\")")

cmd/tfvars/hcl/hcl.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,11 @@ import (
1414
"github.com/spf13/cobra"
1515

1616
"github.com/terraform-docs/terraform-docs/internal/cli"
17+
"github.com/terraform-docs/terraform-docs/print"
1718
)
1819

1920
// NewCommand returns a new cobra.Command for 'tfvars hcl' formatter
20-
func NewCommand(runtime *cli.Runtime, config *cli.Config) *cobra.Command {
21+
func NewCommand(runtime *cli.Runtime, config *print.Config) *cobra.Command {
2122
cmd := &cobra.Command{
2223
Args: cobra.ExactArgs(1),
2324
Use: "hcl [PATH]",

cmd/tfvars/json/json.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,11 @@ import (
1414
"github.com/spf13/cobra"
1515

1616
"github.com/terraform-docs/terraform-docs/internal/cli"
17+
"github.com/terraform-docs/terraform-docs/print"
1718
)
1819

1920
// NewCommand returns a new cobra.Command for 'tfvars json' formatter
20-
func NewCommand(runtime *cli.Runtime, config *cli.Config) *cobra.Command {
21+
func NewCommand(runtime *cli.Runtime, config *print.Config) *cobra.Command {
2122
cmd := &cobra.Command{
2223
Args: cobra.ExactArgs(1),
2324
Use: "json [PATH]",

cmd/tfvars/tfvars.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,11 @@ import (
1616
"github.com/terraform-docs/terraform-docs/cmd/tfvars/hcl"
1717
"github.com/terraform-docs/terraform-docs/cmd/tfvars/json"
1818
"github.com/terraform-docs/terraform-docs/internal/cli"
19+
"github.com/terraform-docs/terraform-docs/print"
1920
)
2021

2122
// NewCommand returns a new cobra.Command for 'tfvars' formatter
22-
func NewCommand(runtime *cli.Runtime, config *cli.Config) *cobra.Command {
23+
func NewCommand(runtime *cli.Runtime, config *print.Config) *cobra.Command {
2324
cmd := &cobra.Command{
2425
Args: cobra.ExactArgs(1),
2526
Use: "tfvars [PATH]",

cmd/toml/toml.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,11 @@ import (
1414
"github.com/spf13/cobra"
1515

1616
"github.com/terraform-docs/terraform-docs/internal/cli"
17+
"github.com/terraform-docs/terraform-docs/print"
1718
)
1819

1920
// NewCommand returns a new cobra.Command for 'toml' formatter
20-
func NewCommand(runtime *cli.Runtime, config *cli.Config) *cobra.Command {
21+
func NewCommand(runtime *cli.Runtime, config *print.Config) *cobra.Command {
2122
cmd := &cobra.Command{
2223
Args: cobra.ExactArgs(1),
2324
Use: "toml [PATH]",

cmd/xml/xml.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,11 @@ import (
1414
"github.com/spf13/cobra"
1515

1616
"github.com/terraform-docs/terraform-docs/internal/cli"
17+
"github.com/terraform-docs/terraform-docs/print"
1718
)
1819

1920
// NewCommand returns a new cobra.Command for 'xml' formatter
20-
func NewCommand(runtime *cli.Runtime, config *cli.Config) *cobra.Command {
21+
func NewCommand(runtime *cli.Runtime, config *print.Config) *cobra.Command {
2122
cmd := &cobra.Command{
2223
Args: cobra.ExactArgs(1),
2324
Use: "xml [PATH]",

cmd/yaml/yaml.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,11 @@ import (
1414
"github.com/spf13/cobra"
1515

1616
"github.com/terraform-docs/terraform-docs/internal/cli"
17+
"github.com/terraform-docs/terraform-docs/print"
1718
)
1819

1920
// NewCommand returns a new cobra.Command for 'yaml' formatter
20-
func NewCommand(runtime *cli.Runtime, config *cli.Config) *cobra.Command {
21+
func NewCommand(runtime *cli.Runtime, config *print.Config) *cobra.Command {
2122
cmd := &cobra.Command{
2223
Args: cobra.ExactArgs(1),
2324
Use: "yaml [PATH]",

docs/reference/asciidoc-document.md

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -484,8 +484,4 @@ generates the following output:
484484

485485
Description: It's unquoted output.
486486

487-
## This is an example of a footer
488-
489-
It looks exactly like a header, but is placed at the end of the document
490-
491487
[examples]: https://github.com/terraform-docs/terraform-docs/tree/master/examples

docs/reference/asciidoc-table.md

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -421,8 +421,4 @@ generates the following output:
421421
|[[output_unquoted]] <<output_unquoted,unquoted>> |It's unquoted output.
422422
|===
423423

424-
## This is an example of a footer
425-
426-
It looks exactly like a header, but is placed at the end of the document
427-
428424
[examples]: https://github.com/terraform-docs/terraform-docs/tree/master/examples

docs/reference/json.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ generates the following output:
5757

5858
{
5959
"header": "Usage:\n\nExample of 'foo_bar' module in `foo_bar.tf`.\n\n- list item 1\n- list item 2\n\nEven inline **formatting** in _here_ is possible.\nand some [link](https://domain.com/)\n\n* list item 3\n* list item 4\n\n```hcl\nmodule \"foo_bar\" {\n source = \"github.com/foo/bar\"\n\n id = \"1234567890\"\n name = \"baz\"\n\n zones = [\"us-east-1\", \"us-west-1\"]\n\n tags = {\n Name = \"baz\"\n Created-By = \"first.last@email.com\"\n Date-Created = \"20180101\"\n }\n}\n```\n\nHere is some trailing text after code block,\nfollowed by another line of text.\n\n| Name | Description |\n|------|-----------------|\n| Foo | Foo description |\n| Bar | Bar description |",
60-
"footer": "## This is an example of a footer\n\nIt looks exactly like a header, but is placed at the end of the document",
60+
"footer": "",
6161
"inputs": [
6262
{
6363
"name": "bool-1",

docs/reference/markdown-document.md

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -486,8 +486,4 @@ generates the following output:
486486

487487
Description: It's unquoted output.
488488

489-
## This is an example of a footer
490-
491-
It looks exactly like a header, but is placed at the end of the document
492-
493489
[examples]: https://github.com/terraform-docs/terraform-docs/tree/master/examples

docs/reference/markdown-table.md

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -184,8 +184,4 @@ generates the following output:
184184
| <a name="output_output-2"></a> [output-2](#output\_output-2) | It's output number two. |
185185
| <a name="output_unquoted"></a> [unquoted](#output\_unquoted) | It's unquoted output. |
186186

187-
## This is an example of a footer
188-
189-
It looks exactly like a header, but is placed at the end of the document
190-
191187
[examples]: https://github.com/terraform-docs/terraform-docs/tree/master/examples

docs/reference/pretty.md

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -259,8 +259,4 @@ generates the following output:
259259
output.unquoted
260260
It's unquoted output.
261261

262-
## This is an example of a footer
263-
264-
It looks exactly like a header, but is placed at the end of the document
265-
266262
[examples]: https://github.com/terraform-docs/terraform-docs/tree/master/examples

docs/reference/toml.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ terraform-docs toml --footer-from footer.md ./examples/
5555
generates the following output:
5656

5757
header = "Usage:\n\nExample of 'foo_bar' module in `foo_bar.tf`.\n\n- list item 1\n- list item 2\n\nEven inline **formatting** in _here_ is possible.\nand some [link](https://domain.com/)\n\n* list item 3\n* list item 4\n\n```hcl\nmodule \"foo_bar\" {\n source = \"github.com/foo/bar\"\n\n id = \"1234567890\"\n name = \"baz\"\n\n zones = [\"us-east-1\", \"us-west-1\"]\n\n tags = {\n Name = \"baz\"\n Created-By = \"first.last@email.com\"\n Date-Created = \"20180101\"\n }\n}\n```\n\nHere is some trailing text after code block,\nfollowed by another line of text.\n\n| Name | Description |\n|------|-----------------|\n| Foo | Foo description |\n| Bar | Bar description |"
58-
footer = "## This is an example of a footer\n\nIt looks exactly like a header, but is placed at the end of the document"
58+
footer = ""
5959

6060
[[inputs]]
6161
name = "bool-1"

docs/reference/xml.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ generates the following output:
5656

5757
<module>
5858
<header>Usage:&#xA;&#xA;Example of &#39;foo_bar&#39; module in `foo_bar.tf`.&#xA;&#xA;- list item 1&#xA;- list item 2&#xA;&#xA;Even inline **formatting** in _here_ is possible.&#xA;and some [link](https://domain.com/)&#xA;&#xA;* list item 3&#xA;* list item 4&#xA;&#xA;```hcl&#xA;module &#34;foo_bar&#34; {&#xA; source = &#34;github.com/foo/bar&#34;&#xA;&#xA; id = &#34;1234567890&#34;&#xA; name = &#34;baz&#34;&#xA;&#xA; zones = [&#34;us-east-1&#34;, &#34;us-west-1&#34;]&#xA;&#xA; tags = {&#xA; Name = &#34;baz&#34;&#xA; Created-By = &#34;first.last@email.com&#34;&#xA; Date-Created = &#34;20180101&#34;&#xA; }&#xA;}&#xA;```&#xA;&#xA;Here is some trailing text after code block,&#xA;followed by another line of text.&#xA;&#xA;| Name | Description |&#xA;|------|-----------------|&#xA;| Foo | Foo description |&#xA;| Bar | Bar description |</header>
59-
<footer>## This is an example of a footer&#xA;&#xA;It looks exactly like a header, but is placed at the end of the document</footer>
59+
<footer></footer>
6060
<inputs>
6161
<input>
6262
<name>bool-1</name>

docs/reference/yaml.md

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -92,10 +92,7 @@ generates the following output:
9292
|------|-----------------|
9393
| Foo | Foo description |
9494
| Bar | Bar description |
95-
footer: |-
96-
## This is an example of a footer
97-
98-
It looks exactly like a header, but is placed at the end of the document
95+
footer: ""
9996
inputs:
10097
- name: bool-1
10198
type: bool

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