forked from imjerrybao/apex
-
Notifications
You must be signed in to change notification settings - Fork 0
/
plugin.go
76 lines (61 loc) · 1.63 KB
/
plugin.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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
package function
import (
"fmt"
)
// defaultPlugins are the default plugins which are required by Apex. Note that
// the order here is important for some plugins such as inference before the
// runtimes.
var defaultPlugins = []string{
"inference",
"golang",
"python",
"nodejs",
"hooks",
"env",
"shim",
}
// Hook type.
type Hook string
// Hooks available.
const (
// OpenHook is called when the function configuration is first loaded.
OpenHook Hook = "open"
// BuildHook is called when a build is started.
BuildHook = "build"
// CleanHook is called when a build is complete.
CleanHook = "clean"
// DeployHook is called after build and before a deploy.
DeployHook = "deploy"
)
// A Plugin is a chunk of isolated(ish) logic which reacts to various
// hooks within the system in order to implement specific features
// such as runtime inference or environment variable support.
type Plugin interface {
Run(hook Hook, fn *Function) error
}
// Registered plugins.
var plugins = make(map[string]Plugin)
// RegisterPlugin registers `plugin` by `name`.
func RegisterPlugin(name string, plugin Plugin) {
plugins[name] = plugin
}
// ByName returns the plugin by `name`.
func ByName(name string) (Plugin, error) {
if v, ok := plugins[name]; ok {
return v, nil
}
return nil, fmt.Errorf("invalid plugin %q", name)
}
// hook runs the default plugins, and those defined by Plugins in sequence.
func (f *Function) hook(hook Hook) error {
for _, name := range defaultPlugins {
plugin, err := ByName(name)
if err != nil {
return err
}
if err := plugin.Run(hook, f); err != nil {
return fmt.Errorf("%s: %s", name, err)
}
}
return nil
}