Browse Source

feat(cli): add command for upgrading one plugin

bergquist 9 năm trước cách đây
mục cha
commit
4131d8b57a

+ 4 - 4
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 . <service grafana-server restart>\n")
+			log.Info("\nRestart grafana after installing plugins . <service grafana-server restart>\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 <plugin name>",
 		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 <plugin name>",
 		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 <plugin name>",
 		Action: runCommand(removeCommand),
 	},
 }

+ 3 - 4
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

+ 26 - 2
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
 }

+ 0 - 3
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 {
 }
 

+ 9 - 5
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))
 }