services.go 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. package services
  2. import (
  3. "encoding/json"
  4. "errors"
  5. "github.com/franela/goreq"
  6. m "github.com/grafana/grafana-cli/pkg/models"
  7. "path"
  8. )
  9. var IoHelper m.IoUtil = IoUtilImp{}
  10. func ListAllPlugins() (m.PluginRepo, error) {
  11. res, _ := goreq.Request{Uri: "https://raw.githubusercontent.com/grafana/grafana-plugin-repository/master/repo.json"}.Do()
  12. var resp m.PluginRepo
  13. err := res.Body.FromJsonTo(&resp)
  14. if err != nil {
  15. return m.PluginRepo{}, errors.New("Could not load plugin data")
  16. }
  17. return resp, nil
  18. }
  19. func ReadPlugin(pluginDir, pluginName string) m.InstalledPlugin {
  20. pluginDataPath := path.Join(pluginDir, pluginName, "plugin.json")
  21. pluginData, _ := IoHelper.ReadFile(pluginDataPath)
  22. res := m.InstalledPlugin{}
  23. json.Unmarshal(pluginData, &res)
  24. if res.Info.Version == "" {
  25. res.Info.Version = "0.0.0"
  26. }
  27. if res.Id == "" {
  28. res.Id = res.Name
  29. }
  30. return res
  31. }
  32. func GetLocalPlugins(pluginDir string) []m.InstalledPlugin {
  33. result := make([]m.InstalledPlugin, 0)
  34. files, _ := IoHelper.ReadDir(pluginDir)
  35. for _, f := range files {
  36. res := ReadPlugin(pluginDir, f.Name())
  37. result = append(result, res)
  38. }
  39. return result
  40. }
  41. func RemoveInstalledPlugin(pluginPath, id string) error {
  42. return IoHelper.RemoveAll(path.Join(pluginPath, id))
  43. }
  44. func GetPlugin(id string) (m.Plugin, error) {
  45. resp, err := ListAllPlugins()
  46. if err != nil {
  47. }
  48. for _, i := range resp.Plugins {
  49. if i.Id == id {
  50. return i, nil
  51. }
  52. }
  53. return m.Plugin{}, errors.New("could not find plugin named \"" + id + "\"")
  54. }