diff --git a/pkg/cmd/grafana-cli/commands/commands.go b/pkg/cmd/grafana-cli/commands/commands.go index 55e9a771480..67e9f945cfd 100644 --- a/pkg/cmd/grafana-cli/commands/commands.go +++ b/pkg/cmd/grafana-cli/commands/commands.go @@ -16,7 +16,7 @@ func runCommand(command func(commandLine CommandLine) error) func(context *cli.C cmd.ShowHelp() os.Exit(1) } else { - log.Info("Restart grafana after installing plugins . \n") + log.Info("\nRestart grafana after installing plugins . \n\n") } } } @@ -24,7 +24,7 @@ func runCommand(command func(commandLine CommandLine) error) func(context *cli.C var Commands = []cli.Command{ { Name: "install", - Usage: "installs a plugin", + Usage: "install ", Action: runCommand(installCommand), }, { Name: "list-remote", @@ -32,7 +32,7 @@ var Commands = []cli.Command{ Action: runCommand(listremoteCommand), }, { Name: "upgrade", - Usage: "upgrades one plugin", + Usage: "upgrade ", Action: runCommand(upgradeCommand), }, { Name: "upgrade-all", @@ -44,7 +44,7 @@ var Commands = []cli.Command{ Action: runCommand(lsCommand), }, { Name: "remove", - Usage: "removes installed plugin", + Usage: "remove ", Action: runCommand(removeCommand), }, } diff --git a/pkg/cmd/grafana-cli/commands/install_command.go b/pkg/cmd/grafana-cli/commands/install_command.go index 6c7970147aa..1d085cad89e 100644 --- a/pkg/cmd/grafana-cli/commands/install_command.go +++ b/pkg/cmd/grafana-cli/commands/install_command.go @@ -71,15 +71,14 @@ func InstallPlugin(pluginName, pluginFolder, version string) error { err = downloadFile(plugin.Id, pluginFolder, downloadURL) if err == nil { - log.Info("Installed %s successfully ✔\n", plugin.Id) + log.Infof("Installed %v successfully ✔\n", plugin.Id) } - res := s.ReadPlugin(pluginFolder, pluginName) + res, _ := s.ReadPlugin(pluginFolder, pluginName) for _, v := range res.Dependency.Plugins { - log.Infof("Installing Dependency: %s\n", v.Id) - InstallPlugin(v.Id, pluginFolder, "") + log.Infof("Installed Dependency: %v ✔\n", v.Id) } return err diff --git a/pkg/cmd/grafana-cli/commands/upgrade_command.go b/pkg/cmd/grafana-cli/commands/upgrade_command.go index df9581b3dc3..5a4fe477c9b 100644 --- a/pkg/cmd/grafana-cli/commands/upgrade_command.go +++ b/pkg/cmd/grafana-cli/commands/upgrade_command.go @@ -1,9 +1,33 @@ package commands import ( - "errors" + s "github.com/grafana/grafana/pkg/cmd/grafana-cli/services" ) func upgradeCommand(c CommandLine) error { - return errors.New("Not yet Implemented") + pluginDir := c.GlobalString("path") + pluginName := c.Args().First() + + localPlugin, err := s.ReadPlugin(pluginDir, pluginName) + + if err != nil { + return err + } + + remotePlugins, err2 := s.ListAllPlugins() + + if err2 != nil { + return err2 + } + + for _, v := range remotePlugins.Plugins { + if localPlugin.Id == v.Id { + if ShouldUpgrade(localPlugin.Info.Version, v) { + s.RemoveInstalledPlugin(pluginDir, pluginName) + return InstallPlugin(localPlugin.Id, pluginDir, "") + } + } + } + + return nil } diff --git a/pkg/cmd/grafana-cli/services/io_util.go b/pkg/cmd/grafana-cli/services/io_util.go index 07629a655d8..b1f1c140e2c 100644 --- a/pkg/cmd/grafana-cli/services/io_util.go +++ b/pkg/cmd/grafana-cli/services/io_util.go @@ -1,13 +1,10 @@ package services import ( - //m "github.com/grafana/grafana/pkg/cmd/grafana-cli/models" "io/ioutil" "os" ) -//var IoUtils m.IoUtil = IoUtilImp{} - type IoUtilImp struct { } diff --git a/pkg/cmd/grafana-cli/services/services.go b/pkg/cmd/grafana-cli/services/services.go index dba14d13d2d..d5051a623a3 100644 --- a/pkg/cmd/grafana-cli/services/services.go +++ b/pkg/cmd/grafana-cli/services/services.go @@ -4,6 +4,7 @@ import ( "encoding/json" "errors" "github.com/franela/goreq" + "github.com/grafana/grafana/pkg/cmd/grafana-cli/log" m "github.com/grafana/grafana/pkg/cmd/grafana-cli/models" "path" ) @@ -22,7 +23,7 @@ func ListAllPlugins() (m.PluginRepo, error) { return resp, nil } -func ReadPlugin(pluginDir, pluginName string) m.InstalledPlugin { +func ReadPlugin(pluginDir, pluginName string) (m.InstalledPlugin, error) { pluginDataPath := path.Join(pluginDir, pluginName, "plugin.json") pluginData, _ := IoHelper.ReadFile(pluginDataPath) @@ -34,24 +35,27 @@ func ReadPlugin(pluginDir, pluginName string) m.InstalledPlugin { } if res.Id == "" { - res.Id = res.Name + return m.InstalledPlugin{}, errors.New("could not read find plugin " + pluginName) } - return res + return res, nil } func GetLocalPlugins(pluginDir string) []m.InstalledPlugin { result := make([]m.InstalledPlugin, 0) files, _ := IoHelper.ReadDir(pluginDir) for _, f := range files { - res := ReadPlugin(pluginDir, f.Name()) - result = append(result, res) + res, err := ReadPlugin(pluginDir, f.Name()) + if err == nil { + result = append(result, res) + } } return result } func RemoveInstalledPlugin(pluginPath, id string) error { + log.Infof("Removing plugin: %v\n", id) return IoHelper.RemoveAll(path.Join(pluginPath, id)) }