mirror of https://github.com/grafana/grafana
This reverts commit ff34279ff4.
pull/69655/head^2
parent
d5cbac0f7b
commit
7a132680ef
@ -1,29 +0,0 @@ |
|||||||
package angulardetector |
|
||||||
|
|
||||||
import "github.com/grafana/grafana/pkg/plugins" |
|
||||||
|
|
||||||
// FakeInspector is an inspector whose Inspect function can be set to any function.
|
|
||||||
type FakeInspector struct { |
|
||||||
// InspectFunc is the function called when calling Inspect()
|
|
||||||
InspectFunc func(p *plugins.Plugin) (bool, error) |
|
||||||
} |
|
||||||
|
|
||||||
func (i *FakeInspector) Inspect(p *plugins.Plugin) (bool, error) { |
|
||||||
return i.InspectFunc(p) |
|
||||||
} |
|
||||||
|
|
||||||
var ( |
|
||||||
// AlwaysAngularFakeInspector is an inspector that always returns `true, nil`
|
|
||||||
AlwaysAngularFakeInspector = &FakeInspector{ |
|
||||||
InspectFunc: func(p *plugins.Plugin) (bool, error) { |
|
||||||
return true, nil |
|
||||||
}, |
|
||||||
} |
|
||||||
|
|
||||||
// NeverAngularFakeInspector is an inspector that always returns `false, nil`
|
|
||||||
NeverAngularFakeInspector = &FakeInspector{ |
|
||||||
InspectFunc: func(p *plugins.Plugin) (bool, error) { |
|
||||||
return false, nil |
|
||||||
}, |
|
||||||
} |
|
||||||
) |
|
||||||
@ -1,62 +0,0 @@ |
|||||||
package angulardetector |
|
||||||
|
|
||||||
import ( |
|
||||||
"fmt" |
|
||||||
"io" |
|
||||||
"regexp" |
|
||||||
|
|
||||||
"github.com/grafana/grafana/pkg/plugins" |
|
||||||
) |
|
||||||
|
|
||||||
// defaultDetectors contains all the detectors to detect Angular plugins.
|
|
||||||
// They are executed in the specified order.
|
|
||||||
var defaultDetectors = []detector{ |
|
||||||
&containsBytesDetector{pattern: []byte("PanelCtrl")}, |
|
||||||
&containsBytesDetector{pattern: []byte("QueryCtrl")}, |
|
||||||
&containsBytesDetector{pattern: []byte("app/plugins/sdk")}, |
|
||||||
&containsBytesDetector{pattern: []byte("angular.isNumber(")}, |
|
||||||
&containsBytesDetector{pattern: []byte("editor.html")}, |
|
||||||
&containsBytesDetector{pattern: []byte("ctrl.annotation")}, |
|
||||||
&containsBytesDetector{pattern: []byte("getLegacyAngularInjector")}, |
|
||||||
|
|
||||||
®exDetector{regex: regexp.MustCompile(`['"](app/core/utils/promiseToDigest)|(app/plugins/.*?)|(app/core/core_module)['"]`)}, |
|
||||||
®exDetector{regex: regexp.MustCompile(`from\s+['"]grafana\/app\/`)}, |
|
||||||
®exDetector{regex: regexp.MustCompile(`System\.register\(`)}, |
|
||||||
} |
|
||||||
|
|
||||||
// PatternsListInspector matches module.js against all the specified patterns, in sequence.
|
|
||||||
type PatternsListInspector struct { |
|
||||||
detectors []detector |
|
||||||
} |
|
||||||
|
|
||||||
// NewDefaultPatternsListInspector returns a new *PatternsListInspector using defaultDetectors as detectors.
|
|
||||||
func NewDefaultPatternsListInspector() *PatternsListInspector { |
|
||||||
return &PatternsListInspector{detectors: defaultDetectors} |
|
||||||
} |
|
||||||
|
|
||||||
func ProvideService() Inspector { |
|
||||||
return NewDefaultPatternsListInspector() |
|
||||||
} |
|
||||||
|
|
||||||
func (i *PatternsListInspector) Inspect(p *plugins.Plugin) (isAngular bool, err error) { |
|
||||||
f, err := p.FS.Open("module.js") |
|
||||||
if err != nil { |
|
||||||
return false, fmt.Errorf("open module.js: %w", err) |
|
||||||
} |
|
||||||
defer func() { |
|
||||||
if closeErr := f.Close(); closeErr != nil && err == nil { |
|
||||||
err = fmt.Errorf("close module.js: %w", closeErr) |
|
||||||
} |
|
||||||
}() |
|
||||||
b, err := io.ReadAll(f) |
|
||||||
if err != nil { |
|
||||||
return false, fmt.Errorf("module.js readall: %w", err) |
|
||||||
} |
|
||||||
for _, d := range i.detectors { |
|
||||||
if d.Detect(b) { |
|
||||||
isAngular = true |
|
||||||
break |
|
||||||
} |
|
||||||
} |
|
||||||
return |
|
||||||
} |
|
||||||
Loading…
Reference in new issue