From 7a8b3c419bf2cd45319a3d5fd5b45f7eae4253cd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Torkel=20=C3=96degaard?= Date: Fri, 8 Jan 2016 20:57:58 +0100 Subject: [PATCH] feat(apps): lots of progress --- pkg/api/app_plugin.go | 1 + pkg/api/dtos/app_plugin.go | 3 ++ pkg/plugins/models.go | 10 ++--- pkg/plugins/plugins.go | 35 ++++++++++++++- pkg/plugins/plugins_test.go | 13 ++++++ public/app/features/apps/partials/edit.html | 13 +++++- tests/app-plugin-json/plugin.json | 50 +++++++++++++++++++++ 7 files changed, 117 insertions(+), 8 deletions(-) create mode 100644 tests/app-plugin-json/plugin.json diff --git a/pkg/api/app_plugin.go b/pkg/api/app_plugin.go index 860334e362e..0ad81827246 100644 --- a/pkg/api/app_plugin.go +++ b/pkg/api/app_plugin.go @@ -22,6 +22,7 @@ func GetAppPlugins(c *middleware.Context) Response { Enabled: app.Enabled, Pinned: app.Pinned, Module: app.Module, + Info: app.Info, } } diff --git a/pkg/api/dtos/app_plugin.go b/pkg/api/dtos/app_plugin.go index 029989008b5..7213c224cf8 100644 --- a/pkg/api/dtos/app_plugin.go +++ b/pkg/api/dtos/app_plugin.go @@ -1,10 +1,13 @@ package dtos +import "github.com/grafana/grafana/pkg/plugins" + type AppPlugin struct { Name string `json:"name"` Type string `json:"type"` Enabled bool `json:"enabled"` Pinned bool `json:"pinned"` Module string `json:"module"` + Info *plugins.PluginInfo `json:"info"` JsonData map[string]interface{} `json:"jsonData"` } diff --git a/pkg/plugins/models.go b/pkg/plugins/models.go index 0db825b56cf..7c0bfcafc50 100644 --- a/pkg/plugins/models.go +++ b/pkg/plugins/models.go @@ -5,13 +5,13 @@ import ( ) type PluginInfo struct { - Author PluginAuthor `json:"author"` - Description string `json:"description"` - Homepage string `json:"homepage"` - Logos PluginLogos `json:"logos"` + Author PluginInfoLink `json:"author"` + Description string `json:"description"` + Links []PluginInfoLink `json:"links"` + Logos PluginLogos `json:"logos"` } -type PluginAuthor struct { +type PluginInfoLink struct { Name string `json:"name"` Url string `json:"url"` } diff --git a/pkg/plugins/plugins.go b/pkg/plugins/plugins.go index 10c511a966b..a1ae0ff8dd6 100644 --- a/pkg/plugins/plugins.go +++ b/pkg/plugins/plugins.go @@ -1,12 +1,15 @@ package plugins import ( + "bytes" "encoding/json" "errors" + "io" "os" "path" "path/filepath" "strings" + "text/template" "github.com/grafana/grafana/pkg/log" "github.com/grafana/grafana/pkg/models" @@ -118,6 +121,28 @@ func addPublicContent(public *PublicContent, currentDir string) { } } +func interpolatePluginJson(reader io.Reader) (io.Reader, error) { + buf := new(bytes.Buffer) + buf.ReadFrom(reader) + jsonStr := buf.String() // + + tmpl, err := template.New("json").Parse(jsonStr) + if err != nil { + return nil, err + } + + data := map[string]interface{}{ + "PluginPublicRoot": "HAHAHA", + } + + var resultBuffer bytes.Buffer + if err := tmpl.ExecuteTemplate(&resultBuffer, "json", data); err != nil { + return nil, err + } + + return bytes.NewReader(resultBuffer.Bytes()), nil +} + func (scanner *PluginScanner) loadPluginJson(pluginJsonFilePath string) error { currentDir := filepath.Dir(pluginJsonFilePath) reader, err := os.Open(pluginJsonFilePath) @@ -128,7 +153,6 @@ func (scanner *PluginScanner) loadPluginJson(pluginJsonFilePath string) error { defer reader.Close() jsonParser := json.NewDecoder(reader) - pluginJson := make(map[string]interface{}) if err := jsonParser.Decode(&pluginJson); err != nil { return err @@ -139,9 +163,16 @@ func (scanner *PluginScanner) loadPluginJson(pluginJsonFilePath string) error { return errors.New("Did not find pluginType property in plugin.json") } + reader.Seek(0, 0) + + if newReader, err := interpolatePluginJson(reader); err != nil { + return err + } else { + jsonParser = json.NewDecoder(newReader) + } + if pluginType == "datasource" { p := DataSourcePlugin{} - reader.Seek(0, 0) if err := jsonParser.Decode(&p); err != nil { return err } diff --git a/pkg/plugins/plugins_test.go b/pkg/plugins/plugins_test.go index bbeac4bba81..812b8ad19f2 100644 --- a/pkg/plugins/plugins_test.go +++ b/pkg/plugins/plugins_test.go @@ -18,5 +18,18 @@ func TestPluginScans(t *testing.T) { So(err, ShouldBeNil) So(len(DataSources), ShouldBeGreaterThan, 1) + So(len(Panels), ShouldBeGreaterThan, 1) }) + + Convey("When reading app plugin definition", t, func() { + setting.Cfg = ini.Empty() + sec, _ := setting.Cfg.NewSection("plugin.app-test") + sec.NewKey("path", "../../tests/app-plugin-json") + err := Init() + + So(err, ShouldBeNil) + So(len(Apps), ShouldBeGreaterThan, 0) + So(Apps["app-test"].Info.Logos.Large, ShouldEqual, "plugins/app-exampl/img/logo_large.png") + }) + } diff --git a/public/app/features/apps/partials/edit.html b/public/app/features/apps/partials/edit.html index 4335a94ead8..b770914c802 100644 --- a/public/app/features/apps/partials/edit.html +++ b/public/app/features/apps/partials/edit.html @@ -6,8 +6,20 @@
+
+ + + +

{{ctrl.appModel.name}}

+ + {{ctrl.appModel.info.description}} +
@@ -24,6 +36,5 @@ -
diff --git a/tests/app-plugin-json/plugin.json b/tests/app-plugin-json/plugin.json new file mode 100644 index 00000000000..34c60f84f52 --- /dev/null +++ b/tests/app-plugin-json/plugin.json @@ -0,0 +1,50 @@ +{ + "pluginType": "app", + "name": "App Example", + "type": "app-test", + + "plugins": [], + + "css": { + "light": "plugin.dark.css", + "dark": "plugin.light.css" + }, + + "module": "app", + + "pages": [ + {"name": "Example1", "url": "/app-example", "reqRole": "Editor"} + ], + + "public": { + "urlFragment": "app-example", + "path": "./public" + }, + + "info": { + "description": "Example Grafana App", + "author": { + "name": "Raintank Inc.", + "url": "http://raintank.io" + }, + "keywords": ["example"], + "logos": { + "small": "{{.PluginPublicRoot}}/img/logo_small.png", + "large": "{{.PluginPublicRoot}}/logo_large.png" + }, + "links": [ + {"name": "Project site", "url": "http://project.com"}, + {"name": "License & Terms", "url": "http://license.com"} + ], + "version": "1.0.0", + "updated": "2015-02-10" + }, + + "dependencies": { + "grafanaVersion": "2.6.x", + "plugins": [ + {"type": "datasource", "id": "graphite", "name": "Graphite", "version": "1.0.0"}, + {"type": "panel", "id": "graph", "name": "Graph", "version": "1.0.0"} + ] + } +}