-
Notifications
You must be signed in to change notification settings - Fork 36
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ab924d4
commit eb08c87
Showing
8 changed files
with
605 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
/* | ||
* This file is part of remco. | ||
* © 2016 The Remco Authors | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
package plugin | ||
|
||
import ( | ||
"context" | ||
"net/rpc" | ||
"net/rpc/jsonrpc" | ||
"os" | ||
"path" | ||
|
||
"github.com/HeavyHorst/easyKV" | ||
berr "github.com/HeavyHorst/remco/backends/error" | ||
"github.com/HeavyHorst/remco/log" | ||
"github.com/HeavyHorst/remco/template" | ||
"github.com/Sirupsen/logrus" | ||
"github.com/natefinch/pie" | ||
) | ||
|
||
type plug struct { | ||
client *rpc.Client | ||
} | ||
|
||
// Plugin represents the config for a plugin. | ||
type Plugin struct { | ||
// the path to the plugin executable | ||
Path string | ||
Config map[string]interface{} | ||
template.Backend | ||
} | ||
|
||
// Connect creates the connection to the plugin and sends the config map to the same. | ||
func (p *Plugin) Connect() (template.Backend, error) { | ||
if p == nil { | ||
return template.Backend{}, berr.ErrNilConfig | ||
} | ||
|
||
p.Backend.Name = path.Base(p.Path) | ||
|
||
client, err := pie.StartProviderCodec(jsonrpc.NewClientCodec, os.Stderr, p.Path) | ||
if err != nil { | ||
return p.Backend, err | ||
} | ||
|
||
if p.Backend.Watch { | ||
log.WithFields(logrus.Fields{ | ||
"backend": p.Backend.Name, | ||
}).Warn("Watch is not supported, using interval instead") | ||
p.Backend.Watch = false | ||
} | ||
|
||
plugin := &plug{client} | ||
if err := plugin.Init(p.Config); err != nil { | ||
return p.Backend, err | ||
} | ||
|
||
p.Backend.ReadWatcher = plugin | ||
return p.Backend, nil | ||
} | ||
|
||
// Init sends the config map to the plugin | ||
// the plugin can then run some initialization tasks | ||
func (p *plug) Init(config map[string]interface{}) error { | ||
var result bool | ||
return p.client.Call("Plugin.Init", config, &result) | ||
} | ||
|
||
// GetValues queries the plugin for keys | ||
func (p *plug) GetValues(keys []string) (result map[string]string, err error) { | ||
err = p.client.Call("Plugin.GetValues", keys, &result) | ||
return result, err | ||
} | ||
|
||
// Close closes the client connection | ||
func (p *plug) Close() { | ||
p.client.Call("Plugin.Close", nil, nil) | ||
p.client.Close() | ||
} | ||
|
||
// WatchPrefix is not supported for now | ||
func (p *plug) WatchPrefix(prefix string, ctx context.Context, opts ...easyKV.WatchOption) (uint64, error) { | ||
return 0, easyKV.ErrWatchNotSupported | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.