forked from grafana/grafana
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfs.go
54 lines (46 loc) · 1.09 KB
/
fs.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
package filestore
import (
"context"
"io"
"github.com/grafana/grafana/pkg/plugins"
"github.com/grafana/grafana/pkg/plugins/log"
"github.com/grafana/grafana/pkg/plugins/manager/registry"
)
type Service struct {
pluginRegistry registry.Service
log log.Logger
}
func ProvideService(pluginRegistry registry.Service) *Service {
return &Service{
pluginRegistry: pluginRegistry,
log: log.New("plugin.fs"),
}
}
func (s *Service) File(ctx context.Context, pluginID, pluginVersion, filename string) (*plugins.File, error) {
if p, exists := s.pluginRegistry.Plugin(ctx, pluginID, pluginVersion); exists {
f, err := p.File(filename)
if err != nil {
return nil, err
}
defer func() {
err = f.Close()
if err != nil {
s.log.Error("Could not close plugin file", "pluginId", p.ID, "file", filename)
}
}()
b, err := io.ReadAll(f)
if err != nil {
return nil, err
}
fi, err := f.Stat()
if err != nil {
return nil, err
}
return &plugins.File{
Content: b,
ModTime: fi.ModTime(),
}, nil
} else {
return nil, plugins.ErrPluginNotInstalled
}
}