forked from terraform-docs/terraform-docs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.go
78 lines (65 loc) · 1.97 KB
/
client.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
/*
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 (
"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
}