From 13698c63afa6baaa8d29db95f721f613eb3d166a Mon Sep 17 00:00:00 2001 From: Damien Lespiau Date: Sun, 27 Oct 2019 16:13:54 +0000 Subject: [PATCH] plugins: Add an echo plugin for testing the plugin mechanism --- plugins/jk-plugin-echo/main.go | 41 ++++++++++++++++++++++++++++++ tests/echo.json | 7 +++++ tests/test-plugin-echo.js | 4 +++ tests/test-plugin-echo.js.expected | 1 + 4 files changed, 53 insertions(+) create mode 100644 plugins/jk-plugin-echo/main.go create mode 100644 tests/echo.json create mode 100644 tests/test-plugin-echo.js create mode 100644 tests/test-plugin-echo.js.expected diff --git a/plugins/jk-plugin-echo/main.go b/plugins/jk-plugin-echo/main.go new file mode 100644 index 00000000..a3ced92c --- /dev/null +++ b/plugins/jk-plugin-echo/main.go @@ -0,0 +1,41 @@ +package main + +import ( + "os" + + "github.com/hashicorp/go-hclog" + "github.com/hashicorp/go-plugin" + + "github.com/jkcfg/jk/pkg/plugin/renderer" +) + +// Echo outputs its input. +type Echo struct { + log hclog.Logger +} + +// Render implements renderer.Renderer. +func (h *Echo) Render(input []byte) ([]byte, error) { + h.log.Debug("debug message from echo plugin") + return input, nil +} + +func main() { + logger := hclog.New(&hclog.LoggerOptions{ + Level: hclog.Info, + Output: os.Stderr, + }) + + r := &Echo{ + log: logger, + } + // pluginMap is the map of plugins we can dispense. + var pluginMap = map[string]plugin.Plugin{ + "renderer": &renderer.Plugin{Impl: r}, + } + + plugin.Serve(&plugin.ServeConfig{ + HandshakeConfig: renderer.RendererV1, + Plugins: pluginMap, + }) +} diff --git a/tests/echo.json b/tests/echo.json new file mode 100644 index 00000000..8a2891d2 --- /dev/null +++ b/tests/echo.json @@ -0,0 +1,7 @@ +{ + "name": "echo", + "version": "0.1.0", + "binaries": { + "linux-amd64": "jk-plugin-echo" + } +} diff --git a/tests/test-plugin-echo.js b/tests/test-plugin-echo.js new file mode 100644 index 00000000..914e4ff7 --- /dev/null +++ b/tests/test-plugin-echo.js @@ -0,0 +1,4 @@ +import * as std from '@jkcfg/std'; +import { render } from '@jkcfg/std/render'; + +render('echo.json', { message: 'success' }).then(r => std.log(r.message)); diff --git a/tests/test-plugin-echo.js.expected b/tests/test-plugin-echo.js.expected new file mode 100644 index 00000000..2e9ba477 --- /dev/null +++ b/tests/test-plugin-echo.js.expected @@ -0,0 +1 @@ +success