Files
terraform-docs/internal/plugin/plugin.go
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

57 lines
1.5 KiB
Go

/*
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 plugin
import (
goplugin "github.com/hashicorp/go-plugin"
pluginsdk "github.com/terraform-docs/terraform-docs/plugin"
)
// namePrefix is the mandatory prefix for name of the plugin file. What
// comes after this is considered to be identifier of the plugin and in
// the overall ecosystem should be unique (as much as possible.)
const namePrefix = "tfdocs-format-"
// homePluginsRoot is the root directory of the plugins
var homePluginsRoot = "~/.tfdocs.d/plugins"
var localPluginsRoot = "./.tfdocs.d/plugins"
// List is an object caching discovered plugins and their corresponding
// clients. Basically, it is a wrapper for go-plugin and provides an API
// to handle them collectively.
type List struct {
formatters map[string]*pluginsdk.Client
clients map[string]*goplugin.Client
}
// All returns all registered plugins.
func (l *List) All() []*pluginsdk.Client {
all := make([]*pluginsdk.Client, 0)
for _, f := range l.formatters {
all = append(all, f)
}
return all
}
// Get plugin by its name.
func (l *List) Get(name string) (*pluginsdk.Client, bool) {
client, ok := l.formatters[name]
return client, ok
}
// Clean is a helper for ending plugin processes.
func (l *List) Clean() {
for _, client := range l.clients {
client.Kill()
}
}