Plugins: Tidy up CLI code (#67813)
* more tidying * move some things around * more tidying * fix linter
This commit is contained in:
@@ -127,7 +127,7 @@ func osAndArchString() string {
|
||||
return osString + "-" + arch
|
||||
}
|
||||
|
||||
func supportsCurrentArch(version *models.Version) bool {
|
||||
func supportsCurrentArch(version models.Version) bool {
|
||||
if version.Arch == nil {
|
||||
return true
|
||||
}
|
||||
@@ -139,10 +139,10 @@ func supportsCurrentArch(version *models.Version) bool {
|
||||
return false
|
||||
}
|
||||
|
||||
func latestSupportedVersion(plugin *models.Plugin) *models.Version {
|
||||
func latestSupportedVersion(plugin models.Plugin) *models.Version {
|
||||
for _, v := range plugin.Versions {
|
||||
ver := v
|
||||
if supportsCurrentArch(&ver) {
|
||||
if supportsCurrentArch(ver) {
|
||||
return &ver
|
||||
}
|
||||
}
|
||||
|
||||
@@ -15,11 +15,10 @@ func listRemoteCommand(c utils.CommandLine) error {
|
||||
}
|
||||
|
||||
for _, p := range plugin.Plugins {
|
||||
plugin := p
|
||||
if len(plugin.Versions) > 0 {
|
||||
ver := latestSupportedVersion(&plugin)
|
||||
if len(p.Versions) > 0 {
|
||||
ver := latestSupportedVersion(p)
|
||||
if ver != nil {
|
||||
logger.Infof("id: %v version: %s\n", plugin.ID, ver.Version)
|
||||
logger.Infof("id: %v version: %s\n", p.ID, ver.Version)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -24,7 +24,7 @@ func listVersionsCommand(c utils.CommandLine) error {
|
||||
|
||||
pluginToList := c.Args().First()
|
||||
|
||||
plugin, err := services.GetPlugin(pluginToList, c.String("repo"))
|
||||
plugin, err := services.GetPluginInfoFromRepo(pluginToList, c.String("repo"))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -6,13 +6,10 @@ import (
|
||||
"github.com/fatih/color"
|
||||
|
||||
"github.com/grafana/grafana/pkg/cmd/grafana-cli/logger"
|
||||
"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"
|
||||
)
|
||||
|
||||
var ls_getPlugins func(path string) []models.InstalledPlugin = services.GetLocalPlugins
|
||||
|
||||
var (
|
||||
errMissingPathFlag = errors.New("missing path flag")
|
||||
errNotDirectory = errors.New("plugin path is not a directory")
|
||||
@@ -41,7 +38,7 @@ func lsCommand(c utils.CommandLine) error {
|
||||
return err
|
||||
}
|
||||
|
||||
plugins := ls_getPlugins(pluginDir)
|
||||
plugins := services.GetLocalPlugins(pluginDir)
|
||||
|
||||
if len(plugins) > 0 {
|
||||
logger.Info("installed plugins:\n")
|
||||
@@ -50,7 +47,8 @@ func lsCommand(c utils.CommandLine) error {
|
||||
}
|
||||
|
||||
for _, plugin := range plugins {
|
||||
logger.Infof("%s %s %s\n", plugin.ID, color.YellowString("@"), plugin.Info.Version)
|
||||
logger.Infof("%s %s %s\n", plugin.Primary.JSONData.ID,
|
||||
color.YellowString("@"), plugin.Primary.JSONData.Info.Version)
|
||||
}
|
||||
|
||||
return nil
|
||||
|
||||
@@ -9,10 +9,15 @@ import (
|
||||
"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"
|
||||
"github.com/grafana/grafana/pkg/plugins"
|
||||
)
|
||||
|
||||
func shouldUpgrade(installed string, remote *models.Plugin) bool {
|
||||
installedVersion, err := version.NewVersion(installed)
|
||||
func shouldUpgrade(installed plugins.FoundPlugin, remote models.Plugin) bool {
|
||||
installedVer := installed.JSONData.Info.Version
|
||||
if installedVer == "" {
|
||||
installedVer = "0.0.0"
|
||||
}
|
||||
installedVersion, err := version.NewVersion(installedVer)
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
@@ -35,30 +40,30 @@ func upgradeAllCommand(c utils.CommandLine) error {
|
||||
return err
|
||||
}
|
||||
|
||||
pluginsToUpgrade := make([]models.InstalledPlugin, 0)
|
||||
pluginsToUpgrade := make([]plugins.FoundPlugin, 0)
|
||||
|
||||
for _, localPlugin := range localPlugins {
|
||||
for _, p := range remotePlugins.Plugins {
|
||||
remotePlugin := p
|
||||
if localPlugin.ID != remotePlugin.ID {
|
||||
if localPlugin.Primary.JSONData.ID != remotePlugin.ID {
|
||||
continue
|
||||
}
|
||||
if shouldUpgrade(localPlugin.Info.Version, &remotePlugin) {
|
||||
pluginsToUpgrade = append(pluginsToUpgrade, localPlugin)
|
||||
if shouldUpgrade(localPlugin.Primary, remotePlugin) {
|
||||
pluginsToUpgrade = append(pluginsToUpgrade, localPlugin.Primary)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
ctx := context.Background()
|
||||
for _, p := range pluginsToUpgrade {
|
||||
logger.Infof("Updating %v \n", p.ID)
|
||||
logger.Infof("Updating %v \n", p.JSONData.ID)
|
||||
|
||||
err = uninstallPlugin(ctx, p.ID, c)
|
||||
err = uninstallPlugin(ctx, p.JSONData.ID, c)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
err = installPlugin(ctx, p.ID, "", c)
|
||||
err = installPlugin(ctx, p.JSONData.ID, "", c)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -4,9 +4,10 @@ import (
|
||||
"fmt"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
|
||||
"github.com/grafana/grafana/pkg/cmd/grafana-cli/models"
|
||||
"github.com/grafana/grafana/pkg/plugins"
|
||||
)
|
||||
|
||||
func TestVersionComparison(t *testing.T) {
|
||||
@@ -16,15 +17,23 @@ func TestVersionComparison(t *testing.T) {
|
||||
{Version: "2.0.0"},
|
||||
}
|
||||
|
||||
upgradeablePlugins := map[string]models.Plugin{
|
||||
"0.0.0": {Versions: versions},
|
||||
"1.0.0": {Versions: versions},
|
||||
upgradeablePlugins := []struct {
|
||||
have plugins.FoundPlugin
|
||||
requested models.Plugin
|
||||
}{
|
||||
{
|
||||
have: plugins.FoundPlugin{JSONData: plugins.JSONData{Info: plugins.Info{Version: "0.0.0"}}},
|
||||
requested: models.Plugin{Versions: versions},
|
||||
},
|
||||
{
|
||||
have: plugins.FoundPlugin{JSONData: plugins.JSONData{Info: plugins.Info{Version: "1.0.0"}}},
|
||||
requested: models.Plugin{Versions: versions},
|
||||
},
|
||||
}
|
||||
|
||||
for k, v := range upgradeablePlugins {
|
||||
val := v
|
||||
t.Run(fmt.Sprintf("for %s should be true", k), func(t *testing.T) {
|
||||
assert.True(t, shouldUpgrade(k, &val))
|
||||
for _, v := range upgradeablePlugins {
|
||||
t.Run(fmt.Sprintf("for %s should be true", v.have.JSONData.Info.Version), func(t *testing.T) {
|
||||
require.True(t, shouldUpgrade(v.have, v.requested))
|
||||
})
|
||||
}
|
||||
})
|
||||
@@ -35,15 +44,23 @@ func TestVersionComparison(t *testing.T) {
|
||||
{Version: "2.0.0"},
|
||||
}
|
||||
|
||||
shouldNotUpgrade := map[string]models.Plugin{
|
||||
"2.0.0": {Versions: versions},
|
||||
"6.0.0": {Versions: versions},
|
||||
shouldNotUpgrade := []struct {
|
||||
have plugins.FoundPlugin
|
||||
requested models.Plugin
|
||||
}{
|
||||
{
|
||||
have: plugins.FoundPlugin{JSONData: plugins.JSONData{Info: plugins.Info{Version: "2.0.0"}}},
|
||||
requested: models.Plugin{Versions: versions},
|
||||
},
|
||||
{
|
||||
have: plugins.FoundPlugin{JSONData: plugins.JSONData{Info: plugins.Info{Version: "6.0.0"}}},
|
||||
requested: models.Plugin{Versions: versions},
|
||||
},
|
||||
}
|
||||
|
||||
for k, v := range shouldNotUpgrade {
|
||||
val := v
|
||||
t.Run(fmt.Sprintf("for %s should be false", k), func(t *testing.T) {
|
||||
assert.False(t, shouldUpgrade(k, &val))
|
||||
for _, v := range shouldNotUpgrade {
|
||||
t.Run(fmt.Sprintf("for %s should be false", v.have.JSONData.Info.Version), func(t *testing.T) {
|
||||
require.False(t, shouldUpgrade(v.have, v.requested))
|
||||
})
|
||||
}
|
||||
})
|
||||
|
||||
@@ -16,17 +16,17 @@ func upgradeCommand(c utils.CommandLine) error {
|
||||
pluginsDir := c.PluginDirectory()
|
||||
pluginID := c.Args().First()
|
||||
|
||||
localPlugin, err := services.ReadPlugin(pluginsDir, pluginID)
|
||||
localPlugin, err := services.GetLocalPlugin(pluginsDir, pluginID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
plugin, err := services.GetPlugin(pluginID, c.PluginRepoURL())
|
||||
plugin, err := services.GetPluginInfoFromRepo(pluginID, c.PluginRepoURL())
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if shouldUpgrade(localPlugin.Info.Version, &plugin) {
|
||||
if shouldUpgrade(localPlugin, plugin) {
|
||||
if err = uninstallPlugin(ctx, pluginID, c); err != nil {
|
||||
return fmt.Errorf("failed to remove plugin '%s': %w", pluginID, err)
|
||||
}
|
||||
|
||||
@@ -14,7 +14,7 @@ import (
|
||||
"github.com/grafana/grafana/pkg/cmd/grafana-cli/models"
|
||||
)
|
||||
|
||||
func GetPlugin(pluginId, repoUrl string) (models.Plugin, error) {
|
||||
func GetPluginInfoFromRepo(pluginId, repoUrl string) (models.Plugin, error) {
|
||||
logger.Debugf("getting plugin metadata from: %v pluginId: %v \n", repoUrl, pluginId)
|
||||
body, err := sendRequestGetBytes(HttpClient, repoUrl, "repo", pluginId)
|
||||
if err != nil {
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
package services
|
||||
|
||||
import (
|
||||
"context"
|
||||
"crypto/tls"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"net"
|
||||
@@ -12,6 +12,10 @@ import (
|
||||
|
||||
"github.com/grafana/grafana/pkg/cmd/grafana-cli/logger"
|
||||
"github.com/grafana/grafana/pkg/cmd/grafana-cli/models"
|
||||
"github.com/grafana/grafana/pkg/plugins"
|
||||
"github.com/grafana/grafana/pkg/plugins/config"
|
||||
"github.com/grafana/grafana/pkg/plugins/manager/loader/finder"
|
||||
"github.com/grafana/grafana/pkg/plugins/manager/sources"
|
||||
)
|
||||
|
||||
var (
|
||||
@@ -62,43 +66,25 @@ func makeHttpClient(skipTLSVerify bool, timeout time.Duration) http.Client {
|
||||
}
|
||||
}
|
||||
|
||||
func ReadPlugin(pluginDir, pluginName string) (models.InstalledPlugin, error) {
|
||||
distPluginDataPath := filepath.Join(pluginDir, pluginName, "dist", "plugin.json")
|
||||
func GetLocalPlugin(pluginDir, pluginID string) (plugins.FoundPlugin, error) {
|
||||
pluginPath := filepath.Join(pluginDir, pluginID)
|
||||
|
||||
data, err := IoHelper.ReadFile(distPluginDataPath)
|
||||
ps := GetLocalPlugins(pluginPath)
|
||||
if len(ps) == 0 {
|
||||
return plugins.FoundPlugin{}, errors.New("could not find plugin " + pluginID + " in " + pluginDir)
|
||||
}
|
||||
|
||||
return ps[0].Primary, nil
|
||||
}
|
||||
|
||||
func GetLocalPlugins(pluginDir string) []*plugins.FoundBundle {
|
||||
f := finder.NewLocalFinder(&config.Cfg{})
|
||||
|
||||
res, err := f.Find(context.Background(), sources.NewLocalSource(plugins.External, []string{pluginDir}))
|
||||
if err != nil {
|
||||
pluginDataPath := filepath.Join(pluginDir, pluginName, "plugin.json")
|
||||
data, err = IoHelper.ReadFile(pluginDataPath)
|
||||
if err != nil {
|
||||
return models.InstalledPlugin{}, errors.New("Could not find dist/plugin.json or plugin.json for " + pluginName + " in " + pluginDir)
|
||||
}
|
||||
logger.Error("Could not get local plugins", err)
|
||||
return make([]*plugins.FoundBundle, 0)
|
||||
}
|
||||
|
||||
res := models.InstalledPlugin{}
|
||||
if err := json.Unmarshal(data, &res); err != nil {
|
||||
return res, err
|
||||
}
|
||||
|
||||
if res.Info.Version == "" {
|
||||
res.Info.Version = "0.0.0"
|
||||
}
|
||||
|
||||
if res.ID == "" {
|
||||
return models.InstalledPlugin{}, errors.New("could not find plugin " + pluginName + " in " + pluginDir)
|
||||
}
|
||||
|
||||
return res, nil
|
||||
}
|
||||
|
||||
func GetLocalPlugins(pluginDir string) []models.InstalledPlugin {
|
||||
result := make([]models.InstalledPlugin, 0)
|
||||
files, _ := IoHelper.ReadDir(pluginDir)
|
||||
for _, f := range files {
|
||||
res, err := ReadPlugin(pluginDir, f.Name())
|
||||
if err == nil {
|
||||
result = append(result, res)
|
||||
}
|
||||
}
|
||||
|
||||
return result
|
||||
return res
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user