Plugins: Update renderer plugin source (#80643)

* rework renderer plugin source

* add tests
This commit is contained in:
Will Browne
2024-01-18 11:06:33 +01:00
committed by GitHub
parent 4b113f87f9
commit 3885497553
10 changed files with 233 additions and 41 deletions
@@ -2,6 +2,10 @@ package sources
import (
"context"
"errors"
"os"
"path/filepath"
"slices"
"github.com/grafana/grafana/pkg/plugins"
)
@@ -36,3 +40,31 @@ func (s *LocalSource) DefaultSignature(_ context.Context) (plugins.Signature, bo
return plugins.Signature{}, false
}
}
func DirAsLocalSources(pluginsPath string, class plugins.Class) ([]*LocalSource, error) {
if pluginsPath == "" {
return []*LocalSource{}, errors.New("plugins path not configured")
}
// It's safe to ignore gosec warning G304 since the variable part of the file path comes from a configuration
// variable.
// nolint:gosec
d, err := os.ReadDir(pluginsPath)
if err != nil {
return []*LocalSource{}, errors.New("failed to open plugins path")
}
var pluginDirs []string
for _, dir := range d {
if dir.IsDir() || dir.Type()&os.ModeSymlink == os.ModeSymlink {
pluginDirs = append(pluginDirs, filepath.Join(pluginsPath, dir.Name()))
}
}
slices.Sort(pluginDirs)
var sources []*LocalSource
for _, dir := range pluginDirs {
sources = append(sources, NewLocalSource(class, []string{dir}))
}
return sources, nil
}