From af216ecf8396c349c538adbd4e17e72eecab300f Mon Sep 17 00:00:00 2001 From: bergquist Date: Sat, 25 Jun 2016 01:18:49 +0200 Subject: [PATCH] tech(cli): remove loop and head straight for plugindir --- pkg/cmd/grafana-cli/commands/remove_command.go | 16 +++++++++------- pkg/cmd/grafana-cli/services/services.go | 13 ++++++++++--- 2 files changed, 19 insertions(+), 10 deletions(-) diff --git a/pkg/cmd/grafana-cli/commands/remove_command.go b/pkg/cmd/grafana-cli/commands/remove_command.go index ef465acd692..d5ed73def05 100644 --- a/pkg/cmd/grafana-cli/commands/remove_command.go +++ b/pkg/cmd/grafana-cli/commands/remove_command.go @@ -2,10 +2,10 @@ package commands import ( "errors" - "fmt" m "github.com/grafana/grafana/pkg/cmd/grafana-cli/models" services "github.com/grafana/grafana/pkg/cmd/grafana-cli/services" + "strings" ) var getPluginss func(path string) []m.InstalledPlugin = services.GetLocalPlugins @@ -13,19 +13,21 @@ var removePlugin func(pluginPath, id string) error = services.RemoveInstalledPlu func removeCommand(c CommandLine) error { pluginPath := c.PluginDirectory() - localPlugins := getPluginss(pluginPath) plugin := c.Args().First() if plugin == "" { return errors.New("Missing plugin parameter") } - for _, p := range localPlugins { - if p.Id == c.Args().First() { - removePlugin(pluginPath, p.Id) - return nil + err := removePlugin(pluginPath, plugin) + + if err != nil { + if strings.Contains(err.Error(), "no such file or directory") { + return fmt.Errorf("Plugin does not exist") } + + return err } - return fmt.Errorf("Could not find plugin named %s", c.Args().First()) + return nil } diff --git a/pkg/cmd/grafana-cli/services/services.go b/pkg/cmd/grafana-cli/services/services.go index 1c53619c071..c5c45460722 100644 --- a/pkg/cmd/grafana-cli/services/services.go +++ b/pkg/cmd/grafana-cli/services/services.go @@ -75,9 +75,16 @@ func GetLocalPlugins(pluginDir string) []m.InstalledPlugin { return result } -func RemoveInstalledPlugin(pluginPath, id string) error { - logger.Infof("Removing plugin: %v\n", id) - return IoHelper.RemoveAll(path.Join(pluginPath, id)) +func RemoveInstalledPlugin(pluginPath, pluginName string) error { + logger.Infof("Removing plugin: %v\n", pluginName) + pluginDir := path.Join(pluginPath, pluginName) + + _, err := IoHelper.Stat(pluginDir) + if err != nil { + return err + } + + return IoHelper.RemoveAll(pluginDir) } func GetPlugin(pluginId, repoUrl string) (m.Plugin, error) {