From 45e478182bf200bf5efee8f46a947ea4a7d3e995 Mon Sep 17 00:00:00 2001 From: Andres Martinez Gotor Date: Wed, 22 Feb 2023 09:24:13 +0100 Subject: [PATCH] grafana-cli: update plugins ls command (#63492) --- pkg/cmd/grafana-cli/commands/commands.go | 5 +---- pkg/cmd/grafana-cli/commands/install_command.go | 12 +++++++++++- pkg/cmd/grafana-cli/commands/ls_command.go | 2 ++ pkg/cmd/grafana-cli/commands/remove_command.go | 2 ++ pkg/cmd/grafana-cli/commands/upgrade_all_command.go | 4 ++++ pkg/cmd/grafana-cli/commands/upgrade_command.go | 6 +++++- 6 files changed, 25 insertions(+), 6 deletions(-) diff --git a/pkg/cmd/grafana-cli/commands/commands.go b/pkg/cmd/grafana-cli/commands/commands.go index 046c0b8b17e..c5e34990a4b 100644 --- a/pkg/cmd/grafana-cli/commands/commands.go +++ b/pkg/cmd/grafana-cli/commands/commands.go @@ -4,7 +4,6 @@ import ( "fmt" "strings" - "github.com/fatih/color" "github.com/urfave/cli/v2" "github.com/grafana/grafana/pkg/bus" @@ -99,8 +98,6 @@ func runPluginCommand(command func(commandLine utils.CommandLine) error) func(co if err := command(cmd); err != nil { return err } - - logger.Info(color.GreenString("Please restart Grafana after installing plugins. Refer to Grafana documentation for instructions if necessary.\n\n")) return nil } } @@ -139,7 +136,7 @@ var pluginCommands = []*cli.Command{ Action: runPluginCommand(cmd.upgradeAllCommand), }, { Name: "ls", - Usage: "list all installed plugins", + Usage: "list installed plugins (excludes core plugins)", Action: runPluginCommand(cmd.lsCommand), }, { Name: "uninstall", diff --git a/pkg/cmd/grafana-cli/commands/install_command.go b/pkg/cmd/grafana-cli/commands/install_command.go index ba1620a4048..be8b7b75499 100644 --- a/pkg/cmd/grafana-cli/commands/install_command.go +++ b/pkg/cmd/grafana-cli/commands/install_command.go @@ -8,6 +8,8 @@ import ( "runtime" "strings" + "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" @@ -41,6 +43,10 @@ func validateInput(c utils.CommandLine, pluginFolder string) error { return nil } +func logRestartNotice() { + logger.Info(color.GreenString("Please restart Grafana after installing or removing plugins. Refer to Grafana documentation for instructions if necessary.\n\n")) +} + func (cmd Command) installCommand(c utils.CommandLine) error { pluginFolder := c.PluginDirectory() if err := validateInput(c, pluginFolder); err != nil { @@ -49,7 +55,11 @@ func (cmd Command) installCommand(c utils.CommandLine) error { pluginID := c.Args().First() version := c.Args().Get(1) - return installPlugin(context.Background(), pluginID, version, c) + err := installPlugin(context.Background(), pluginID, version, c) + if err == nil { + logRestartNotice() + } + return err } // installPlugin downloads the plugin code as a zip file from the Grafana.com API diff --git a/pkg/cmd/grafana-cli/commands/ls_command.go b/pkg/cmd/grafana-cli/commands/ls_command.go index 35c7fdd591b..aa3b94d0214 100644 --- a/pkg/cmd/grafana-cli/commands/ls_command.go +++ b/pkg/cmd/grafana-cli/commands/ls_command.go @@ -45,6 +45,8 @@ func (cmd Command) lsCommand(c utils.CommandLine) error { if len(plugins) > 0 { logger.Info("installed plugins:\n") + } else { + logger.Info("no installed plugins found\n") } for _, plugin := range plugins { diff --git a/pkg/cmd/grafana-cli/commands/remove_command.go b/pkg/cmd/grafana-cli/commands/remove_command.go index 4eee1a9dbd7..953bb7e7583 100644 --- a/pkg/cmd/grafana-cli/commands/remove_command.go +++ b/pkg/cmd/grafana-cli/commands/remove_command.go @@ -27,6 +27,8 @@ func (cmd Command) removeCommand(c utils.CommandLine) error { } return err + } else { + logRestartNotice() } return nil diff --git a/pkg/cmd/grafana-cli/commands/upgrade_all_command.go b/pkg/cmd/grafana-cli/commands/upgrade_all_command.go index 759b6561bca..a2309877a33 100644 --- a/pkg/cmd/grafana-cli/commands/upgrade_all_command.go +++ b/pkg/cmd/grafana-cli/commands/upgrade_all_command.go @@ -63,5 +63,9 @@ func (cmd Command) upgradeAllCommand(c utils.CommandLine) error { } } + if len(pluginsToUpgrade) > 0 { + logRestartNotice() + } + return nil } diff --git a/pkg/cmd/grafana-cli/commands/upgrade_command.go b/pkg/cmd/grafana-cli/commands/upgrade_command.go index 9e1eb5df6df..81ba757c4ac 100644 --- a/pkg/cmd/grafana-cli/commands/upgrade_command.go +++ b/pkg/cmd/grafana-cli/commands/upgrade_command.go @@ -31,7 +31,11 @@ func (cmd Command) upgradeCommand(c utils.CommandLine) error { return fmt.Errorf("failed to remove plugin '%s': %w", pluginName, err) } - return installPlugin(context.Background(), pluginName, "", c) + err := installPlugin(context.Background(), pluginName, "", c) + if err == nil { + logRestartNotice() + } + return err } logger.Infof("%s %s is up to date \n", color.GreenString("✔"), pluginName)