upgrade_all_command.go 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. package commands
  2. import (
  3. "github.com/grafana/grafana/pkg/cmd/grafana-cli/log"
  4. m "github.com/grafana/grafana/pkg/cmd/grafana-cli/models"
  5. s "github.com/grafana/grafana/pkg/cmd/grafana-cli/services"
  6. "github.com/hashicorp/go-version"
  7. )
  8. func ShouldUpgrade(installed string, remote m.Plugin) bool {
  9. installedVersion, err1 := version.NewVersion(installed)
  10. if err1 != nil {
  11. return false
  12. }
  13. for _, v := range remote.Versions {
  14. remoteVersion, err2 := version.NewVersion(v.Version)
  15. if err2 == nil {
  16. if installedVersion.LessThan(remoteVersion) {
  17. return true
  18. }
  19. }
  20. }
  21. return false
  22. }
  23. func upgradeAllCommand(c CommandLine) error {
  24. pluginDir := c.GlobalString("path")
  25. localPlugins := s.GetLocalPlugins(pluginDir)
  26. remotePlugins, err := s.ListAllPlugins(c.GlobalString("repo"))
  27. if err != nil {
  28. return err
  29. }
  30. pluginsToUpgrade := make([]m.InstalledPlugin, 0)
  31. for _, localPlugin := range localPlugins {
  32. for _, remotePlugin := range remotePlugins.Plugins {
  33. if localPlugin.Id == remotePlugin.Id {
  34. if ShouldUpgrade(localPlugin.Info.Version, remotePlugin) {
  35. pluginsToUpgrade = append(pluginsToUpgrade, localPlugin)
  36. }
  37. }
  38. }
  39. }
  40. for _, p := range pluginsToUpgrade {
  41. log.Infof("Upgrading %v \n", p.Id)
  42. s.RemoveInstalledPlugin(pluginDir, p.Id)
  43. InstallPlugin(p.Id, pluginDir, "", c.GlobalString("repo"))
  44. }
  45. return nil
  46. }