11package plugin
22
3- import "github.com/DevLabFoundry/configmanager/v3/internal/config"
3+ import (
4+ "net/rpc"
5+
6+ "github.com/DevLabFoundry/configmanager/v3/internal/config"
7+ "github.com/hashicorp/go-plugin"
8+ )
49
510// Plugin is responsible for managing plugins within configmanager
611//
@@ -14,3 +19,58 @@ type Plugin struct {
1419 fallbackPaths []string
1520 engineInstance * Engine
1621}
22+
23+ // ValueProvider is the interface that we're exposing as a plugin.
24+ type ValueProvider interface {
25+ Value (token string , metadata string ) (string , error )
26+ }
27+
28+ // Here is an implementation that talks over RPC
29+ type StorePluginRPC struct { client * rpc.Client }
30+
31+ func (g * StorePluginRPC ) Greet () string {
32+ var resp string
33+ err := g .client .Call ("Plugin.Greet" , new (interface {}), & resp )
34+ if err != nil {
35+ // You usually want your interfaces to return errors. If they don't,
36+ // there isn't much other choice here.
37+ panic (err )
38+ }
39+
40+ return resp
41+ }
42+
43+ // Here is the RPC server that GreeterRPC talks to, conforming to
44+ // the requirements of net/rpc
45+ type GreeterRPCServer struct {
46+ // This is the real implementation
47+ Impl ValueProvider
48+ }
49+
50+ func (s * GreeterRPCServer ) Greet (args interface {}, resp * string ) error {
51+ * resp = s .Impl .Value ()
52+ return nil
53+ }
54+
55+ // This is the implementation of plugin.Plugin so we can serve/consume this
56+ //
57+ // This has two methods: Server must return an RPC server for this plugin
58+ // type. We construct a GreeterRPCServer for this.
59+ //
60+ // Client must return an implementation of our interface that communicates
61+ // over an RPC client. We return GreeterRPC for this.
62+ //
63+ // Ignore MuxBroker. That is used to create more multiplexed streams on our
64+ // plugin connection and is a more advanced use case.
65+ type GreeterPlugin struct {
66+ // Impl Injection
67+ Impl ValueProvider
68+ }
69+
70+ func (p * GreeterPlugin ) Server (* plugin.MuxBroker ) (interface {}, error ) {
71+ return & GreeterRPCServer {Impl : p .Impl }, nil
72+ }
73+
74+ func (GreeterPlugin ) Client (b * plugin.MuxBroker , c * rpc.Client ) (interface {}, error ) {
75+ return & StorePluginRPC {client : c }, nil
76+ }
0 commit comments