forked from grafana/grafana
-
Notifications
You must be signed in to change notification settings - Fork 0
/
plugins.go
98 lines (77 loc) · 1.78 KB
/
plugins.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
package plugins
import (
"encoding/json"
"errors"
"os"
"path"
"path/filepath"
"github.com/grafana/grafana/pkg/log"
"github.com/grafana/grafana/pkg/setting"
)
type PluginMeta struct {
Type string `json:"type"`
Name string `json:"name"`
}
var (
DataSources map[string]interface{}
)
type PluginScanner struct {
pluginPath string
errors []error
}
func Init() {
scan(path.Join(setting.StaticRootPath, "app/plugins"))
}
func scan(pluginDir string) error {
DataSources = make(map[string]interface{})
scanner := &PluginScanner{
pluginPath: pluginDir,
}
if err := filepath.Walk(pluginDir, scanner.walker); err != nil {
return err
}
if len(scanner.errors) > 0 {
return errors.New("Some plugins failed to load")
}
return nil
}
func (scanner *PluginScanner) walker(path string, f os.FileInfo, err error) error {
if err != nil {
return err
}
if f.IsDir() {
return nil
}
if f.Name() == "plugin.json" {
err := scanner.loadPluginJson(path)
if err != nil {
log.Error(3, "Failed to load plugin json file: %v, err: %v", path, err)
scanner.errors = append(scanner.errors, err)
}
}
return nil
}
func (scanner *PluginScanner) loadPluginJson(path string) error {
reader, err := os.Open(path)
if err != nil {
return err
}
defer reader.Close()
jsonParser := json.NewDecoder(reader)
pluginJson := make(map[string]interface{})
if err := jsonParser.Decode(&pluginJson); err != nil {
return err
}
pluginType, exists := pluginJson["pluginType"]
if !exists {
return errors.New("Did not find pluginType property in plugin.json")
}
if pluginType == "datasource" {
datasourceType, exists := pluginJson["type"]
if !exists {
return errors.New("Did not find type property in plugin.json")
}
DataSources[datasourceType.(string)] = pluginJson
}
return nil
}