mirror of
https://github.com/terraform-docs/terraform-docs.git
synced 2026-03-27 04:48:33 +07:00
Move print package from internal to public
Signed-off-by: Khosrow Moossavi <khos2ow@gmail.com>
This commit is contained in:
@@ -14,7 +14,7 @@ import (
|
||||
"embed"
|
||||
gotemplate "text/template"
|
||||
|
||||
"github.com/terraform-docs/terraform-docs/internal/print"
|
||||
"github.com/terraform-docs/terraform-docs/print"
|
||||
"github.com/terraform-docs/terraform-docs/template"
|
||||
"github.com/terraform-docs/terraform-docs/terraform"
|
||||
)
|
||||
@@ -22,14 +22,18 @@ import (
|
||||
//go:embed templates/asciidoc_document*.tmpl
|
||||
var asciidocsDocumentFS embed.FS
|
||||
|
||||
// AsciidocDocument represents AsciiDoc Document format.
|
||||
type AsciidocDocument struct {
|
||||
// asciidocDocument represents AsciiDoc Document format.
|
||||
type asciidocDocument struct {
|
||||
*print.Generator
|
||||
|
||||
config *print.Config
|
||||
template *template.Template
|
||||
settings *print.Settings
|
||||
}
|
||||
|
||||
// NewAsciidocDocument returns new instance of AsciidocDocument.
|
||||
func NewAsciidocDocument(settings *print.Settings) print.Engine {
|
||||
// NewAsciidocDocument returns new instance of Asciidoc Document.
|
||||
func NewAsciidocDocument(config *print.Config) Type {
|
||||
settings, _ := config.Extract()
|
||||
items := readTemplateItems(asciidocsDocumentFS, "asciidoc_document")
|
||||
|
||||
settings.EscapeCharacters = false
|
||||
@@ -57,30 +61,26 @@ func NewAsciidocDocument(settings *print.Settings) print.Engine {
|
||||
return settings.ShowRequired
|
||||
},
|
||||
})
|
||||
return &AsciidocDocument{
|
||||
template: tt,
|
||||
settings: settings,
|
||||
|
||||
return &asciidocDocument{
|
||||
Generator: print.NewGenerator("json", config.ModuleRoot),
|
||||
config: config,
|
||||
template: tt,
|
||||
settings: settings,
|
||||
}
|
||||
}
|
||||
|
||||
// Generate a Terraform module as AsciiDoc document.
|
||||
func (d *AsciidocDocument) Generate(module *terraform.Module) (*print.Generator, error) {
|
||||
funcs := []print.GenerateFunc{}
|
||||
|
||||
err := print.ForEach(func(name string, fn print.GeneratorCallback) error {
|
||||
func (d *asciidocDocument) Generate(module *terraform.Module) error {
|
||||
err := d.Generator.ForEach(func(name string) (string, error) {
|
||||
rendered, err := d.template.Render(name, module)
|
||||
if err != nil {
|
||||
return err
|
||||
return "", err
|
||||
}
|
||||
|
||||
funcs = append(funcs, fn(sanitize(rendered)))
|
||||
return nil
|
||||
return sanitize(rendered), nil
|
||||
})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return print.NewGenerator("asciidoc document", funcs...), nil
|
||||
return err
|
||||
}
|
||||
|
||||
func init() {
|
||||
|
||||
@@ -15,8 +15,8 @@ import (
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
|
||||
"github.com/terraform-docs/terraform-docs/internal/print"
|
||||
"github.com/terraform-docs/terraform-docs/internal/testutil"
|
||||
"github.com/terraform-docs/terraform-docs/print"
|
||||
"github.com/terraform-docs/terraform-docs/terraform"
|
||||
)
|
||||
|
||||
@@ -184,12 +184,12 @@ func TestAsciidocDocument(t *testing.T) {
|
||||
module, err := testutil.GetModule(options)
|
||||
assert.Nil(err)
|
||||
|
||||
formatter := NewAsciidocDocument(&tt.settings)
|
||||
formatter := NewAsciidocDocument(tt.settings.ToConfig())
|
||||
|
||||
generator, err := formatter.Generate(module)
|
||||
err = formatter.Generate(module)
|
||||
assert.Nil(err)
|
||||
|
||||
actual, err := generator.ExecuteTemplate("")
|
||||
actual, err := formatter.ExecuteTemplate("")
|
||||
|
||||
assert.Nil(err)
|
||||
assert.Equal(expected, actual)
|
||||
|
||||
@@ -14,7 +14,7 @@ import (
|
||||
"embed"
|
||||
gotemplate "text/template"
|
||||
|
||||
"github.com/terraform-docs/terraform-docs/internal/print"
|
||||
"github.com/terraform-docs/terraform-docs/print"
|
||||
"github.com/terraform-docs/terraform-docs/template"
|
||||
"github.com/terraform-docs/terraform-docs/terraform"
|
||||
)
|
||||
@@ -22,14 +22,18 @@ import (
|
||||
//go:embed templates/asciidoc_table*.tmpl
|
||||
var asciidocTableFS embed.FS
|
||||
|
||||
// AsciidocTable represents AsciiDoc Table format.
|
||||
type AsciidocTable struct {
|
||||
// asciidocTable represents AsciiDoc Table format.
|
||||
type asciidocTable struct {
|
||||
*print.Generator
|
||||
|
||||
config *print.Config
|
||||
template *template.Template
|
||||
settings *print.Settings
|
||||
}
|
||||
|
||||
// NewAsciidocTable returns new instance of AsciidocTable.
|
||||
func NewAsciidocTable(settings *print.Settings) print.Engine {
|
||||
// NewAsciidocTable returns new instance of Asciidoc Table.
|
||||
func NewAsciidocTable(config *print.Config) Type {
|
||||
settings, _ := config.Extract()
|
||||
items := readTemplateItems(asciidocTableFS, "asciidoc_table")
|
||||
|
||||
settings.EscapeCharacters = false
|
||||
@@ -48,30 +52,26 @@ func NewAsciidocTable(settings *print.Settings) print.Engine {
|
||||
return result
|
||||
},
|
||||
})
|
||||
return &AsciidocTable{
|
||||
template: tt,
|
||||
settings: settings,
|
||||
|
||||
return &asciidocTable{
|
||||
Generator: print.NewGenerator("json", config.ModuleRoot),
|
||||
config: config,
|
||||
template: tt,
|
||||
settings: settings,
|
||||
}
|
||||
}
|
||||
|
||||
// Generate a Terraform module as AsciiDoc tables.
|
||||
func (t *AsciidocTable) Generate(module *terraform.Module) (*print.Generator, error) {
|
||||
funcs := []print.GenerateFunc{}
|
||||
|
||||
err := print.ForEach(func(name string, fn print.GeneratorCallback) error {
|
||||
func (t *asciidocTable) Generate(module *terraform.Module) error {
|
||||
err := t.Generator.ForEach(func(name string) (string, error) {
|
||||
rendered, err := t.template.Render(name, module)
|
||||
if err != nil {
|
||||
return err
|
||||
return "", err
|
||||
}
|
||||
|
||||
funcs = append(funcs, fn(sanitize(rendered)))
|
||||
return nil
|
||||
return sanitize(rendered), nil
|
||||
})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return print.NewGenerator("asciidoc table", funcs...), nil
|
||||
return err
|
||||
}
|
||||
|
||||
func init() {
|
||||
|
||||
@@ -15,8 +15,8 @@ import (
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
|
||||
"github.com/terraform-docs/terraform-docs/internal/print"
|
||||
"github.com/terraform-docs/terraform-docs/internal/testutil"
|
||||
"github.com/terraform-docs/terraform-docs/print"
|
||||
"github.com/terraform-docs/terraform-docs/terraform"
|
||||
)
|
||||
|
||||
@@ -184,12 +184,12 @@ func TestAsciidocTable(t *testing.T) {
|
||||
module, err := testutil.GetModule(options)
|
||||
assert.Nil(err)
|
||||
|
||||
formatter := NewAsciidocTable(&tt.settings)
|
||||
formatter := NewAsciidocTable(tt.settings.ToConfig())
|
||||
|
||||
generator, err := formatter.Generate(module)
|
||||
err = formatter.Generate(module)
|
||||
assert.Nil(err)
|
||||
|
||||
actual, err := generator.ExecuteTemplate("")
|
||||
actual, err := formatter.ExecuteTemplate("")
|
||||
|
||||
assert.Nil(err)
|
||||
assert.Equal(expected, actual)
|
||||
|
||||
@@ -11,7 +11,7 @@ the root directory of this source tree.
|
||||
package format
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
jsonsdk "encoding/json"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
@@ -73,7 +73,7 @@ func TestCommonSort(t *testing.T) {
|
||||
|
||||
var expected Expected
|
||||
|
||||
err = json.Unmarshal([]byte(golden), &expected)
|
||||
err = jsonsdk.Unmarshal([]byte(golden), &expected)
|
||||
assert.Nil(err)
|
||||
|
||||
for ii, i := range module.Inputs {
|
||||
|
||||
@@ -8,28 +8,39 @@ You may obtain a copy of the License at the LICENSE file in
|
||||
the root directory of this source tree.
|
||||
*/
|
||||
|
||||
// Package format provides different, out of the box supported, output formats.
|
||||
// Package format provides different, out of the box supported, output format types.
|
||||
//
|
||||
// Usage
|
||||
//
|
||||
// A specific format can be instantiated either for `format.Factory()` function or
|
||||
// A specific format can be instantiated either for `format.New()` function or
|
||||
// directly calling its function (e.g. `NewMarkdownTable`, etc)
|
||||
//
|
||||
// formatter, err := format.Factory("markdown table", settings)
|
||||
// config := print.DefaultConfig()
|
||||
// config.Formatter = "markdown table"
|
||||
//
|
||||
// formatter, err := format.New(config)
|
||||
// if err != nil {
|
||||
// return err
|
||||
// }
|
||||
//
|
||||
// generator, err := formatter.Generate(tfmodule)
|
||||
// err := formatter.Generate(tfmodule)
|
||||
// if err != nil {
|
||||
// return err
|
||||
// }
|
||||
//
|
||||
// output, err := generator.ExecuteTemplate("")
|
||||
//
|
||||
// output, err := formatter.ExecuteTemplate("")
|
||||
// if err != nil {
|
||||
// return err
|
||||
// }
|
||||
//
|
||||
// Note: if you don't intend to provide additional template for the generated
|
||||
// content, or the target format doesn't provide templating (e.g. json, yaml,
|
||||
// xml, or toml) you can use `Content()` function instead of `ExecuteTemplate()`.
|
||||
// `Content()` returns all the sections combined with predefined order.
|
||||
//
|
||||
// output := formatter.Content()
|
||||
//
|
||||
// Supported formats are:
|
||||
//
|
||||
// • `NewAsciidocDocument`
|
||||
|
||||
@@ -1,45 +0,0 @@
|
||||
/*
|
||||
Copyright 2021 The terraform-docs Authors.
|
||||
|
||||
Licensed under the MIT license (the "License"); you may not
|
||||
use this file except in compliance with the License.
|
||||
|
||||
You may obtain a copy of the License at the LICENSE file in
|
||||
the root directory of this source tree.
|
||||
*/
|
||||
|
||||
package format
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/terraform-docs/terraform-docs/internal/print"
|
||||
)
|
||||
|
||||
// initializerFn returns a concrete implementation of an Engine.
|
||||
type initializerFn func(*print.Settings) print.Engine
|
||||
|
||||
// initializers list of all registered engine initializer functions.
|
||||
var initializers = make(map[string]initializerFn)
|
||||
|
||||
// register a formatter engine initializer function.
|
||||
func register(e map[string]initializerFn) {
|
||||
if e == nil {
|
||||
return
|
||||
}
|
||||
for k, v := range e {
|
||||
initializers[k] = v
|
||||
}
|
||||
}
|
||||
|
||||
// Factory initializes and returns the concrete implementation of
|
||||
// format.Engine based on the provided 'name', for example for name
|
||||
// of 'json' it will return '*format.JSON' through 'format.NewJSON'
|
||||
// function.
|
||||
func Factory(name string, settings *print.Settings) (print.Engine, error) {
|
||||
fn, ok := initializers[name]
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("formatter '%s' not found", name)
|
||||
}
|
||||
return fn(settings), nil
|
||||
}
|
||||
@@ -1,211 +0,0 @@
|
||||
/*
|
||||
Copyright 2021 The terraform-docs Authors.
|
||||
|
||||
Licensed under the MIT license (the "License"); you may not
|
||||
use this file except in compliance with the License.
|
||||
|
||||
You may obtain a copy of the License at the LICENSE file in
|
||||
the root directory of this source tree.
|
||||
*/
|
||||
|
||||
package format
|
||||
|
||||
import (
|
||||
"reflect"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
|
||||
"github.com/terraform-docs/terraform-docs/internal/print"
|
||||
)
|
||||
|
||||
func TestFormatFactory(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
format string
|
||||
expected string
|
||||
wantErr bool
|
||||
}{
|
||||
{
|
||||
name: "format factory from name",
|
||||
format: "asciidoc",
|
||||
expected: "*format.AsciidocTable",
|
||||
wantErr: false,
|
||||
},
|
||||
{
|
||||
name: "format factory from name",
|
||||
format: "adoc",
|
||||
expected: "*format.AsciidocTable",
|
||||
wantErr: false,
|
||||
},
|
||||
{
|
||||
name: "format factory from name",
|
||||
format: "asciidoc document",
|
||||
expected: "*format.AsciidocDocument",
|
||||
wantErr: false,
|
||||
},
|
||||
{
|
||||
name: "format factory from name",
|
||||
format: "asciidoc doc",
|
||||
expected: "*format.AsciidocDocument",
|
||||
wantErr: false,
|
||||
},
|
||||
{
|
||||
name: "format factory from name",
|
||||
format: "adoc document",
|
||||
expected: "*format.AsciidocDocument",
|
||||
wantErr: false,
|
||||
},
|
||||
{
|
||||
name: "format factory from name",
|
||||
format: "adoc doc",
|
||||
expected: "*format.AsciidocDocument",
|
||||
wantErr: false,
|
||||
},
|
||||
{
|
||||
name: "format factory from name",
|
||||
format: "asciidoc table",
|
||||
expected: "*format.AsciidocTable",
|
||||
wantErr: false,
|
||||
},
|
||||
{
|
||||
name: "format factory from name",
|
||||
format: "asciidoc tbl",
|
||||
expected: "*format.AsciidocTable",
|
||||
wantErr: false,
|
||||
},
|
||||
{
|
||||
name: "format factory from name",
|
||||
format: "adoc table",
|
||||
expected: "*format.AsciidocTable",
|
||||
wantErr: false,
|
||||
},
|
||||
{
|
||||
name: "format factory from name",
|
||||
format: "adoc tbl",
|
||||
expected: "*format.AsciidocTable",
|
||||
wantErr: false,
|
||||
},
|
||||
{
|
||||
name: "format factory from name",
|
||||
format: "json",
|
||||
expected: "*format.JSON",
|
||||
wantErr: false,
|
||||
},
|
||||
{
|
||||
name: "format factory from name",
|
||||
format: "markdown",
|
||||
expected: "*format.MarkdownTable",
|
||||
wantErr: false,
|
||||
},
|
||||
{
|
||||
name: "format factory from name",
|
||||
format: "md",
|
||||
expected: "*format.MarkdownTable",
|
||||
wantErr: false,
|
||||
},
|
||||
{
|
||||
name: "format factory from name",
|
||||
format: "markdown document",
|
||||
expected: "*format.MarkdownDocument",
|
||||
wantErr: false,
|
||||
},
|
||||
{
|
||||
name: "format factory from name",
|
||||
format: "markdown doc",
|
||||
expected: "*format.MarkdownDocument",
|
||||
wantErr: false,
|
||||
},
|
||||
{
|
||||
name: "format factory from name",
|
||||
format: "md document",
|
||||
expected: "*format.MarkdownDocument",
|
||||
wantErr: false,
|
||||
},
|
||||
{
|
||||
name: "format factory from name",
|
||||
format: "md doc",
|
||||
expected: "*format.MarkdownDocument",
|
||||
wantErr: false,
|
||||
},
|
||||
{
|
||||
name: "format factory from name",
|
||||
format: "markdown table",
|
||||
expected: "*format.MarkdownTable",
|
||||
wantErr: false,
|
||||
},
|
||||
{
|
||||
name: "format factory from name",
|
||||
format: "markdown tbl",
|
||||
expected: "*format.MarkdownTable",
|
||||
wantErr: false,
|
||||
},
|
||||
{
|
||||
name: "format factory from name",
|
||||
format: "md table",
|
||||
expected: "*format.MarkdownTable",
|
||||
wantErr: false,
|
||||
},
|
||||
{
|
||||
name: "format factory from name",
|
||||
format: "md tbl",
|
||||
expected: "*format.MarkdownTable",
|
||||
wantErr: false,
|
||||
},
|
||||
{
|
||||
name: "format factory from name",
|
||||
format: "pretty",
|
||||
expected: "*format.Pretty",
|
||||
wantErr: false,
|
||||
},
|
||||
{
|
||||
name: "format factory from name",
|
||||
format: "tfvars hcl",
|
||||
expected: "*format.TfvarsHCL",
|
||||
wantErr: false,
|
||||
},
|
||||
{
|
||||
name: "format factory from name",
|
||||
format: "tfvars json",
|
||||
expected: "*format.TfvarsJSON",
|
||||
wantErr: false,
|
||||
},
|
||||
{
|
||||
name: "format factory from name",
|
||||
format: "toml",
|
||||
expected: "*format.TOML",
|
||||
wantErr: false,
|
||||
},
|
||||
{
|
||||
name: "format factory from name",
|
||||
format: "xml",
|
||||
expected: "*format.XML",
|
||||
wantErr: false,
|
||||
},
|
||||
{
|
||||
name: "format factory from name",
|
||||
format: "yaml",
|
||||
expected: "*format.YAML",
|
||||
wantErr: false,
|
||||
},
|
||||
{
|
||||
name: "format factory from name",
|
||||
format: "unknown",
|
||||
expected: "",
|
||||
wantErr: true,
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
assert := assert.New(t)
|
||||
settings := &print.Settings{}
|
||||
actual, err := Factory(tt.format, settings)
|
||||
if tt.wantErr {
|
||||
assert.NotNil(err)
|
||||
} else {
|
||||
assert.Nil(err)
|
||||
assert.Equal(tt.expected, reflect.TypeOf(actual).String())
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -12,55 +12,48 @@ package format
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
jsonsdk "encoding/json"
|
||||
"strings"
|
||||
|
||||
"github.com/terraform-docs/terraform-docs/internal/print"
|
||||
"github.com/terraform-docs/terraform-docs/print"
|
||||
"github.com/terraform-docs/terraform-docs/terraform"
|
||||
)
|
||||
|
||||
// JSON represents JSON format.
|
||||
type JSON struct {
|
||||
// json represents JSON format.
|
||||
type json struct {
|
||||
*print.Generator
|
||||
|
||||
config *print.Config
|
||||
settings *print.Settings
|
||||
}
|
||||
|
||||
// NewJSON returns new instance of JSON.
|
||||
func NewJSON(settings *print.Settings) print.Engine {
|
||||
return &JSON{
|
||||
settings: settings,
|
||||
func NewJSON(config *print.Config) Type {
|
||||
settings, _ := config.Extract()
|
||||
|
||||
return &json{
|
||||
Generator: print.NewGenerator("json", config.ModuleRoot),
|
||||
config: config,
|
||||
settings: settings,
|
||||
}
|
||||
}
|
||||
|
||||
// Generate a Terraform module as json.
|
||||
func (j *JSON) Generate(module *terraform.Module) (*print.Generator, error) {
|
||||
copy := &terraform.Module{
|
||||
Header: "",
|
||||
Footer: "",
|
||||
Inputs: make([]*terraform.Input, 0),
|
||||
ModuleCalls: make([]*terraform.ModuleCall, 0),
|
||||
Outputs: make([]*terraform.Output, 0),
|
||||
Providers: make([]*terraform.Provider, 0),
|
||||
Requirements: make([]*terraform.Requirement, 0),
|
||||
Resources: make([]*terraform.Resource, 0),
|
||||
}
|
||||
|
||||
print.CopySections(j.settings, module, copy)
|
||||
func (j *json) Generate(module *terraform.Module) error {
|
||||
copy := copySections(j.settings, module)
|
||||
|
||||
buffer := new(bytes.Buffer)
|
||||
|
||||
encoder := json.NewEncoder(buffer)
|
||||
encoder := jsonsdk.NewEncoder(buffer)
|
||||
encoder.SetIndent("", " ")
|
||||
encoder.SetEscapeHTML(j.settings.EscapeCharacters)
|
||||
|
||||
err := encoder.Encode(copy)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
if err := encoder.Encode(copy); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return print.NewGenerator(
|
||||
"json",
|
||||
print.WithContent(strings.TrimSuffix(buffer.String(), "\n")),
|
||||
), nil
|
||||
j.Generator.Funcs(print.WithContent(strings.TrimSuffix(buffer.String(), "\n")))
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func init() {
|
||||
|
||||
@@ -15,8 +15,8 @@ import (
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
|
||||
"github.com/terraform-docs/terraform-docs/internal/print"
|
||||
"github.com/terraform-docs/terraform-docs/internal/testutil"
|
||||
"github.com/terraform-docs/terraform-docs/print"
|
||||
"github.com/terraform-docs/terraform-docs/terraform"
|
||||
)
|
||||
|
||||
@@ -125,12 +125,12 @@ func TestJson(t *testing.T) {
|
||||
module, err := testutil.GetModule(options)
|
||||
assert.Nil(err)
|
||||
|
||||
formatter := NewJSON(&tt.settings)
|
||||
formatter := NewJSON(tt.settings.ToConfig())
|
||||
|
||||
generator, err := formatter.Generate(module)
|
||||
err = formatter.Generate(module)
|
||||
assert.Nil(err)
|
||||
|
||||
actual, err := generator.ExecuteTemplate("")
|
||||
actual, err := formatter.ExecuteTemplate("")
|
||||
|
||||
assert.Nil(err)
|
||||
assert.Equal(expected, actual)
|
||||
|
||||
@@ -14,7 +14,7 @@ import (
|
||||
"embed"
|
||||
gotemplate "text/template"
|
||||
|
||||
"github.com/terraform-docs/terraform-docs/internal/print"
|
||||
"github.com/terraform-docs/terraform-docs/print"
|
||||
"github.com/terraform-docs/terraform-docs/template"
|
||||
"github.com/terraform-docs/terraform-docs/terraform"
|
||||
)
|
||||
@@ -22,14 +22,18 @@ import (
|
||||
//go:embed templates/markdown_document*.tmpl
|
||||
var markdownDocumentFS embed.FS
|
||||
|
||||
// MarkdownDocument represents Markdown Document format.
|
||||
type MarkdownDocument struct {
|
||||
// markdownDocument represents Markdown Document format.
|
||||
type markdownDocument struct {
|
||||
*print.Generator
|
||||
|
||||
config *print.Config
|
||||
template *template.Template
|
||||
settings *print.Settings
|
||||
}
|
||||
|
||||
// NewMarkdownDocument returns new instance of Document.
|
||||
func NewMarkdownDocument(settings *print.Settings) print.Engine {
|
||||
// NewMarkdownDocument returns new instance of Markdown Document.
|
||||
func NewMarkdownDocument(config *print.Config) Type {
|
||||
settings, _ := config.Extract()
|
||||
items := readTemplateItems(markdownDocumentFS, "markdown_document")
|
||||
|
||||
tt := template.New(settings, items...)
|
||||
@@ -55,30 +59,26 @@ func NewMarkdownDocument(settings *print.Settings) print.Engine {
|
||||
return settings.ShowRequired
|
||||
},
|
||||
})
|
||||
return &MarkdownDocument{
|
||||
template: tt,
|
||||
settings: settings,
|
||||
|
||||
return &markdownDocument{
|
||||
Generator: print.NewGenerator("json", config.ModuleRoot),
|
||||
config: config,
|
||||
template: tt,
|
||||
settings: settings,
|
||||
}
|
||||
}
|
||||
|
||||
// Generate a Terraform module as Markdown document.
|
||||
func (d *MarkdownDocument) Generate(module *terraform.Module) (*print.Generator, error) {
|
||||
funcs := []print.GenerateFunc{}
|
||||
|
||||
err := print.ForEach(func(name string, fn print.GeneratorCallback) error {
|
||||
func (d *markdownDocument) Generate(module *terraform.Module) error {
|
||||
err := d.Generator.ForEach(func(name string) (string, error) {
|
||||
rendered, err := d.template.Render(name, module)
|
||||
if err != nil {
|
||||
return err
|
||||
return "", err
|
||||
}
|
||||
|
||||
funcs = append(funcs, fn(sanitize(rendered)))
|
||||
return nil
|
||||
return sanitize(rendered), nil
|
||||
})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return print.NewGenerator("markdown document", funcs...), nil
|
||||
return err
|
||||
}
|
||||
|
||||
func init() {
|
||||
|
||||
@@ -15,8 +15,8 @@ import (
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
|
||||
"github.com/terraform-docs/terraform-docs/internal/print"
|
||||
"github.com/terraform-docs/terraform-docs/internal/testutil"
|
||||
"github.com/terraform-docs/terraform-docs/print"
|
||||
"github.com/terraform-docs/terraform-docs/terraform"
|
||||
)
|
||||
|
||||
@@ -232,12 +232,12 @@ func TestMarkdownDocument(t *testing.T) {
|
||||
module, err := testutil.GetModule(options)
|
||||
assert.Nil(err)
|
||||
|
||||
formatter := NewMarkdownDocument(&tt.settings)
|
||||
formatter := NewMarkdownDocument(tt.settings.ToConfig())
|
||||
|
||||
generator, err := formatter.Generate(module)
|
||||
err = formatter.Generate(module)
|
||||
assert.Nil(err)
|
||||
|
||||
actual, err := generator.ExecuteTemplate("")
|
||||
actual, err := formatter.ExecuteTemplate("")
|
||||
|
||||
assert.Nil(err)
|
||||
assert.Equal(expected, actual)
|
||||
|
||||
@@ -14,7 +14,7 @@ import (
|
||||
"embed"
|
||||
gotemplate "text/template"
|
||||
|
||||
"github.com/terraform-docs/terraform-docs/internal/print"
|
||||
"github.com/terraform-docs/terraform-docs/print"
|
||||
"github.com/terraform-docs/terraform-docs/template"
|
||||
"github.com/terraform-docs/terraform-docs/terraform"
|
||||
)
|
||||
@@ -22,14 +22,18 @@ import (
|
||||
//go:embed templates/markdown_table*.tmpl
|
||||
var markdownTableFS embed.FS
|
||||
|
||||
// MarkdownTable represents Markdown Table format.
|
||||
type MarkdownTable struct {
|
||||
// markdownTable represents Markdown Table format.
|
||||
type markdownTable struct {
|
||||
*print.Generator
|
||||
|
||||
config *print.Config
|
||||
template *template.Template
|
||||
settings *print.Settings
|
||||
}
|
||||
|
||||
// NewMarkdownTable returns new instance of Table.
|
||||
func NewMarkdownTable(settings *print.Settings) print.Engine {
|
||||
// NewMarkdownTable returns new instance of Markdown Table.
|
||||
func NewMarkdownTable(config *print.Config) Type {
|
||||
settings, _ := config.Extract()
|
||||
items := readTemplateItems(markdownTableFS, "markdown_table")
|
||||
|
||||
tt := template.New(settings, items...)
|
||||
@@ -46,30 +50,26 @@ func NewMarkdownTable(settings *print.Settings) print.Engine {
|
||||
return result
|
||||
},
|
||||
})
|
||||
return &MarkdownTable{
|
||||
template: tt,
|
||||
settings: settings,
|
||||
|
||||
return &markdownTable{
|
||||
Generator: print.NewGenerator("markdown table", config.ModuleRoot),
|
||||
config: config,
|
||||
template: tt,
|
||||
settings: settings,
|
||||
}
|
||||
}
|
||||
|
||||
// Generate a Terraform module as Markdown tables.
|
||||
func (t *MarkdownTable) Generate(module *terraform.Module) (*print.Generator, error) {
|
||||
funcs := []print.GenerateFunc{}
|
||||
|
||||
err := print.ForEach(func(name string, fn print.GeneratorCallback) error {
|
||||
func (t *markdownTable) Generate(module *terraform.Module) error {
|
||||
err := t.Generator.ForEach(func(name string) (string, error) {
|
||||
rendered, err := t.template.Render(name, module)
|
||||
if err != nil {
|
||||
return err
|
||||
return "", err
|
||||
}
|
||||
|
||||
funcs = append(funcs, fn(sanitize(rendered)))
|
||||
return nil
|
||||
return sanitize(rendered), nil
|
||||
})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return print.NewGenerator("markdown table", funcs...), nil
|
||||
return err
|
||||
}
|
||||
|
||||
func init() {
|
||||
|
||||
@@ -15,8 +15,8 @@ import (
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
|
||||
"github.com/terraform-docs/terraform-docs/internal/print"
|
||||
"github.com/terraform-docs/terraform-docs/internal/testutil"
|
||||
"github.com/terraform-docs/terraform-docs/print"
|
||||
"github.com/terraform-docs/terraform-docs/terraform"
|
||||
)
|
||||
|
||||
@@ -232,12 +232,12 @@ func TestMarkdownTable(t *testing.T) {
|
||||
module, err := testutil.GetModule(options)
|
||||
assert.Nil(err)
|
||||
|
||||
formatter := NewMarkdownTable(&tt.settings)
|
||||
formatter := NewMarkdownTable(tt.settings.ToConfig())
|
||||
|
||||
generator, err := formatter.Generate(module)
|
||||
err = formatter.Generate(module)
|
||||
assert.Nil(err)
|
||||
|
||||
actual, err := generator.ExecuteTemplate("")
|
||||
actual, err := formatter.ExecuteTemplate("")
|
||||
|
||||
assert.Nil(err)
|
||||
assert.Equal(expected, actual)
|
||||
|
||||
@@ -16,7 +16,7 @@ import (
|
||||
"regexp"
|
||||
gotemplate "text/template"
|
||||
|
||||
"github.com/terraform-docs/terraform-docs/internal/print"
|
||||
"github.com/terraform-docs/terraform-docs/print"
|
||||
"github.com/terraform-docs/terraform-docs/template"
|
||||
"github.com/terraform-docs/terraform-docs/terraform"
|
||||
)
|
||||
@@ -24,14 +24,19 @@ import (
|
||||
//go:embed templates/pretty.tmpl
|
||||
var prettyTpl []byte
|
||||
|
||||
// Pretty represents colorized pretty format.
|
||||
type Pretty struct {
|
||||
// pretty represents colorized pretty format.
|
||||
type pretty struct {
|
||||
*print.Generator
|
||||
|
||||
config *print.Config
|
||||
template *template.Template
|
||||
settings *print.Settings
|
||||
}
|
||||
|
||||
// NewPretty returns new instance of Pretty.
|
||||
func NewPretty(settings *print.Settings) print.Engine {
|
||||
func NewPretty(config *print.Config) Type {
|
||||
settings, _ := config.Extract()
|
||||
|
||||
tt := template.New(settings, &template.Item{
|
||||
Name: "pretty",
|
||||
Text: string(prettyTpl),
|
||||
@@ -46,23 +51,25 @@ func NewPretty(settings *print.Settings) print.Engine {
|
||||
return fmt.Sprintf("%s%s%s", c, s, r)
|
||||
},
|
||||
})
|
||||
return &Pretty{
|
||||
template: tt,
|
||||
settings: settings,
|
||||
|
||||
return &pretty{
|
||||
Generator: print.NewGenerator("pretty", config.ModuleRoot),
|
||||
config: config,
|
||||
template: tt,
|
||||
settings: settings,
|
||||
}
|
||||
}
|
||||
|
||||
// Generate a Terraform module document.
|
||||
func (p *Pretty) Generate(module *terraform.Module) (*print.Generator, error) {
|
||||
func (p *pretty) Generate(module *terraform.Module) error {
|
||||
rendered, err := p.template.Render("pretty", module)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
return err
|
||||
}
|
||||
|
||||
return print.NewGenerator(
|
||||
"pretty",
|
||||
print.WithContent(regexp.MustCompile(`(\r?\n)*$`).ReplaceAllString(rendered, "")),
|
||||
), nil
|
||||
p.Generator.Funcs(print.WithContent(regexp.MustCompile(`(\r?\n)*$`).ReplaceAllString(rendered, "")))
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func init() {
|
||||
|
||||
@@ -15,8 +15,8 @@ import (
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
|
||||
"github.com/terraform-docs/terraform-docs/internal/print"
|
||||
"github.com/terraform-docs/terraform-docs/internal/testutil"
|
||||
"github.com/terraform-docs/terraform-docs/print"
|
||||
"github.com/terraform-docs/terraform-docs/terraform"
|
||||
)
|
||||
|
||||
@@ -125,12 +125,12 @@ func TestPretty(t *testing.T) {
|
||||
module, err := testutil.GetModule(options)
|
||||
assert.Nil(err)
|
||||
|
||||
formatter := NewPretty(&tt.settings)
|
||||
formatter := NewPretty(tt.settings.ToConfig())
|
||||
|
||||
generator, err := formatter.Generate(module)
|
||||
err = formatter.Generate(module)
|
||||
assert.Nil(err)
|
||||
|
||||
actual, err := generator.ExecuteTemplate("")
|
||||
actual, err := formatter.ExecuteTemplate("")
|
||||
|
||||
assert.Nil(err)
|
||||
assert.Equal(expected, actual)
|
||||
|
||||
@@ -16,8 +16,8 @@ import (
|
||||
"strings"
|
||||
gotemplate "text/template"
|
||||
|
||||
"github.com/terraform-docs/terraform-docs/internal/print"
|
||||
"github.com/terraform-docs/terraform-docs/internal/types"
|
||||
"github.com/terraform-docs/terraform-docs/print"
|
||||
"github.com/terraform-docs/terraform-docs/template"
|
||||
"github.com/terraform-docs/terraform-docs/terraform"
|
||||
)
|
||||
@@ -25,8 +25,11 @@ import (
|
||||
//go:embed templates/tfvars_hcl.tmpl
|
||||
var tfvarsHCLTpl []byte
|
||||
|
||||
// TfvarsHCL represents Terraform tfvars HCL format.
|
||||
type TfvarsHCL struct {
|
||||
// tfvarsHCL represents Terraform tfvars HCL format.
|
||||
type tfvarsHCL struct {
|
||||
*print.Generator
|
||||
|
||||
config *print.Config
|
||||
template *template.Template
|
||||
settings *print.Settings
|
||||
}
|
||||
@@ -34,7 +37,9 @@ type TfvarsHCL struct {
|
||||
var padding []int
|
||||
|
||||
// NewTfvarsHCL returns new instance of TfvarsHCL.
|
||||
func NewTfvarsHCL(settings *print.Settings) print.Engine {
|
||||
func NewTfvarsHCL(config *print.Config) Type {
|
||||
settings, _ := config.Extract()
|
||||
|
||||
tt := template.New(settings, &template.Item{
|
||||
Name: "tfvars",
|
||||
Text: string(tfvarsHCLTpl),
|
||||
@@ -56,25 +61,27 @@ func NewTfvarsHCL(settings *print.Settings) print.Engine {
|
||||
return settings.ShowDescription
|
||||
},
|
||||
})
|
||||
return &TfvarsHCL{
|
||||
template: tt,
|
||||
settings: settings,
|
||||
|
||||
return &tfvarsHCL{
|
||||
Generator: print.NewGenerator("tfvars hcl", config.ModuleRoot),
|
||||
config: config,
|
||||
template: tt,
|
||||
settings: settings,
|
||||
}
|
||||
}
|
||||
|
||||
// Generate a Terraform module as Terraform tfvars HCL.
|
||||
func (h *TfvarsHCL) Generate(module *terraform.Module) (*print.Generator, error) {
|
||||
func (h *tfvarsHCL) Generate(module *terraform.Module) error {
|
||||
alignments(module.Inputs, h.settings)
|
||||
|
||||
rendered, err := h.template.Render("tfvars", module)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
return err
|
||||
}
|
||||
|
||||
return print.NewGenerator(
|
||||
"tfvars hcl",
|
||||
print.WithContent(strings.TrimSuffix(sanitize(rendered), "\n")),
|
||||
), nil
|
||||
h.Generator.Funcs(print.WithContent(strings.TrimSuffix(sanitize(rendered), "\n")))
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func isMultilineFormat(input *terraform.Input) bool {
|
||||
|
||||
@@ -15,8 +15,8 @@ import (
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
|
||||
"github.com/terraform-docs/terraform-docs/internal/print"
|
||||
"github.com/terraform-docs/terraform-docs/internal/testutil"
|
||||
"github.com/terraform-docs/terraform-docs/print"
|
||||
"github.com/terraform-docs/terraform-docs/terraform"
|
||||
)
|
||||
|
||||
@@ -104,12 +104,12 @@ func TestTfvarsHcl(t *testing.T) {
|
||||
module, err := testutil.GetModule(options)
|
||||
assert.Nil(err)
|
||||
|
||||
formatter := NewTfvarsHCL(&tt.settings)
|
||||
formatter := NewTfvarsHCL(tt.settings.ToConfig())
|
||||
|
||||
generator, err := formatter.Generate(module)
|
||||
err = formatter.Generate(module)
|
||||
assert.Nil(err)
|
||||
|
||||
actual, err := generator.ExecuteTemplate("")
|
||||
actual, err := formatter.ExecuteTemplate("")
|
||||
|
||||
assert.Nil(err)
|
||||
assert.Equal(expected, actual)
|
||||
|
||||
@@ -12,29 +12,36 @@ package format
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
jsonsdk "encoding/json"
|
||||
"strings"
|
||||
|
||||
"github.com/iancoleman/orderedmap"
|
||||
|
||||
"github.com/terraform-docs/terraform-docs/internal/print"
|
||||
"github.com/terraform-docs/terraform-docs/print"
|
||||
"github.com/terraform-docs/terraform-docs/terraform"
|
||||
)
|
||||
|
||||
// TfvarsJSON represents Terraform tfvars JSON format.
|
||||
type TfvarsJSON struct {
|
||||
// tfvarsJSON represents Terraform tfvars JSON format.
|
||||
type tfvarsJSON struct {
|
||||
*print.Generator
|
||||
|
||||
config *print.Config
|
||||
settings *print.Settings
|
||||
}
|
||||
|
||||
// NewTfvarsJSON returns new instance of TfvarsJSON.
|
||||
func NewTfvarsJSON(settings *print.Settings) print.Engine {
|
||||
return &TfvarsJSON{
|
||||
settings: settings,
|
||||
func NewTfvarsJSON(config *print.Config) Type {
|
||||
settings, _ := config.Extract()
|
||||
|
||||
return &tfvarsJSON{
|
||||
Generator: print.NewGenerator("tfvars json", config.ModuleRoot),
|
||||
config: config,
|
||||
settings: settings,
|
||||
}
|
||||
}
|
||||
|
||||
// Generate a Terraform module as Terraform tfvars JSON.
|
||||
func (j *TfvarsJSON) Generate(module *terraform.Module) (*print.Generator, error) {
|
||||
func (j *tfvarsJSON) Generate(module *terraform.Module) error {
|
||||
copy := orderedmap.New()
|
||||
copy.SetEscapeHTML(false)
|
||||
for _, i := range module.Inputs {
|
||||
@@ -42,20 +49,17 @@ func (j *TfvarsJSON) Generate(module *terraform.Module) (*print.Generator, error
|
||||
}
|
||||
|
||||
buffer := new(bytes.Buffer)
|
||||
|
||||
encoder := json.NewEncoder(buffer)
|
||||
encoder := jsonsdk.NewEncoder(buffer)
|
||||
encoder.SetIndent("", " ")
|
||||
encoder.SetEscapeHTML(false)
|
||||
|
||||
err := encoder.Encode(copy)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
if err := encoder.Encode(copy); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return print.NewGenerator(
|
||||
"tfvars json",
|
||||
print.WithContent(strings.TrimSuffix(buffer.String(), "\n")),
|
||||
), nil
|
||||
j.Generator.Funcs(print.WithContent(strings.TrimSuffix(buffer.String(), "\n")))
|
||||
|
||||
return nil
|
||||
|
||||
}
|
||||
|
||||
|
||||
@@ -13,8 +13,8 @@ import (
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
|
||||
"github.com/terraform-docs/terraform-docs/internal/print"
|
||||
"github.com/terraform-docs/terraform-docs/internal/testutil"
|
||||
"github.com/terraform-docs/terraform-docs/print"
|
||||
"github.com/terraform-docs/terraform-docs/terraform"
|
||||
)
|
||||
|
||||
@@ -89,12 +89,12 @@ func TestTfvarsJson(t *testing.T) {
|
||||
module, err := testutil.GetModule(options)
|
||||
assert.Nil(err)
|
||||
|
||||
formatter := NewTfvarsJSON(&tt.settings)
|
||||
formatter := NewTfvarsJSON(tt.settings.ToConfig())
|
||||
|
||||
generator, err := formatter.Generate(module)
|
||||
err = formatter.Generate(module)
|
||||
assert.Nil(err)
|
||||
|
||||
actual, err := generator.ExecuteTemplate("")
|
||||
actual, err := formatter.ExecuteTemplate("")
|
||||
|
||||
assert.Nil(err)
|
||||
assert.Equal(expected, actual)
|
||||
|
||||
@@ -14,50 +14,45 @@ import (
|
||||
"bytes"
|
||||
"strings"
|
||||
|
||||
"github.com/BurntSushi/toml"
|
||||
tomlsdk "github.com/BurntSushi/toml"
|
||||
|
||||
"github.com/terraform-docs/terraform-docs/internal/print"
|
||||
"github.com/terraform-docs/terraform-docs/print"
|
||||
"github.com/terraform-docs/terraform-docs/terraform"
|
||||
)
|
||||
|
||||
// TOML represents TOML format.
|
||||
type TOML struct {
|
||||
// toml represents TOML format.
|
||||
type toml struct {
|
||||
*print.Generator
|
||||
|
||||
config *print.Config
|
||||
settings *print.Settings
|
||||
}
|
||||
|
||||
// NewTOML returns new instance of TOML.
|
||||
func NewTOML(settings *print.Settings) print.Engine {
|
||||
return &TOML{
|
||||
settings: settings,
|
||||
func NewTOML(config *print.Config) Type {
|
||||
settings, _ := config.Extract()
|
||||
|
||||
return &toml{
|
||||
Generator: print.NewGenerator("toml", config.ModuleRoot),
|
||||
config: config,
|
||||
settings: settings,
|
||||
}
|
||||
}
|
||||
|
||||
// Generate a Terraform module as toml.
|
||||
func (t *TOML) Generate(module *terraform.Module) (*print.Generator, error) {
|
||||
copy := &terraform.Module{
|
||||
Header: "",
|
||||
Footer: "",
|
||||
Inputs: make([]*terraform.Input, 0),
|
||||
ModuleCalls: make([]*terraform.ModuleCall, 0),
|
||||
Outputs: make([]*terraform.Output, 0),
|
||||
Providers: make([]*terraform.Provider, 0),
|
||||
Requirements: make([]*terraform.Requirement, 0),
|
||||
Resources: make([]*terraform.Resource, 0),
|
||||
}
|
||||
|
||||
print.CopySections(t.settings, module, copy)
|
||||
func (t *toml) Generate(module *terraform.Module) error {
|
||||
copy := copySections(t.settings, module)
|
||||
|
||||
buffer := new(bytes.Buffer)
|
||||
encoder := toml.NewEncoder(buffer)
|
||||
err := encoder.Encode(copy)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
encoder := tomlsdk.NewEncoder(buffer)
|
||||
|
||||
if err := encoder.Encode(copy); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return print.NewGenerator(
|
||||
"toml",
|
||||
print.WithContent(strings.TrimSuffix(buffer.String(), "\n")),
|
||||
), nil
|
||||
t.Generator.Funcs(print.WithContent(strings.TrimSuffix(buffer.String(), "\n")))
|
||||
|
||||
return nil
|
||||
|
||||
}
|
||||
|
||||
|
||||
@@ -15,8 +15,8 @@ import (
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
|
||||
"github.com/terraform-docs/terraform-docs/internal/print"
|
||||
"github.com/terraform-docs/terraform-docs/internal/testutil"
|
||||
"github.com/terraform-docs/terraform-docs/print"
|
||||
"github.com/terraform-docs/terraform-docs/terraform"
|
||||
)
|
||||
|
||||
@@ -114,12 +114,12 @@ func TestToml(t *testing.T) {
|
||||
module, err := testutil.GetModule(options)
|
||||
assert.Nil(err)
|
||||
|
||||
formatter := NewTOML(&tt.settings)
|
||||
formatter := NewTOML(tt.settings.ToConfig())
|
||||
|
||||
generator, err := formatter.Generate(module)
|
||||
err = formatter.Generate(module)
|
||||
assert.Nil(err)
|
||||
|
||||
actual, err := generator.ExecuteTemplate("")
|
||||
actual, err := formatter.ExecuteTemplate("")
|
||||
|
||||
assert.Nil(err)
|
||||
assert.Equal(expected, actual)
|
||||
|
||||
66
format/type.go
Normal file
66
format/type.go
Normal file
@@ -0,0 +1,66 @@
|
||||
/*
|
||||
Copyright 2021 The terraform-docs Authors.
|
||||
|
||||
Licensed under the MIT license (the "License"); you may not
|
||||
use this file except in compliance with the License.
|
||||
|
||||
You may obtain a copy of the License at the LICENSE file in
|
||||
the root directory of this source tree.
|
||||
*/
|
||||
|
||||
package format
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/terraform-docs/terraform-docs/print"
|
||||
"github.com/terraform-docs/terraform-docs/terraform"
|
||||
)
|
||||
|
||||
// Type represents an output format type (e.g. json, markdown table, yaml, etc).
|
||||
type Type interface {
|
||||
Generate(*terraform.Module) error // generate the Terraform module
|
||||
|
||||
Content() string // all the sections combined based on the underlying format
|
||||
|
||||
Header() string // header section based on the underlying format
|
||||
Footer() string // footer section based on the underlying format
|
||||
Inputs() string // inputs section based on the underlying format
|
||||
Modules() string // modules section based on the underlying format
|
||||
Outputs() string // outputs section based on the underlying format
|
||||
Providers() string // providers section based on the underlying format
|
||||
Requirements() string // requirements section based on the underlying format
|
||||
Resources() string // resources section based on the underlying format
|
||||
|
||||
ExecuteTemplate(contentTmpl string) (string, error)
|
||||
}
|
||||
|
||||
// initializerFn returns a concrete implementation of an Engine.
|
||||
type initializerFn func(*print.Config) Type
|
||||
|
||||
// initializers list of all registered engine initializer functions.
|
||||
var initializers = make(map[string]initializerFn)
|
||||
|
||||
// register a formatter engine initializer function.
|
||||
func register(e map[string]initializerFn) {
|
||||
if e == nil {
|
||||
return
|
||||
}
|
||||
for k, v := range e {
|
||||
initializers[k] = v
|
||||
}
|
||||
}
|
||||
|
||||
// New initializes and returns the concrete implementation of
|
||||
// format.Engine based on the provided 'name', for example for name
|
||||
// of 'json' it will return '*format.JSON' through 'format.NewJSON'
|
||||
// function.
|
||||
// func New(name string, settings *print.Settings) (Generator, error) {
|
||||
func New(config *print.Config) (Type, error) {
|
||||
name := config.Formatter
|
||||
fn, ok := initializers[name]
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("formatter '%s' not found", name)
|
||||
}
|
||||
return fn(config), nil
|
||||
}
|
||||
212
format/type_test.go
Normal file
212
format/type_test.go
Normal file
@@ -0,0 +1,212 @@
|
||||
// /*
|
||||
// Copyright 2021 The terraform-docs Authors.
|
||||
|
||||
// Licensed under the MIT license (the "License"); you may not
|
||||
// use this file except in compliance with the License.
|
||||
|
||||
// You may obtain a copy of the License at the LICENSE file in
|
||||
// the root directory of this source tree.
|
||||
// */
|
||||
|
||||
package format
|
||||
|
||||
import (
|
||||
"reflect"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
|
||||
"github.com/terraform-docs/terraform-docs/print"
|
||||
)
|
||||
|
||||
func TestFormatType(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
format string
|
||||
expected string
|
||||
wantErr bool
|
||||
}{
|
||||
{
|
||||
name: "format type from name",
|
||||
format: "asciidoc",
|
||||
expected: "*format.asciidocTable",
|
||||
wantErr: false,
|
||||
},
|
||||
{
|
||||
name: "format type from name",
|
||||
format: "adoc",
|
||||
expected: "*format.asciidocTable",
|
||||
wantErr: false,
|
||||
},
|
||||
{
|
||||
name: "format type from name",
|
||||
format: "asciidoc document",
|
||||
expected: "*format.asciidocDocument",
|
||||
wantErr: false,
|
||||
},
|
||||
{
|
||||
name: "format type from name",
|
||||
format: "asciidoc doc",
|
||||
expected: "*format.asciidocDocument",
|
||||
wantErr: false,
|
||||
},
|
||||
{
|
||||
name: "format type from name",
|
||||
format: "adoc document",
|
||||
expected: "*format.asciidocDocument",
|
||||
wantErr: false,
|
||||
},
|
||||
{
|
||||
name: "format type from name",
|
||||
format: "adoc doc",
|
||||
expected: "*format.asciidocDocument",
|
||||
wantErr: false,
|
||||
},
|
||||
{
|
||||
name: "format type from name",
|
||||
format: "asciidoc table",
|
||||
expected: "*format.asciidocTable",
|
||||
wantErr: false,
|
||||
},
|
||||
{
|
||||
name: "format type from name",
|
||||
format: "asciidoc tbl",
|
||||
expected: "*format.asciidocTable",
|
||||
wantErr: false,
|
||||
},
|
||||
{
|
||||
name: "format type from name",
|
||||
format: "adoc table",
|
||||
expected: "*format.asciidocTable",
|
||||
wantErr: false,
|
||||
},
|
||||
{
|
||||
name: "format type from name",
|
||||
format: "adoc tbl",
|
||||
expected: "*format.asciidocTable",
|
||||
wantErr: false,
|
||||
},
|
||||
{
|
||||
name: "format type from name",
|
||||
format: "json",
|
||||
expected: "*format.json",
|
||||
wantErr: false,
|
||||
},
|
||||
{
|
||||
name: "format type from name",
|
||||
format: "markdown",
|
||||
expected: "*format.markdownTable",
|
||||
wantErr: false,
|
||||
},
|
||||
{
|
||||
name: "format type from name",
|
||||
format: "md",
|
||||
expected: "*format.markdownTable",
|
||||
wantErr: false,
|
||||
},
|
||||
{
|
||||
name: "format type from name",
|
||||
format: "markdown document",
|
||||
expected: "*format.markdownDocument",
|
||||
wantErr: false,
|
||||
},
|
||||
{
|
||||
name: "format type from name",
|
||||
format: "markdown doc",
|
||||
expected: "*format.markdownDocument",
|
||||
wantErr: false,
|
||||
},
|
||||
{
|
||||
name: "format type from name",
|
||||
format: "md document",
|
||||
expected: "*format.markdownDocument",
|
||||
wantErr: false,
|
||||
},
|
||||
{
|
||||
name: "format type from name",
|
||||
format: "md doc",
|
||||
expected: "*format.markdownDocument",
|
||||
wantErr: false,
|
||||
},
|
||||
{
|
||||
name: "format type from name",
|
||||
format: "markdown table",
|
||||
expected: "*format.markdownTable",
|
||||
wantErr: false,
|
||||
},
|
||||
{
|
||||
name: "format type from name",
|
||||
format: "markdown tbl",
|
||||
expected: "*format.markdownTable",
|
||||
wantErr: false,
|
||||
},
|
||||
{
|
||||
name: "format type from name",
|
||||
format: "md table",
|
||||
expected: "*format.markdownTable",
|
||||
wantErr: false,
|
||||
},
|
||||
{
|
||||
name: "format type from name",
|
||||
format: "md tbl",
|
||||
expected: "*format.markdownTable",
|
||||
wantErr: false,
|
||||
},
|
||||
{
|
||||
name: "format type from name",
|
||||
format: "pretty",
|
||||
expected: "*format.pretty",
|
||||
wantErr: false,
|
||||
},
|
||||
{
|
||||
name: "format type from name",
|
||||
format: "tfvars hcl",
|
||||
expected: "*format.tfvarsHCL",
|
||||
wantErr: false,
|
||||
},
|
||||
{
|
||||
name: "format type from name",
|
||||
format: "tfvars json",
|
||||
expected: "*format.tfvarsJSON",
|
||||
wantErr: false,
|
||||
},
|
||||
{
|
||||
name: "format type from name",
|
||||
format: "toml",
|
||||
expected: "*format.toml",
|
||||
wantErr: false,
|
||||
},
|
||||
{
|
||||
name: "format type from name",
|
||||
format: "xml",
|
||||
expected: "*format.xml",
|
||||
wantErr: false,
|
||||
},
|
||||
{
|
||||
name: "format type from name",
|
||||
format: "yaml",
|
||||
expected: "*format.yaml",
|
||||
wantErr: false,
|
||||
},
|
||||
{
|
||||
name: "format type from name",
|
||||
format: "unknown",
|
||||
expected: "",
|
||||
wantErr: true,
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
assert := assert.New(t)
|
||||
config := print.DefaultConfig()
|
||||
config.Formatter = tt.format
|
||||
actual, err := New(config)
|
||||
if tt.wantErr {
|
||||
assert.NotNil(err)
|
||||
} else {
|
||||
assert.Nil(err)
|
||||
assert.Equal(tt.expected, reflect.TypeOf(actual).String())
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -17,7 +17,9 @@ import (
|
||||
"regexp"
|
||||
"strings"
|
||||
|
||||
"github.com/terraform-docs/terraform-docs/print"
|
||||
"github.com/terraform-docs/terraform-docs/template"
|
||||
"github.com/terraform-docs/terraform-docs/terraform"
|
||||
)
|
||||
|
||||
// sanitize cleans a Markdown document to soothe linters.
|
||||
@@ -99,3 +101,58 @@ func readTemplateItems(efs embed.FS, prefix string) []*template.Item {
|
||||
}
|
||||
return items
|
||||
}
|
||||
|
||||
// copySections sets the sections that'll be printed
|
||||
func copySections(settings *print.Settings, src *terraform.Module) *terraform.Module {
|
||||
dest := &terraform.Module{
|
||||
Header: "",
|
||||
Footer: "",
|
||||
Inputs: make([]*terraform.Input, 0),
|
||||
ModuleCalls: make([]*terraform.ModuleCall, 0),
|
||||
Outputs: make([]*terraform.Output, 0),
|
||||
Providers: make([]*terraform.Provider, 0),
|
||||
Requirements: make([]*terraform.Requirement, 0),
|
||||
Resources: make([]*terraform.Resource, 0),
|
||||
}
|
||||
|
||||
if settings.ShowHeader {
|
||||
dest.Header = src.Header
|
||||
}
|
||||
if settings.ShowFooter {
|
||||
dest.Footer = src.Footer
|
||||
}
|
||||
if settings.ShowInputs {
|
||||
dest.Inputs = src.Inputs
|
||||
}
|
||||
if settings.ShowModuleCalls {
|
||||
dest.ModuleCalls = src.ModuleCalls
|
||||
}
|
||||
if settings.ShowOutputs {
|
||||
dest.Outputs = src.Outputs
|
||||
}
|
||||
if settings.ShowProviders {
|
||||
dest.Providers = src.Providers
|
||||
}
|
||||
if settings.ShowRequirements {
|
||||
dest.Requirements = src.Requirements
|
||||
}
|
||||
if settings.ShowResources || settings.ShowDataSources {
|
||||
dest.Resources = filterResourcesByMode(settings, src.Resources)
|
||||
}
|
||||
|
||||
return dest
|
||||
}
|
||||
|
||||
// filterResourcesByMode returns the managed or data resources defined by the show argument
|
||||
func filterResourcesByMode(settings *print.Settings, module []*terraform.Resource) []*terraform.Resource {
|
||||
resources := make([]*terraform.Resource, 0)
|
||||
for _, r := range module {
|
||||
if settings.ShowResources && r.Mode == "managed" {
|
||||
resources = append(resources, r)
|
||||
}
|
||||
if settings.ShowDataSources && r.Mode == "data" {
|
||||
resources = append(resources, r)
|
||||
}
|
||||
}
|
||||
return resources
|
||||
}
|
||||
|
||||
@@ -11,49 +11,44 @@ the root directory of this source tree.
|
||||
package format
|
||||
|
||||
import (
|
||||
"encoding/xml"
|
||||
xmlsdk "encoding/xml"
|
||||
"strings"
|
||||
|
||||
"github.com/terraform-docs/terraform-docs/internal/print"
|
||||
"github.com/terraform-docs/terraform-docs/print"
|
||||
"github.com/terraform-docs/terraform-docs/terraform"
|
||||
)
|
||||
|
||||
// XML represents XML format.
|
||||
type XML struct {
|
||||
// xml represents XML format.
|
||||
type xml struct {
|
||||
*print.Generator
|
||||
|
||||
config *print.Config
|
||||
settings *print.Settings
|
||||
}
|
||||
|
||||
// NewXML returns new instance of XML.
|
||||
func NewXML(settings *print.Settings) print.Engine {
|
||||
return &XML{
|
||||
settings: settings,
|
||||
func NewXML(config *print.Config) Type {
|
||||
settings, _ := config.Extract()
|
||||
|
||||
return &xml{
|
||||
Generator: print.NewGenerator("xml", config.ModuleRoot),
|
||||
config: config,
|
||||
settings: settings,
|
||||
}
|
||||
}
|
||||
|
||||
// Generate a Terraform module as xml.
|
||||
func (x *XML) Generate(module *terraform.Module) (*print.Generator, error) {
|
||||
copy := &terraform.Module{
|
||||
Header: "",
|
||||
Footer: "",
|
||||
Inputs: make([]*terraform.Input, 0),
|
||||
ModuleCalls: make([]*terraform.ModuleCall, 0),
|
||||
Outputs: make([]*terraform.Output, 0),
|
||||
Providers: make([]*terraform.Provider, 0),
|
||||
Requirements: make([]*terraform.Requirement, 0),
|
||||
Resources: make([]*terraform.Resource, 0),
|
||||
}
|
||||
func (x *xml) Generate(module *terraform.Module) error {
|
||||
copy := copySections(x.settings, module)
|
||||
|
||||
print.CopySections(x.settings, module, copy)
|
||||
|
||||
out, err := xml.MarshalIndent(copy, "", " ")
|
||||
out, err := xmlsdk.MarshalIndent(copy, "", " ")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
return err
|
||||
}
|
||||
|
||||
return print.NewGenerator(
|
||||
"xml",
|
||||
print.WithContent(strings.TrimSuffix(string(out), "\n")),
|
||||
), nil
|
||||
x.Generator.Funcs(print.WithContent(strings.TrimSuffix(string(out), "\n")))
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func init() {
|
||||
|
||||
@@ -15,8 +15,8 @@ import (
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
|
||||
"github.com/terraform-docs/terraform-docs/internal/print"
|
||||
"github.com/terraform-docs/terraform-docs/internal/testutil"
|
||||
"github.com/terraform-docs/terraform-docs/print"
|
||||
"github.com/terraform-docs/terraform-docs/terraform"
|
||||
)
|
||||
|
||||
@@ -114,12 +114,12 @@ func TestXml(t *testing.T) {
|
||||
module, err := testutil.GetModule(options)
|
||||
assert.Nil(err)
|
||||
|
||||
formatter := NewXML(&tt.settings)
|
||||
formatter := NewXML(tt.settings.ToConfig())
|
||||
|
||||
generator, err := formatter.Generate(module)
|
||||
err = formatter.Generate(module)
|
||||
assert.Nil(err)
|
||||
|
||||
actual, err := generator.ExecuteTemplate("")
|
||||
actual, err := formatter.ExecuteTemplate("")
|
||||
|
||||
assert.Nil(err)
|
||||
assert.Equal(expected, actual)
|
||||
|
||||
@@ -14,53 +14,46 @@ import (
|
||||
"bytes"
|
||||
"strings"
|
||||
|
||||
"gopkg.in/yaml.v3"
|
||||
yamlv3 "gopkg.in/yaml.v3"
|
||||
|
||||
"github.com/terraform-docs/terraform-docs/internal/print"
|
||||
"github.com/terraform-docs/terraform-docs/print"
|
||||
"github.com/terraform-docs/terraform-docs/terraform"
|
||||
)
|
||||
|
||||
// YAML represents YAML format.
|
||||
type YAML struct {
|
||||
// yaml represents YAML format.
|
||||
type yaml struct {
|
||||
*print.Generator
|
||||
|
||||
config *print.Config
|
||||
settings *print.Settings
|
||||
}
|
||||
|
||||
// NewYAML returns new instance of YAML.
|
||||
func NewYAML(settings *print.Settings) print.Engine {
|
||||
return &YAML{
|
||||
settings: settings,
|
||||
func NewYAML(config *print.Config) Type {
|
||||
settings, _ := config.Extract()
|
||||
|
||||
return &yaml{
|
||||
Generator: print.NewGenerator("yaml", config.ModuleRoot),
|
||||
config: config,
|
||||
settings: settings,
|
||||
}
|
||||
}
|
||||
|
||||
// Generate a Terraform module as yaml.
|
||||
func (y *YAML) Generate(module *terraform.Module) (*print.Generator, error) {
|
||||
copy := &terraform.Module{
|
||||
Header: "",
|
||||
Footer: "",
|
||||
Inputs: make([]*terraform.Input, 0),
|
||||
ModuleCalls: make([]*terraform.ModuleCall, 0),
|
||||
Outputs: make([]*terraform.Output, 0),
|
||||
Providers: make([]*terraform.Provider, 0),
|
||||
Requirements: make([]*terraform.Requirement, 0),
|
||||
Resources: make([]*terraform.Resource, 0),
|
||||
}
|
||||
|
||||
print.CopySections(y.settings, module, copy)
|
||||
// Generate a Terraform module as YAML.
|
||||
func (y *yaml) Generate(module *terraform.Module) error {
|
||||
copy := copySections(y.settings, module)
|
||||
|
||||
buffer := new(bytes.Buffer)
|
||||
|
||||
encoder := yaml.NewEncoder(buffer)
|
||||
encoder := yamlv3.NewEncoder(buffer)
|
||||
encoder.SetIndent(2)
|
||||
|
||||
err := encoder.Encode(copy)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
if err := encoder.Encode(copy); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return print.NewGenerator(
|
||||
"yaml",
|
||||
print.WithContent(strings.TrimSuffix(buffer.String(), "\n")),
|
||||
), nil
|
||||
y.Generator.Funcs(print.WithContent(strings.TrimSuffix(buffer.String(), "\n")))
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func init() {
|
||||
|
||||
@@ -15,8 +15,8 @@ import (
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
|
||||
"github.com/terraform-docs/terraform-docs/internal/print"
|
||||
"github.com/terraform-docs/terraform-docs/internal/testutil"
|
||||
"github.com/terraform-docs/terraform-docs/print"
|
||||
"github.com/terraform-docs/terraform-docs/terraform"
|
||||
)
|
||||
|
||||
@@ -114,12 +114,12 @@ func TestYaml(t *testing.T) {
|
||||
module, err := testutil.GetModule(options)
|
||||
assert.Nil(err)
|
||||
|
||||
formatter := NewYAML(&tt.settings)
|
||||
formatter := NewYAML(tt.settings.ToConfig())
|
||||
|
||||
generator, err := formatter.Generate(module)
|
||||
err = formatter.Generate(module)
|
||||
assert.Nil(err)
|
||||
|
||||
actual, err := generator.ExecuteTemplate("")
|
||||
actual, err := formatter.ExecuteTemplate("")
|
||||
|
||||
assert.Nil(err)
|
||||
assert.Equal(expected, actual)
|
||||
|
||||
Reference in New Issue
Block a user