/
command.go
207 lines (176 loc) · 5.24 KB
/
command.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0
package plugin
import (
"context"
"github.com/LK4D4/joincontext"
"github.com/hashicorp/go-argmapper"
"github.com/hashicorp/go-plugin"
"google.golang.org/grpc"
"google.golang.org/protobuf/types/known/emptypb"
"github.com/hashicorp/vagrant-plugin-sdk/component"
"github.com/hashicorp/vagrant-plugin-sdk/core"
"github.com/hashicorp/vagrant-plugin-sdk/internal/funcspec"
"github.com/hashicorp/vagrant-plugin-sdk/proto/vagrant_plugin_sdk"
)
// CommandPlugin implements plugin.Plugin (specifically GRPCPlugin) for
// the Command component type.
type CommandPlugin struct {
plugin.NetRPCUnsupportedPlugin
Impl component.Command // Impl is the concrete implementation
*BasePlugin
}
func (p *CommandPlugin) GRPCServer(broker *plugin.GRPCBroker, s *grpc.Server) error {
vagrant_plugin_sdk.RegisterCommandServiceServer(s,
&commandServer{
Impl: p.Impl,
BaseServer: p.NewServer(broker, p.Impl),
},
)
return nil
}
func (p *CommandPlugin) GRPCClient(
ctx context.Context,
broker *plugin.GRPCBroker,
c *grpc.ClientConn,
) (interface{}, error) {
cl := vagrant_plugin_sdk.NewCommandServiceClient(c)
return &commandClient{
client: cl,
BaseClient: p.NewClient(ctx, broker, cl.(SeederClient)),
}, nil
}
// commandClient is an implementation of component.Command over gRPC.
type commandClient struct {
*BaseClient
client vagrant_plugin_sdk.CommandServiceClient
}
func (c *commandClient) CommandInfoFunc() interface{} {
spec, err := c.client.CommandInfoSpec(c.Ctx, &emptypb.Empty{})
if err != nil {
return funcErr(err)
}
spec.Result = nil
cb := func(ctx context.Context, args funcspec.Args) (*vagrant_plugin_sdk.Command_CommandInfo, error) {
ctx, _ = joincontext.Join(c.Ctx, ctx)
resp, err := c.client.CommandInfo(
ctx, &vagrant_plugin_sdk.FuncSpec_Args{Args: args},
)
if err != nil {
return nil, err
}
return resp.CommandInfo, nil
}
return c.GenerateFunc(spec, cb)
}
func (c *commandClient) CommandInfo() (*component.CommandInfo, error) {
f := c.CommandInfoFunc()
raw, err := c.CallDynamicFunc(f, (**component.CommandInfo)(nil),
argmapper.Typed(c.Ctx))
if err != nil {
return nil, err
}
return raw.(*component.CommandInfo), err
}
func (c *commandClient) ExecuteFunc(cliArgs []string) interface{} {
spec, err := c.client.ExecuteSpec(c.Ctx, &vagrant_plugin_sdk.Command_ExecuteSpecReq{
CommandArgs: cliArgs})
if err != nil {
return funcErr(err)
}
spec.Result = nil
cb := func(ctx context.Context, args funcspec.Args) (int32, error) {
ctx, _ = joincontext.Join(c.Ctx, ctx)
executeArgs := &vagrant_plugin_sdk.Command_ExecuteReq{
Spec: &vagrant_plugin_sdk.FuncSpec_Args{Args: args},
CommandArgs: cliArgs,
}
resp, err := c.client.Execute(ctx, executeArgs)
if err != nil {
return -1, err
}
return resp.ExitCode, nil
}
return c.GenerateFunc(spec, cb)
}
func (c *commandClient) Execute(cliArgs []string) (int32, error) {
f := c.ExecuteFunc(cliArgs)
raw, err := c.CallDynamicFunc(f, (*int32)(nil),
argmapper.Typed(c.Ctx))
if err != nil {
return -1, err
}
return raw.(int32), nil
}
// commandServer is a gRPC server that the client talks to and calls a
// real implementation of the component.
type commandServer struct {
*BaseServer
Impl component.Command
vagrant_plugin_sdk.UnsafeCommandServiceServer
}
func (s *commandServer) CommandInfoSpec(
ctx context.Context,
_ *emptypb.Empty,
) (*vagrant_plugin_sdk.FuncSpec, error) {
if err := isImplemented(s, "command"); err != nil {
return nil, err
}
return s.GenerateSpec(s.Impl.CommandInfoFunc())
}
func (s *commandServer) CommandInfo(
ctx context.Context,
req *vagrant_plugin_sdk.FuncSpec_Args,
) (*vagrant_plugin_sdk.Command_CommandInfoResp, error) {
raw, err := s.CallDynamicFunc(s.Impl.CommandInfoFunc(),
(**vagrant_plugin_sdk.Command_CommandInfo)(nil),
req.Args,
argmapper.Typed(ctx),
)
if err != nil {
s.Logger.Error("command info failed",
"error", err,
)
return nil, err
}
return &vagrant_plugin_sdk.Command_CommandInfoResp{
CommandInfo: raw.(*vagrant_plugin_sdk.Command_CommandInfo),
}, nil
}
func (s *commandServer) ExecuteSpec(
ctx context.Context,
req *vagrant_plugin_sdk.Command_ExecuteSpecReq,
) (*vagrant_plugin_sdk.FuncSpec, error) {
if err := isImplemented(s, "command"); err != nil {
return nil, err
}
return s.GenerateSpec(s.Impl.ExecuteFunc(req.CommandArgs))
}
func (s *commandServer) Execute(
ctx context.Context,
req *vagrant_plugin_sdk.Command_ExecuteReq,
) (*vagrant_plugin_sdk.Command_ExecuteResp, error) {
raw, err := s.CallDynamicFunc(s.Impl.ExecuteFunc(req.CommandArgs),
(*int32)(nil),
req.Spec.Args,
argmapper.Typed(ctx),
)
if err != nil {
s.Logger.Error("command execution failed",
"error", err,
)
return nil, err
}
result := &vagrant_plugin_sdk.Command_ExecuteResp{
ExitCode: raw.(int32),
}
return result, nil
}
var (
_ plugin.Plugin = (*CommandPlugin)(nil)
_ plugin.GRPCPlugin = (*CommandPlugin)(nil)
_ vagrant_plugin_sdk.CommandServiceServer = (*commandServer)(nil)
_ component.Command = (*commandClient)(nil)
_ core.Command = (*commandClient)(nil)
_ core.Seeder = (*commandClient)(nil)
)