Plugins: Remove dead CLI code and use pkg/plugins for uninstall process (#67711)
* remove dead code and use pkg/plugins for uninstall process * fix linter
This commit is contained in:
@@ -11,7 +11,7 @@ import (
|
||||
"github.com/grafana/grafana/pkg/cmd/grafana-cli/utils"
|
||||
)
|
||||
|
||||
// RunCLI is the entrypoint for the grafana-cli command. It returns the exit code for the grafana-cli program.
|
||||
// CLICommand is the entrypoint for the grafana-cli command. It returns the exit code for the grafana-cli program.
|
||||
func CLICommand(version string) *cli.Command {
|
||||
return &cli.Command{
|
||||
Name: "cli",
|
||||
|
||||
@@ -5,6 +5,7 @@ import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"runtime"
|
||||
"strings"
|
||||
|
||||
@@ -13,6 +14,7 @@ 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"
|
||||
"github.com/grafana/grafana/pkg/plugins/repo"
|
||||
"github.com/grafana/grafana/pkg/plugins/storage"
|
||||
)
|
||||
@@ -104,6 +106,21 @@ func installPlugin(ctx context.Context, pluginID, version string, c utils.Comman
|
||||
return nil
|
||||
}
|
||||
|
||||
// uninstallPlugin removes the plugin directory
|
||||
func uninstallPlugin(_ context.Context, pluginID string, c utils.CommandLine) error {
|
||||
logger.Infof("Removing plugin: %v\n", pluginID)
|
||||
|
||||
pluginPath := filepath.Join(c.PluginDirectory(), pluginID)
|
||||
fs := plugins.NewLocalFS(pluginPath)
|
||||
|
||||
logger.Debugf("Removing directory %v\n", pluginPath)
|
||||
err := fs.Remove()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func osAndArchString() string {
|
||||
osString := strings.ToLower(runtime.GOOS)
|
||||
arch := runtime.GOARCH
|
||||
|
||||
@@ -1,31 +1,25 @@
|
||||
package commands
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"github.com/grafana/grafana/pkg/cmd/grafana-cli/services"
|
||||
"github.com/grafana/grafana/pkg/cmd/grafana-cli/utils"
|
||||
)
|
||||
|
||||
var removePlugin func(pluginPath, id string) error = services.RemoveInstalledPlugin
|
||||
|
||||
func (cmd Command) removeCommand(c utils.CommandLine) error {
|
||||
pluginPath := c.PluginDirectory()
|
||||
|
||||
plugin := c.Args().First()
|
||||
if plugin == "" {
|
||||
pluginID := c.Args().First()
|
||||
if pluginID == "" {
|
||||
return errors.New("missing plugin parameter")
|
||||
}
|
||||
|
||||
err := removePlugin(pluginPath, plugin)
|
||||
|
||||
err := uninstallPlugin(context.Background(), pluginID, c)
|
||||
if err != nil {
|
||||
if strings.Contains(err.Error(), "no such file or directory") {
|
||||
return fmt.Errorf("plugin does not exist")
|
||||
}
|
||||
|
||||
return err
|
||||
} else {
|
||||
logRestartNotice()
|
||||
|
||||
@@ -49,15 +49,16 @@ func (cmd Command) upgradeAllCommand(c utils.CommandLine) error {
|
||||
}
|
||||
}
|
||||
|
||||
ctx := context.Background()
|
||||
for _, p := range pluginsToUpgrade {
|
||||
logger.Infof("Updating %v \n", p.ID)
|
||||
|
||||
err := services.RemoveInstalledPlugin(pluginsDir, p.ID)
|
||||
err = uninstallPlugin(ctx, p.ID, c)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
err = installPlugin(context.Background(), p.ID, "", c)
|
||||
err = installPlugin(ctx, p.ID, "", c)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -12,32 +12,32 @@ import (
|
||||
)
|
||||
|
||||
func (cmd Command) upgradeCommand(c utils.CommandLine) error {
|
||||
ctx := context.Background()
|
||||
pluginsDir := c.PluginDirectory()
|
||||
pluginName := c.Args().First()
|
||||
|
||||
localPlugin, err := services.ReadPlugin(pluginsDir, pluginName)
|
||||
pluginID := c.Args().First()
|
||||
|
||||
localPlugin, err := services.ReadPlugin(pluginsDir, pluginID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
plugin, err2 := cmd.Client.GetPlugin(pluginName, c.PluginRepoURL())
|
||||
if err2 != nil {
|
||||
return err2
|
||||
plugin, err := cmd.Client.GetPlugin(pluginID, c.PluginRepoURL())
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if shouldUpgrade(localPlugin.Info.Version, &plugin) {
|
||||
if err := services.RemoveInstalledPlugin(pluginsDir, pluginName); err != nil {
|
||||
return fmt.Errorf("failed to remove plugin '%s': %w", pluginName, err)
|
||||
if err = uninstallPlugin(ctx, pluginID, c); err != nil {
|
||||
return fmt.Errorf("failed to remove plugin '%s': %w", pluginID, err)
|
||||
}
|
||||
|
||||
err := installPlugin(context.Background(), pluginName, "", c)
|
||||
err = installPlugin(ctx, pluginID, "", c)
|
||||
if err == nil {
|
||||
logRestartNotice()
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
logger.Infof("%s %s is up to date \n", color.GreenString("✔"), pluginName)
|
||||
logger.Infof("%s %s is up to date \n", color.GreenString("✔"), pluginID)
|
||||
return nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user