forked from grafana/grafana
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathinitialization.go
59 lines (48 loc) · 1.54 KB
/
initialization.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
package initialization
import (
"context"
"github.com/grafana/grafana/pkg/plugins"
"github.com/grafana/grafana/pkg/plugins/config"
"github.com/grafana/grafana/pkg/plugins/log"
)
// Initializer is responsible for the Initialization stage of the plugin loader pipeline.
type Initializer interface {
Initialize(ctx context.Context, ps *plugins.Plugin) (*plugins.Plugin, error)
}
// InitializeFunc is the function used for the Initialize step of the Initialization stage.
type InitializeFunc func(ctx context.Context, p *plugins.Plugin) (*plugins.Plugin, error)
type Initialize struct {
cfg *config.PluginManagementCfg
initializeSteps []InitializeFunc
log log.Logger
}
type Opts struct {
InitializeFuncs []InitializeFunc
}
// New returns a new Initialization stage.
func New(cfg *config.PluginManagementCfg, opts Opts) *Initialize {
if opts.InitializeFuncs == nil {
opts.InitializeFuncs = []InitializeFunc{}
}
return &Initialize{
cfg: cfg,
initializeSteps: opts.InitializeFuncs,
log: log.New("plugins.initialization"),
}
}
// Initialize will execute the Initialize steps of the Initialization stage.
func (i *Initialize) Initialize(ctx context.Context, ps *plugins.Plugin) (*plugins.Plugin, error) {
if len(i.initializeSteps) == 0 {
return ps, nil
}
var err error
var ip *plugins.Plugin
for _, init := range i.initializeSteps {
ip, err = init(ctx, ps)
if err != nil {
i.log.Error("Could not initialize plugin", "pluginId", ps.ID, "error", err)
return nil, err
}
}
return ip, nil
}