Plugins: Update renderer plugin source (#80643)
* rework renderer plugin source * add tests
This commit is contained in:
@@ -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
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user