services.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. package services
  2. import (
  3. "encoding/json"
  4. "errors"
  5. "fmt"
  6. "path"
  7. "github.com/franela/goreq"
  8. "github.com/grafana/grafana/pkg/cmd/grafana-cli/logger"
  9. m "github.com/grafana/grafana/pkg/cmd/grafana-cli/models"
  10. )
  11. var IoHelper m.IoUtil = IoUtilImp{}
  12. func ListAllPlugins(repoUrl string) (m.PluginRepo, error) {
  13. fullUrl := repoUrl + "/repo"
  14. res, err := goreq.Request{Uri: fullUrl, MaxRedirects: 3}.Do()
  15. if err != nil {
  16. return m.PluginRepo{}, err
  17. }
  18. if res.StatusCode != 200 {
  19. return m.PluginRepo{}, fmt.Errorf("Could not access %s statuscode %v", fullUrl, res.StatusCode)
  20. }
  21. var resp m.PluginRepo
  22. err = res.Body.FromJsonTo(&resp)
  23. if err != nil {
  24. return m.PluginRepo{}, errors.New("Could not load plugin data")
  25. }
  26. return resp, nil
  27. }
  28. func ReadPlugin(pluginDir, pluginName string) (m.InstalledPlugin, error) {
  29. distPluginDataPath := path.Join(pluginDir, pluginName, "dist", "plugin.json")
  30. var data []byte
  31. var err error
  32. data, err = IoHelper.ReadFile(distPluginDataPath)
  33. if err != nil {
  34. pluginDataPath := path.Join(pluginDir, pluginName, "plugin.json")
  35. data, err = IoHelper.ReadFile(pluginDataPath)
  36. if err != nil {
  37. return m.InstalledPlugin{}, errors.New("Could not find dist/plugin.json or plugin.json on " + pluginName + " in " + pluginDir)
  38. }
  39. }
  40. res := m.InstalledPlugin{}
  41. json.Unmarshal(data, &res)
  42. if res.Info.Version == "" {
  43. res.Info.Version = "0.0.0"
  44. }
  45. if res.Id == "" {
  46. return m.InstalledPlugin{}, errors.New("could not find plugin " + pluginName + " in " + pluginDir)
  47. }
  48. return res, nil
  49. }
  50. func GetLocalPlugins(pluginDir string) []m.InstalledPlugin {
  51. result := make([]m.InstalledPlugin, 0)
  52. files, _ := IoHelper.ReadDir(pluginDir)
  53. for _, f := range files {
  54. res, err := ReadPlugin(pluginDir, f.Name())
  55. if err == nil {
  56. result = append(result, res)
  57. }
  58. }
  59. return result
  60. }
  61. func RemoveInstalledPlugin(pluginPath, id string) error {
  62. logger.Infof("Removing plugin: %v\n", id)
  63. return IoHelper.RemoveAll(path.Join(pluginPath, id))
  64. }
  65. func GetPlugin(pluginId, repoUrl string) (m.Plugin, error) {
  66. fullUrl := repoUrl + "/repo/" + pluginId
  67. res, err := goreq.Request{Uri: fullUrl, MaxRedirects: 3}.Do()
  68. if err != nil {
  69. return m.Plugin{}, err
  70. }
  71. if res.StatusCode != 200 {
  72. return m.Plugin{}, fmt.Errorf("Could not access %s statuscode %v", fullUrl, res.StatusCode)
  73. }
  74. var resp m.Plugin
  75. err = res.Body.FromJsonTo(&resp)
  76. if err != nil {
  77. return m.Plugin{}, errors.New("Could not load plugin data")
  78. }
  79. return resp, nil
  80. }