Commit 766d8a58 by Carl Bergquist Committed by GitHub

avoid aliased import in cli (#22566)

parent 3fdd2648
...@@ -20,8 +20,8 @@ import ( ...@@ -20,8 +20,8 @@ import (
"golang.org/x/xerrors" "golang.org/x/xerrors"
"github.com/grafana/grafana/pkg/cmd/grafana-cli/logger" "github.com/grafana/grafana/pkg/cmd/grafana-cli/logger"
m "github.com/grafana/grafana/pkg/cmd/grafana-cli/models" "github.com/grafana/grafana/pkg/cmd/grafana-cli/models"
s "github.com/grafana/grafana/pkg/cmd/grafana-cli/services" "github.com/grafana/grafana/pkg/cmd/grafana-cli/services"
) )
func validateInput(c utils.CommandLine, pluginFolder string) error { func validateInput(c utils.CommandLine, pluginFolder string) error {
...@@ -132,7 +132,7 @@ func InstallPlugin(pluginName, version string, c utils.CommandLine, client utils ...@@ -132,7 +132,7 @@ func InstallPlugin(pluginName, version string, c utils.CommandLine, client utils
logger.Infof("%s Installed %s successfully \n", color.GreenString("✔"), pluginName) logger.Infof("%s Installed %s successfully \n", color.GreenString("✔"), pluginName)
res, _ := s.ReadPlugin(pluginFolder, pluginName) res, _ := services.ReadPlugin(pluginFolder, pluginName)
for _, v := range res.Dependencies.Plugins { for _, v := range res.Dependencies.Plugins {
if err := InstallPlugin(v.Id, "", c, client); err != nil { if err := InstallPlugin(v.Id, "", c, client); err != nil {
return errutil.Wrapf(err, "failed to install plugin '%s'", v.Id) return errutil.Wrapf(err, "failed to install plugin '%s'", v.Id)
...@@ -150,7 +150,7 @@ func osAndArchString() string { ...@@ -150,7 +150,7 @@ func osAndArchString() string {
return osString + "-" + arch return osString + "-" + arch
} }
func supportsCurrentArch(version *m.Version) bool { func supportsCurrentArch(version *models.Version) bool {
if version.Arch == nil { if version.Arch == nil {
return true return true
} }
...@@ -162,7 +162,7 @@ func supportsCurrentArch(version *m.Version) bool { ...@@ -162,7 +162,7 @@ func supportsCurrentArch(version *m.Version) bool {
return false return false
} }
func latestSupportedVersion(plugin *m.Plugin) *m.Version { func latestSupportedVersion(plugin *models.Plugin) *models.Version {
for _, ver := range plugin.Versions { for _, ver := range plugin.Versions {
if supportsCurrentArch(&ver) { if supportsCurrentArch(&ver) {
return &ver return &ver
...@@ -174,8 +174,8 @@ func latestSupportedVersion(plugin *m.Plugin) *m.Version { ...@@ -174,8 +174,8 @@ func latestSupportedVersion(plugin *m.Plugin) *m.Version {
// SelectVersion returns latest version if none is specified or the specified version. If the version string is not // SelectVersion returns latest version if none is specified or the specified version. If the version string is not
// matched to existing version it errors out. It also errors out if version that is matched is not available for current // matched to existing version it errors out. It also errors out if version that is matched is not available for current
// os and platform. It expects plugin.Versions to be sorted so the newest version is first. // os and platform. It expects plugin.Versions to be sorted so the newest version is first.
func SelectVersion(plugin *m.Plugin, version string) (*m.Version, error) { func SelectVersion(plugin *models.Plugin, version string) (*models.Version, error) {
var ver m.Version var ver models.Version
latestForArch := latestSupportedVersion(plugin) latestForArch := latestSupportedVersion(plugin)
if latestForArch == nil { if latestForArch == nil {
......
...@@ -5,12 +5,12 @@ import ( ...@@ -5,12 +5,12 @@ import (
"github.com/fatih/color" "github.com/fatih/color"
"github.com/grafana/grafana/pkg/cmd/grafana-cli/logger" "github.com/grafana/grafana/pkg/cmd/grafana-cli/logger"
m "github.com/grafana/grafana/pkg/cmd/grafana-cli/models" "github.com/grafana/grafana/pkg/cmd/grafana-cli/models"
s "github.com/grafana/grafana/pkg/cmd/grafana-cli/services" "github.com/grafana/grafana/pkg/cmd/grafana-cli/services"
"github.com/grafana/grafana/pkg/cmd/grafana-cli/utils" "github.com/grafana/grafana/pkg/cmd/grafana-cli/utils"
) )
var ls_getPlugins func(path string) []m.InstalledPlugin = s.GetLocalPlugins var ls_getPlugins func(path string) []models.InstalledPlugin = services.GetLocalPlugins
var validateLsCommand = func(pluginDir string) error { var validateLsCommand = func(pluginDir string) error {
if pluginDir == "" { if pluginDir == "" {
...@@ -18,7 +18,7 @@ var validateLsCommand = func(pluginDir string) error { ...@@ -18,7 +18,7 @@ var validateLsCommand = func(pluginDir string) error {
} }
logger.Debug("plugindir: " + pluginDir + "\n") logger.Debug("plugindir: " + pluginDir + "\n")
pluginDirInfo, err := s.IoHelper.Stat(pluginDir) pluginDirInfo, err := services.IoHelper.Stat(pluginDir)
if err != nil { if err != nil {
return err return err
} }
......
...@@ -2,10 +2,11 @@ package commands ...@@ -2,10 +2,11 @@ package commands
import ( import (
"errors" "errors"
"testing"
"github.com/grafana/grafana/pkg/cmd/grafana-cli/commands/commandstest" "github.com/grafana/grafana/pkg/cmd/grafana-cli/commands/commandstest"
s "github.com/grafana/grafana/pkg/cmd/grafana-cli/services" "github.com/grafana/grafana/pkg/cmd/grafana-cli/services"
. "github.com/smartystreets/goconvey/convey" . "github.com/smartystreets/goconvey/convey"
"testing"
) )
func TestMissingPath(t *testing.T) { func TestMissingPath(t *testing.T) {
...@@ -18,7 +19,7 @@ func TestMissingPath(t *testing.T) { ...@@ -18,7 +19,7 @@ func TestMissingPath(t *testing.T) {
cmd := Command{} cmd := Command{}
c, err := commandstest.NewCliContext(map[string]string{}) c, err := commandstest.NewCliContext(map[string]string{})
So(err, ShouldBeNil) So(err, ShouldBeNil)
s.IoHelper = &commandstest.FakeIoUtil{} services.IoHelper = &commandstest.FakeIoUtil{}
Convey("should return error", func() { Convey("should return error", func() {
err := cmd.lsCommand(c) err := cmd.lsCommand(c)
...@@ -29,7 +30,7 @@ func TestMissingPath(t *testing.T) { ...@@ -29,7 +30,7 @@ func TestMissingPath(t *testing.T) {
Convey("Path is not a directory", func() { Convey("Path is not a directory", func() {
c, err := commandstest.NewCliContext(map[string]string{"path": "/var/lib/grafana/plugins"}) c, err := commandstest.NewCliContext(map[string]string{"path": "/var/lib/grafana/plugins"})
So(err, ShouldBeNil) So(err, ShouldBeNil)
s.IoHelper = &commandstest.FakeIoUtil{ services.IoHelper = &commandstest.FakeIoUtil{
FakeIsDirectory: false, FakeIsDirectory: false,
} }
cmd := Command{} cmd := Command{}
......
...@@ -2,13 +2,13 @@ package commands ...@@ -2,13 +2,13 @@ package commands
import ( import (
"github.com/grafana/grafana/pkg/cmd/grafana-cli/logger" "github.com/grafana/grafana/pkg/cmd/grafana-cli/logger"
m "github.com/grafana/grafana/pkg/cmd/grafana-cli/models" "github.com/grafana/grafana/pkg/cmd/grafana-cli/models"
s "github.com/grafana/grafana/pkg/cmd/grafana-cli/services" "github.com/grafana/grafana/pkg/cmd/grafana-cli/services"
"github.com/grafana/grafana/pkg/cmd/grafana-cli/utils" "github.com/grafana/grafana/pkg/cmd/grafana-cli/utils"
"github.com/hashicorp/go-version" "github.com/hashicorp/go-version"
) )
func shouldUpgrade(installed string, remote *m.Plugin) bool { func shouldUpgrade(installed string, remote *models.Plugin) bool {
installedVersion, err := version.NewVersion(installed) installedVersion, err := version.NewVersion(installed)
if err != nil { if err != nil {
return false return false
...@@ -25,14 +25,14 @@ func shouldUpgrade(installed string, remote *m.Plugin) bool { ...@@ -25,14 +25,14 @@ func shouldUpgrade(installed string, remote *m.Plugin) bool {
func (cmd Command) upgradeAllCommand(c utils.CommandLine) error { func (cmd Command) upgradeAllCommand(c utils.CommandLine) error {
pluginsDir := c.PluginDirectory() pluginsDir := c.PluginDirectory()
localPlugins := s.GetLocalPlugins(pluginsDir) localPlugins := services.GetLocalPlugins(pluginsDir)
remotePlugins, err := cmd.Client.ListAllPlugins(c.String("repo")) remotePlugins, err := cmd.Client.ListAllPlugins(c.String("repo"))
if err != nil { if err != nil {
return err return err
} }
pluginsToUpgrade := make([]m.InstalledPlugin, 0) pluginsToUpgrade := make([]models.InstalledPlugin, 0)
for _, localPlugin := range localPlugins { for _, localPlugin := range localPlugins {
for _, remotePlugin := range remotePlugins.Plugins { for _, remotePlugin := range remotePlugins.Plugins {
...@@ -47,7 +47,7 @@ func (cmd Command) upgradeAllCommand(c utils.CommandLine) error { ...@@ -47,7 +47,7 @@ func (cmd Command) upgradeAllCommand(c utils.CommandLine) error {
for _, p := range pluginsToUpgrade { for _, p := range pluginsToUpgrade {
logger.Infof("Updating %v \n", p.Id) logger.Infof("Updating %v \n", p.Id)
err := s.RemoveInstalledPlugin(pluginsDir, p.Id) err := services.RemoveInstalledPlugin(pluginsDir, p.Id)
if err != nil { if err != nil {
return err return err
} }
......
...@@ -3,7 +3,7 @@ package commands ...@@ -3,7 +3,7 @@ package commands
import ( import (
"github.com/fatih/color" "github.com/fatih/color"
"github.com/grafana/grafana/pkg/cmd/grafana-cli/logger" "github.com/grafana/grafana/pkg/cmd/grafana-cli/logger"
s "github.com/grafana/grafana/pkg/cmd/grafana-cli/services" "github.com/grafana/grafana/pkg/cmd/grafana-cli/services"
"github.com/grafana/grafana/pkg/cmd/grafana-cli/utils" "github.com/grafana/grafana/pkg/cmd/grafana-cli/utils"
"github.com/grafana/grafana/pkg/util/errutil" "github.com/grafana/grafana/pkg/util/errutil"
) )
...@@ -12,7 +12,7 @@ func (cmd Command) upgradeCommand(c utils.CommandLine) error { ...@@ -12,7 +12,7 @@ func (cmd Command) upgradeCommand(c utils.CommandLine) error {
pluginsDir := c.PluginDirectory() pluginsDir := c.PluginDirectory()
pluginName := c.Args().First() pluginName := c.Args().First()
localPlugin, err := s.ReadPlugin(pluginsDir, pluginName) localPlugin, err := services.ReadPlugin(pluginsDir, pluginName)
if err != nil { if err != nil {
return err return err
...@@ -24,7 +24,7 @@ func (cmd Command) upgradeCommand(c utils.CommandLine) error { ...@@ -24,7 +24,7 @@ func (cmd Command) upgradeCommand(c utils.CommandLine) error {
} }
if shouldUpgrade(localPlugin.Info.Version, &plugin) { if shouldUpgrade(localPlugin.Info.Version, &plugin) {
if err := s.RemoveInstalledPlugin(pluginsDir, pluginName); err != nil { if err := services.RemoveInstalledPlugin(pluginsDir, pluginName); err != nil {
return errutil.Wrapf(err, "failed to remove plugin '%s'", pluginName) return errutil.Wrapf(err, "failed to remove plugin '%s'", pluginName)
} }
......
...@@ -11,11 +11,11 @@ import ( ...@@ -11,11 +11,11 @@ import (
"time" "time"
"github.com/grafana/grafana/pkg/cmd/grafana-cli/logger" "github.com/grafana/grafana/pkg/cmd/grafana-cli/logger"
m "github.com/grafana/grafana/pkg/cmd/grafana-cli/models" "github.com/grafana/grafana/pkg/cmd/grafana-cli/models"
) )
var ( var (
IoHelper m.IoUtil = IoUtilImp{} IoHelper models.IoUtil = IoUtilImp{}
HttpClient http.Client HttpClient http.Client
HttpClientNoTimeout http.Client HttpClientNoTimeout http.Client
grafanaVersion string grafanaVersion string
...@@ -63,7 +63,7 @@ func makeHttpClient(skipTLSVerify bool, timeout time.Duration) http.Client { ...@@ -63,7 +63,7 @@ func makeHttpClient(skipTLSVerify bool, timeout time.Duration) http.Client {
} }
} }
func ReadPlugin(pluginDir, pluginName string) (m.InstalledPlugin, error) { func ReadPlugin(pluginDir, pluginName string) (models.InstalledPlugin, error) {
distPluginDataPath := path.Join(pluginDir, pluginName, "dist", "plugin.json") distPluginDataPath := path.Join(pluginDir, pluginName, "dist", "plugin.json")
var data []byte var data []byte
...@@ -75,11 +75,11 @@ func ReadPlugin(pluginDir, pluginName string) (m.InstalledPlugin, error) { ...@@ -75,11 +75,11 @@ func ReadPlugin(pluginDir, pluginName string) (m.InstalledPlugin, error) {
data, err = IoHelper.ReadFile(pluginDataPath) data, err = IoHelper.ReadFile(pluginDataPath)
if err != nil { if err != nil {
return m.InstalledPlugin{}, errors.New("Could not find dist/plugin.json or plugin.json on " + pluginName + " in " + pluginDir) return models.InstalledPlugin{}, errors.New("Could not find dist/plugin.json or plugin.json on " + pluginName + " in " + pluginDir)
} }
} }
res := m.InstalledPlugin{} res := models.InstalledPlugin{}
if err := json.Unmarshal(data, &res); err != nil { if err := json.Unmarshal(data, &res); err != nil {
return res, err return res, err
} }
...@@ -89,14 +89,14 @@ func ReadPlugin(pluginDir, pluginName string) (m.InstalledPlugin, error) { ...@@ -89,14 +89,14 @@ func ReadPlugin(pluginDir, pluginName string) (m.InstalledPlugin, error) {
} }
if res.Id == "" { if res.Id == "" {
return m.InstalledPlugin{}, errors.New("could not find plugin " + pluginName + " in " + pluginDir) return models.InstalledPlugin{}, errors.New("could not find plugin " + pluginName + " in " + pluginDir)
} }
return res, nil return res, nil
} }
func GetLocalPlugins(pluginDir string) []m.InstalledPlugin { func GetLocalPlugins(pluginDir string) []models.InstalledPlugin {
result := make([]m.InstalledPlugin, 0) result := make([]models.InstalledPlugin, 0)
files, _ := IoHelper.ReadDir(pluginDir) files, _ := IoHelper.ReadDir(pluginDir)
for _, f := range files { for _, f := range files {
res, err := ReadPlugin(pluginDir, f.Name()) res, err := ReadPlugin(pluginDir, f.Name())
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment