feat(cli): add command for upgrading one plugin

This commit is contained in:
bergquist
2016-02-16 08:49:27 +01:00
parent 36f4f1d0f3
commit 4131d8b57a
5 changed files with 42 additions and 18 deletions

View File

@@ -16,7 +16,7 @@ func runCommand(command func(commandLine CommandLine) error) func(context *cli.C
cmd.ShowHelp() cmd.ShowHelp()
os.Exit(1) os.Exit(1)
} else { } 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{ var Commands = []cli.Command{
{ {
Name: "install", Name: "install",
Usage: "installs a plugin", Usage: "install <plugin name>",
Action: runCommand(installCommand), Action: runCommand(installCommand),
}, { }, {
Name: "list-remote", Name: "list-remote",
@@ -32,7 +32,7 @@ var Commands = []cli.Command{
Action: runCommand(listremoteCommand), Action: runCommand(listremoteCommand),
}, { }, {
Name: "upgrade", Name: "upgrade",
Usage: "upgrades one plugin", Usage: "upgrade <plugin name>",
Action: runCommand(upgradeCommand), Action: runCommand(upgradeCommand),
}, { }, {
Name: "upgrade-all", Name: "upgrade-all",
@@ -44,7 +44,7 @@ var Commands = []cli.Command{
Action: runCommand(lsCommand), Action: runCommand(lsCommand),
}, { }, {
Name: "remove", Name: "remove",
Usage: "removes installed plugin", Usage: "remove <plugin name>",
Action: runCommand(removeCommand), Action: runCommand(removeCommand),
}, },
} }

View File

@@ -71,15 +71,14 @@ func InstallPlugin(pluginName, pluginFolder, version string) error {
err = downloadFile(plugin.Id, pluginFolder, downloadURL) err = downloadFile(plugin.Id, pluginFolder, downloadURL)
if err == nil { 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 { for _, v := range res.Dependency.Plugins {
log.Infof("Installing Dependency: %s\n", v.Id)
InstallPlugin(v.Id, pluginFolder, "") InstallPlugin(v.Id, pluginFolder, "")
log.Infof("Installed Dependency: %v ✔\n", v.Id)
} }
return err return err

View File

@@ -1,9 +1,33 @@
package commands package commands
import ( import (
"errors" s "github.com/grafana/grafana/pkg/cmd/grafana-cli/services"
) )
func upgradeCommand(c CommandLine) error { 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
} }

View File

@@ -1,13 +1,10 @@
package services package services
import ( import (
//m "github.com/grafana/grafana/pkg/cmd/grafana-cli/models"
"io/ioutil" "io/ioutil"
"os" "os"
) )
//var IoUtils m.IoUtil = IoUtilImp{}
type IoUtilImp struct { type IoUtilImp struct {
} }

View File

@@ -4,6 +4,7 @@ import (
"encoding/json" "encoding/json"
"errors" "errors"
"github.com/franela/goreq" "github.com/franela/goreq"
"github.com/grafana/grafana/pkg/cmd/grafana-cli/log"
m "github.com/grafana/grafana/pkg/cmd/grafana-cli/models" m "github.com/grafana/grafana/pkg/cmd/grafana-cli/models"
"path" "path"
) )
@@ -22,7 +23,7 @@ func ListAllPlugins() (m.PluginRepo, error) {
return resp, nil 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") pluginDataPath := path.Join(pluginDir, pluginName, "plugin.json")
pluginData, _ := IoHelper.ReadFile(pluginDataPath) pluginData, _ := IoHelper.ReadFile(pluginDataPath)
@@ -34,24 +35,27 @@ func ReadPlugin(pluginDir, pluginName string) m.InstalledPlugin {
} }
if res.Id == "" { 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 { func GetLocalPlugins(pluginDir string) []m.InstalledPlugin {
result := make([]m.InstalledPlugin, 0) result := make([]m.InstalledPlugin, 0)
files, _ := IoHelper.ReadDir(pluginDir) files, _ := IoHelper.ReadDir(pluginDir)
for _, f := range files { for _, f := range files {
res := ReadPlugin(pluginDir, f.Name()) res, err := ReadPlugin(pluginDir, f.Name())
result = append(result, res) if err == nil {
result = append(result, res)
}
} }
return result return result
} }
func RemoveInstalledPlugin(pluginPath, id string) error { func RemoveInstalledPlugin(pluginPath, id string) error {
log.Infof("Removing plugin: %v\n", id)
return IoHelper.RemoveAll(path.Join(pluginPath, id)) return IoHelper.RemoveAll(path.Join(pluginPath, id))
} }