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

69 lines
1.8 KiB
Go

/*
Copyright 2021 The terraform-docs Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package plugin
import (
goplugin "github.com/hashicorp/go-plugin"
"github.com/terraform-docs/terraform-docs/print"
"github.com/terraform-docs/terraform-docs/terraform"
)
// Server is an RPC Server acting as a plugin.
type Server struct {
impl *formatter
broker *goplugin.MuxBroker
}
type printFunc func(*print.Config, *terraform.Module) (string, error)
// ServeOpts is an option for serving a plugin.
type ServeOpts struct {
Name string
Version string
Printer printFunc
}
// Serve is a wrapper of plugin.Serve. This is entrypoint of all plugins.
func Serve(opts *ServeOpts) {
goplugin.Serve(&goplugin.ServeConfig{
HandshakeConfig: handshakeConfig,
Plugins: goplugin.PluginSet{
"formatter": newFormatter(opts.Name, opts.Version, opts.Printer),
},
})
}
// Name returns the version of the plugin.
func (s *Server) Name(args interface{}, resp *string) error {
*resp = s.impl.Name()
return nil
}
// Version returns the version of the plugin.
func (s *Server) Version(args interface{}, resp *string) error {
*resp = s.impl.Version()
return nil
}
// Execute returns the generated output.
func (s *Server) Execute(args *ExecuteArgs, resp *string) error {
r, err := s.impl.Execute(args)
*resp = r
return err
}