Files
grafana/pkg/plugins/manager/loader/finder/finder.go
T
Will Browne ec82719372 Plugins: Introduce plugin package specific logger (#62204)
* refactor

* implement with infra log for now

* undo moving

* update package name

* update name

* fix tests

* update pretty signature

* update naming

* simplify

* fix typo

* delete comment

* fix import

* retrigger
2023-02-28 16:10:27 +01:00

91 lines
1.9 KiB
Go

package finder
import (
"errors"
"fmt"
"os"
"path/filepath"
"github.com/grafana/grafana/pkg/infra/fs"
"github.com/grafana/grafana/pkg/plugins/log"
"github.com/grafana/grafana/pkg/util"
)
var walk = util.Walk
type Finder struct {
log log.Logger
}
func New() Finder {
return Finder{log: log.New("plugin.finder")}
}
func (f *Finder) Find(pluginPaths []string) ([]string, error) {
var pluginJSONPaths []string
for _, path := range pluginPaths {
exists, err := fs.Exists(path)
if err != nil {
f.log.Warn("Error occurred when checking if plugin directory exists", "path", path, "err", err)
}
if !exists {
f.log.Warn("Skipping finding plugins as directory does not exist", "path", path)
continue
}
paths, err := f.getAbsPluginJSONPaths(path)
if err != nil {
return nil, err
}
pluginJSONPaths = append(pluginJSONPaths, paths...)
}
return pluginJSONPaths, nil
}
func (f *Finder) getAbsPluginJSONPaths(path string) ([]string, error) {
var pluginJSONPaths []string
var err error
path, err = filepath.Abs(path)
if err != nil {
return []string{}, err
}
if err := walk(path, true, true,
func(currentPath string, fi os.FileInfo, err error) error {
if err != nil {
if errors.Is(err, os.ErrNotExist) {
f.log.Error("Couldn't scan directory since it doesn't exist", "pluginDir", path, "err", err)
return nil
}
if errors.Is(err, os.ErrPermission) {
f.log.Error("Couldn't scan directory due to lack of permissions", "pluginDir", path, "err", err)
return nil
}
return fmt.Errorf("filepath.Walk reported an error for %q: %w", currentPath, err)
}
if fi.Name() == "node_modules" {
return util.ErrWalkSkipDir
}
if fi.IsDir() {
return nil
}
if fi.Name() != "plugin.json" {
return nil
}
pluginJSONPaths = append(pluginJSONPaths, currentPath)
return nil
}); err != nil {
return []string{}, err
}
return pluginJSONPaths, nil
}