-
Notifications
You must be signed in to change notification settings - Fork 2
/
plugins.go
44 lines (38 loc) · 900 Bytes
/
plugins.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
package main
import (
"errors"
"fmt"
"path"
"plugin"
"regexp"
"text/template"
)
var re *regexp.Regexp
func init() {
// match '_' | unicode_letter | unicode_digit
// (ie characters valid in template identifiers)
re = regexp.MustCompile(`^([_\pL\p{Nd}]+)\.so$`)
}
func loadPlugin(so *string, fm *template.FuncMap) error {
matches := re.FindStringSubmatch(path.Base(*so))
if matches == nil {
return errors.New("invalid characters in filename - must use underscores and unicode letters/digits only")
}
ns := matches[1]
plug, err := plugin.Open(*so)
if err != nil {
return err
}
sym, err := plug.Lookup("FuncMap")
if err != nil {
return err
}
f, ok := sym.(func() template.FuncMap)
if !ok {
return errors.New("could not assert symbol 'FuncMap' to be of type func() template.FuncMap")
}
for k, v := range f() {
(*fm)[fmt.Sprintf("%s_%s", ns, k)] = v
}
return nil
}