grafana/pkg/cmd/grafana-cli/commands/upgrade_all_command.go

65 lines
1.5 KiB
Go
Raw Normal View History

2016-02-15 07:09:34 -06:00
package commands
import (
"github.com/grafana/grafana/pkg/cmd/grafana-cli/logger"
2020-03-04 06:18:13 -06:00
"github.com/grafana/grafana/pkg/cmd/grafana-cli/models"
"github.com/grafana/grafana/pkg/cmd/grafana-cli/services"
"github.com/grafana/grafana/pkg/cmd/grafana-cli/utils"
2016-02-15 07:09:34 -06:00
"github.com/hashicorp/go-version"
)
2020-03-04 06:18:13 -06:00
func shouldUpgrade(installed string, remote *models.Plugin) bool {
installedVersion, err := version.NewVersion(installed)
if err != nil {
2016-02-15 07:09:34 -06:00
return false
}
latest := latestSupportedVersion(remote)
latestVersion, err := version.NewVersion(latest.Version)
if err != nil {
return false
2016-02-15 07:09:34 -06:00
}
return installedVersion.LessThan(latestVersion)
2016-02-15 07:09:34 -06:00
}
func (cmd Command) upgradeAllCommand(c utils.CommandLine) error {
pluginsDir := c.PluginDirectory()
2016-02-15 07:09:34 -06:00
2020-03-04 06:18:13 -06:00
localPlugins := services.GetLocalPlugins(pluginsDir)
2016-02-15 07:09:34 -06:00
remotePlugins, err := cmd.Client.ListAllPlugins(c.String("repo"))
2016-02-15 07:09:34 -06:00
if err != nil {
return err
}
2020-03-04 06:18:13 -06:00
pluginsToUpgrade := make([]models.InstalledPlugin, 0)
2016-02-15 07:09:34 -06:00
for _, localPlugin := range localPlugins {
for _, p := range remotePlugins.Plugins {
remotePlugin := p
if localPlugin.ID != remotePlugin.ID {
continue
}
if shouldUpgrade(localPlugin.Info.Version, &remotePlugin) {
pluginsToUpgrade = append(pluginsToUpgrade, localPlugin)
2016-02-15 07:09:34 -06:00
}
}
}
for _, p := range pluginsToUpgrade {
logger.Infof("Updating %v \n", p.ID)
2016-02-15 07:09:34 -06:00
err := services.RemoveInstalledPlugin(pluginsDir, p.ID)
if err != nil {
return err
}
err = InstallPlugin(p.ID, "", c, cmd.Client)
if err != nil {
return err
}
2016-02-15 07:09:34 -06:00
}
return nil
}