Files
terraform-docs/docs/developer-guide/plugins.md
Khosrow Moossavi 4a9ffe7c33 Move plugin-sdk to in-tree in core repository
Moving terraform-docs/plugin-sdk standalone module to in-tree, because
maintaining both of them, specifically if anything needs to be added to
Config, or terraform will required dual effort on both repository. As
such now everything is consolidated under one repository. Example usage
for plugin developer after this move is as follow:

```go
package main

import (
	_ "embed" //nolint

	"github.com/terraform-docs/terraform-docs/plugin"
	"github.com/terraform-docs/terraform-docs/print"
	"github.com/terraform-docs/terraform-docs/template"
	"github.com/terraform-docs/terraform-docs/terraform"
)

func main() {
	plugin.Serve(&plugin.ServeOpts{
		Name:    "template",
		Version: "0.1.0",
		Printer: printer,
	})
}

//go:embed sections.tmpl
var tplCustom []byte

// Print the custom format template. You have all the flexibility to generate
// the output however you choose to.
func printer(config *print.Config, module *terraform.Module) (string, error) {
	tpl := template.New(config,
		&template.Item{Name: "custom", Text: string(tplCustom)},
	)

	rendered, err := tpl.Render("custom", module)
	if err != nil {
		return "", err
	}

	return rendered, nil
}
```

Signed-off-by: Khosrow Moossavi <khos2ow@gmail.com>
2021-10-05 18:54:49 -04:00

2.0 KiB

title, description, menu, weight, toc
title description menu weight toc
Plugins terraform-docs plugin development guide
docs
parent
developer-guide
310 false

Generated output can be heavily customized with [content], but if using that is not enough for your use-case, you can write your own plugin.

In order to install a plugin the following steps are needed:

  • download the plugin and place it in ~/.tfdocs.d/plugins (or ./.tfdocs.d/plugins)
  • make sure the plugin file name is tfdocs-format-<NAME>
  • modify [formatter] of .terraform-docs.yml file to be <NAME>

Important notes:

  • if the plugin file name is different than the example above, terraform-docs won't be able to to pick it up nor register it properly
  • you can only use plugin thorough .terraform-docs.yml file and it cannot be used with CLI arguments

To create a new plugin create a new repository called tfdocs-format-<NAME> with following main.go:

package main

import (
    _ "embed" //nolint

    "github.com/terraform-docs/terraform-docs/plugin"
    "github.com/terraform-docs/terraform-docs/print"
    "github.com/terraform-docs/terraform-docs/template"
    "github.com/terraform-docs/terraform-docs/terraform"
)

func main() {
    plugin.Serve(&plugin.ServeOpts{
        Name:    "<NAME>",
        Version: "0.1.0",
        Printer: printerFunc,
    })
}

//go:embed sections.tmpl
var tplCustom []byte

// printerFunc the function being executed by the plugin client.
func printerFunc(config *print.Config, module *terraform.Module) (string, error) {
    tpl := template.New(config,
        &template.Item{Name: "custom", Text: string(tplCustom)},
    )

    rendered, err := tpl.Render("custom", module)
    if err != nil {
        return "", err
    }

    return rendered, nil
}

Please refer to [tfdocs-format-template] for more details. You can create a new repository from it by clicking on Use this template button.

[content]: {{< ref "content" >}} [formatter]: {{< ref "formatter" >}} [tfdocs-format-template]: https://github.com/terraform-docs/tfdocs-format-template