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 \"\")")

0 commit comments

Comments
 (0)