diff --git a/docs/plugins.md b/docs/plugins.md index b51d11bfd..5faff1156 100644 --- a/docs/plugins.md +++ b/docs/plugins.md @@ -59,3 +59,10 @@ pipeline: Writing a plugin is as easy as pulling out a set of operators in a working configuration file, then templatizing it with any parts of the config that need to be treated as variable. In the example of the Tomcat access log plugin above, that just means adding variables for `path` and `output`. Plugins use Go's [`text/template`](https://golang.org/pkg/text/template/) package for template rendering. All fields from the plugin configuration are available as variables in the templates except the `type` field. + +### Additional Template Functions + +| Function Key | Argument(s) | Description | +| --- | --- | --- | +| `makeSlice` | `...Interface{}` | Takes any amount of interfaces and adds them into a slice and returns it. | +| `default` | `default interface{}`, `value interface{}` | Returns the default if the value is nil. | \ No newline at end of file diff --git a/plugin/plugin.go b/plugin/plugin.go index 5d62f8576..e434c4556 100644 --- a/plugin/plugin.go +++ b/plugin/plugin.go @@ -240,6 +240,7 @@ func RegisterPlugins(pluginDir string, registry *operator.Registry) []error { func pluginFuncs() template.FuncMap { funcs := make(map[string]interface{}) funcs["default"] = defaultPluginFunc + funcs["makeSlice"] = makeSlice return funcs } @@ -250,3 +251,8 @@ func defaultPluginFunc(def interface{}, val interface{}) interface{} { } return val } + +// makeSlice is a plugin function returns a slice of all the given arguements +func makeSlice(args ...interface{}) []interface{} { + return args +} diff --git a/plugin/plugin_test.go b/plugin/plugin_test.go index 7928fa093..e755df30d 100644 --- a/plugin/plugin_test.go +++ b/plugin/plugin_test.go @@ -681,6 +681,16 @@ func TestDefaultPluginFuncWithoutValue(t *testing.T) { require.Equal(t, "default_value", result) } +func TestMakeSlicePluginFuncWithString(t *testing.T) { + result := makeSlice("string1", "string2", "string3", "string4") + require.Equal(t, []interface{}{"string1", "string2", "string3", "string4"}, result) +} + +func TestDefaultPluginFuncWithInteger(t *testing.T) { + result := makeSlice(1, 2, 3, 4, 5) + require.Equal(t, []interface{}{1, 2, 3, 4, 5}, result) +} + func TestSplitPluginFile(t *testing.T) { cases := map[int]struct { input string