mirror of
https://github.com/terraform-docs/terraform-docs.git
synced 2026-03-27 21:08:41 +07:00
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>
81 lines
2.1 KiB
Go
81 lines
2.1 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 (
|
|
"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))
|
|
}
|