grafana/pkg/plugins/manager/filestore/fs.go
Will Browne 788b9afda3
Plugins: Make it possible to support multiple plugin versions (#82116)
* first pass

* use version in more places

* add comment

* update installer

* fix wire

* fix tests

* tidy

* simplify changes

* fix in mem

* remove unused step

* fix step dupe logic for child plugins + add tests
2024-02-12 12:47:49 +01:00

55 lines
1.1 KiB
Go

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
}
}