forked from grafana/grafana
-
Notifications
You must be signed in to change notification settings - Fork 0
/
upgrade_all_command.go
68 lines (52 loc) · 1.39 KB
/
upgrade_all_command.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
package commands
import (
"github.com/grafana/grafana/pkg/cmd/grafana-cli/logger"
m "github.com/grafana/grafana/pkg/cmd/grafana-cli/models"
s "github.com/grafana/grafana/pkg/cmd/grafana-cli/services"
"github.com/hashicorp/go-version"
)
func ShouldUpgrade(installed string, remote m.Plugin) bool {
installedVersion, err1 := version.NewVersion(installed)
if err1 != nil {
return false
}
for _, v := range remote.Versions {
remoteVersion, err2 := version.NewVersion(v.Version)
if err2 == nil {
if installedVersion.LessThan(remoteVersion) {
return true
}
}
}
return false
}
func upgradeAllCommand(c CommandLine) error {
pluginsDir := c.PluginDirectory()
localPlugins := s.GetLocalPlugins(pluginsDir)
remotePlugins, err := s.ListAllPlugins(c.GlobalString("repo"))
if err != nil {
return err
}
pluginsToUpgrade := make([]m.InstalledPlugin, 0)
for _, localPlugin := range localPlugins {
for _, remotePlugin := range remotePlugins.Plugins {
if localPlugin.Id == remotePlugin.Id {
if ShouldUpgrade(localPlugin.Info.Version, remotePlugin) {
pluginsToUpgrade = append(pluginsToUpgrade, localPlugin)
}
}
}
}
for _, p := range pluginsToUpgrade {
logger.Infof("Updating %v \n", p.Id)
err := s.RemoveInstalledPlugin(pluginsDir, p.Id)
if err != nil {
return err
}
err = InstallPlugin(p.Id, "", c)
if err != nil {
return err
}
}
return nil
}