mirror of
https://github.com/terraform-docs/terraform-docs.git
synced 2026-03-27 04:48:33 +07:00
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>
This commit is contained in:
84
plugin/client.go
Normal file
84
plugin/client.go
Normal file
@@ -0,0 +1,84 @@
|
||||
/*
|
||||
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 (
|
||||
"net/rpc"
|
||||
"os"
|
||||
"os/exec"
|
||||
|
||||
"github.com/hashicorp/go-hclog"
|
||||
goplugin "github.com/hashicorp/go-plugin"
|
||||
|
||||
"github.com/terraform-docs/terraform-docs/print"
|
||||
"github.com/terraform-docs/terraform-docs/terraform"
|
||||
)
|
||||
|
||||
// Client is an RPC Client for the host.
|
||||
type Client struct {
|
||||
rpcClient *rpc.Client
|
||||
broker *goplugin.MuxBroker
|
||||
}
|
||||
|
||||
// ClientOpts is an option for initializing a Client.
|
||||
type ClientOpts struct {
|
||||
Cmd *exec.Cmd
|
||||
}
|
||||
|
||||
// ExecuteArgs is the collection of arguments being sent by terraform-docs
|
||||
// core while executing the plugin command.
|
||||
type ExecuteArgs struct {
|
||||
Module *terraform.Module
|
||||
Config *print.Config
|
||||
}
|
||||
|
||||
// NewClient is a wrapper of plugin.NewClient.
|
||||
func NewClient(opts *ClientOpts) *goplugin.Client {
|
||||
return goplugin.NewClient(&goplugin.ClientConfig{
|
||||
HandshakeConfig: handshakeConfig,
|
||||
Plugins: map[string]goplugin.Plugin{
|
||||
"formatter": &formatter{},
|
||||
},
|
||||
Cmd: opts.Cmd,
|
||||
Logger: hclog.New(&hclog.LoggerOptions{
|
||||
Name: "plugin",
|
||||
Output: os.Stderr,
|
||||
Level: hclog.LevelFromString(os.Getenv("TFDOCS_LOG")),
|
||||
}),
|
||||
})
|
||||
}
|
||||
|
||||
// Name calls the server-side Name method and returns its version.
|
||||
func (c *Client) Name() (string, error) {
|
||||
var resp string
|
||||
err := c.rpcClient.Call("Plugin.Name", new(interface{}), &resp)
|
||||
return resp, err
|
||||
}
|
||||
|
||||
// Version calls the server-side Version method and returns its version.
|
||||
func (c *Client) Version() (string, error) {
|
||||
var resp string
|
||||
err := c.rpcClient.Call("Plugin.Version", new(interface{}), &resp)
|
||||
return resp, err
|
||||
}
|
||||
|
||||
// Execute calls the server-side Execute method and returns generated output.
|
||||
func (c *Client) Execute(args *ExecuteArgs) (string, error) {
|
||||
var resp string
|
||||
err := c.rpcClient.Call("Plugin.Execute", args, &resp)
|
||||
return resp, err
|
||||
}
|
||||
68
plugin/doc.go
Normal file
68
plugin/doc.go
Normal file
@@ -0,0 +1,68 @@
|
||||
/*
|
||||
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 contains the implementations needed to make
|
||||
// the built binary act as a plugin.
|
||||
//
|
||||
// A plugin is implemented as an RPC server and the host acts
|
||||
// as the client, sending analysis requests to the plugin.
|
||||
// Note that the server-client relationship here is the opposite of
|
||||
// the communication that takes place during the checking phase.
|
||||
//
|
||||
// Implementation details are hidden in go-plugin. This package is
|
||||
// essentially a wrapper for go-plugin.
|
||||
//
|
||||
// Usage
|
||||
//
|
||||
// A simple plugin can look like this:
|
||||
//
|
||||
// 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: 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
|
||||
// }
|
||||
//
|
||||
package plugin
|
||||
80
plugin/plugin.go
Normal file
80
plugin/plugin.go
Normal file
@@ -0,0 +1,80 @@
|
||||
/*
|
||||
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 (
|
||||
"encoding/gob"
|
||||
"net/rpc"
|
||||
|
||||
goplugin "github.com/hashicorp/go-plugin"
|
||||
|
||||
"github.com/terraform-docs/terraform-docs/internal/types"
|
||||
)
|
||||
|
||||
// handshakeConfig is used for UX. ProcotolVersion will be updated by incompatible changes.
|
||||
var handshakeConfig = goplugin.HandshakeConfig{
|
||||
ProtocolVersion: 7,
|
||||
MagicCookieKey: "TFDOCS_PLUGIN",
|
||||
MagicCookieValue: "A7U5oTDDJwdL6UKOw6RXATDa86NEo4xLK3rz7QqegT1N4EY66qb6UeAJDSxLwtXH",
|
||||
}
|
||||
|
||||
// formatter is a wrapper to satisfy the interface of go-plugin.
|
||||
type formatter struct {
|
||||
name string
|
||||
version string
|
||||
printer printFunc
|
||||
}
|
||||
|
||||
func newFormatter(name string, version string, printer printFunc) *formatter {
|
||||
return &formatter{
|
||||
name: name,
|
||||
version: version,
|
||||
printer: printer,
|
||||
}
|
||||
}
|
||||
|
||||
func (f *formatter) Name() string {
|
||||
return f.name
|
||||
}
|
||||
|
||||
func (f *formatter) Version() string {
|
||||
return f.version
|
||||
}
|
||||
|
||||
func (f *formatter) Execute(args *ExecuteArgs) (string, error) {
|
||||
return f.printer(args.Config, args.Module)
|
||||
}
|
||||
|
||||
// Server returns an RPC server acting as a plugin.
|
||||
func (f *formatter) Server(b *goplugin.MuxBroker) (interface{}, error) {
|
||||
return &Server{impl: f, broker: b}, nil
|
||||
}
|
||||
|
||||
// Client returns an RPC client for the host.
|
||||
func (formatter) Client(b *goplugin.MuxBroker, c *rpc.Client) (interface{}, error) {
|
||||
return &Client{rpcClient: c, broker: b}, nil
|
||||
}
|
||||
|
||||
func init() {
|
||||
gob.Register(new(types.Bool))
|
||||
gob.Register(new(types.Empty))
|
||||
gob.Register(new(types.List))
|
||||
gob.Register(new(types.Map))
|
||||
gob.Register(new(types.Nil))
|
||||
gob.Register(new(types.Number))
|
||||
gob.Register(new(types.String))
|
||||
}
|
||||
68
plugin/server.go
Normal file
68
plugin/server.go
Normal file
@@ -0,0 +1,68 @@
|
||||
/*
|
||||
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
|
||||
}
|
||||
Reference in New Issue
Block a user