Optimization logic

Signed-off-by: yxxhero <aiopsclub@163.com>
pull/9854/head
yxxhero 4 years ago
parent 3f75449824
commit ecf82a882c

@ -71,7 +71,7 @@ func (o *pluginInstallOptions) complete(args []string) error {
func (o *pluginInstallOptions) run(out io.Writer) error {
installer.Debug = settings.Debug
i, err := installer.NewForSource(o.source, o.version, settings.PluginsDirectory)
i, err := installer.NewForSourceWithPluginsDirectory(o.source, o.version, settings.PluginsDirectory)
if err != nil {
return err
}

@ -96,7 +96,7 @@ func updatePlugin(p *plugin.Plugin) error {
return err
}
i, err := installer.FindSource(absExactLocation, settings.PluginsDirectory)
i, err := installer.FindSource(absExactLocation)
if err != nil {
return err
}

@ -82,7 +82,7 @@ func NewExtractor(source string) (Extractor, error) {
}
// NewHTTPInstaller creates a new HttpInstaller.
func NewHTTPInstaller(source, pluginsDirectory string) (*HTTPInstaller, error) {
func NewHTTPInstaller(source string) (*HTTPInstaller, error) {
key, err := cache.Key(source)
if err != nil {
return nil, err
@ -101,13 +101,22 @@ func NewHTTPInstaller(source, pluginsDirectory string) (*HTTPInstaller, error) {
i := &HTTPInstaller{
CacheDir: helmpath.CachePath("plugins", key),
PluginName: stripPluginName(filepath.Base(source)),
base: newBase(source, pluginsDirectory),
extractor: extractor,
getter: get,
}
return i, nil
}
// NewHTTPInstallerWithPluginsDirectory creates a new HttpInstaller with pluginsDirectory arg, it should be removed in helm v4.
func NewHTTPInstallerWithPluginsDirectory(source, pluginsDirectory string) (*HTTPInstaller, error) {
i, err := NewHTTPInstaller(source)
if err != nil {
return nil, err
}
i.base = newBase(source, pluginsDirectory)
return i, nil
}
// helper that relies on some sort of convention for plugin name (plugin-name-<version>)
func stripPluginName(name string) string {
var strippedName string

@ -92,7 +92,7 @@ func TestHTTPInstaller(t *testing.T) {
t.Fatalf("Could not create %s: %s", helmpath.DataPath("plugins"), err)
}
i, err := NewForSource(source, "0.0.1", settings.PluginsDirectory)
i, err := NewForSourceWithPluginsDirectory(source, "0.0.1", settings.PluginsDirectory)
if err != nil {
t.Fatalf("unexpected error: %s", err)
}
@ -141,7 +141,7 @@ func TestHTTPInstallerNonExistentVersion(t *testing.T) {
t.Fatalf("Could not create %s: %s", helmpath.DataPath("plugins"), err)
}
i, err := NewForSource(source, "0.0.2", settings.PluginsDirectory)
i, err := NewForSourceWithPluginsDirectory(source, "0.0.2", settings.PluginsDirectory)
if err != nil {
t.Fatalf("unexpected error: %s", err)
}
@ -175,7 +175,7 @@ func TestHTTPInstallerUpdate(t *testing.T) {
t.Fatalf("Could not create %s: %s", helmpath.DataPath("plugins"), err)
}
i, err := NewForSource(source, "0.0.1", settings.PluginsDirectory)
i, err := NewForSourceWithPluginsDirectory(source, "0.0.1", settings.PluginsDirectory)
if err != nil {
t.Fatalf("unexpected error: %s", err)
}

@ -63,20 +63,31 @@ func Update(i Installer) error {
return i.Update()
}
// NewForSourceWithPluginsDirectory determines the correct Installer for the given source with pluginsDiretory arg, it should be removed in helm v4.
func NewForSourceWithPluginsDirectory(source, version, pluginsDirectory string) (Installer, error) {
// Check if source is a local directory
if isLocalReference(source) {
return NewLocalInstallerWithPluginsDirectory(source, pluginsDirectory)
} else if isRemoteHTTPArchive(source) {
return NewHTTPInstallerWithPluginsDirectory(source, pluginsDirectory)
}
return NewVCSInstallerWithPluginsDirectory(source, version, pluginsDirectory)
}
// NewForSource determines the correct Installer for the given source.
func NewForSource(source, version, pluginsDirectory string) (Installer, error) {
func NewForSource(source, version string) (Installer, error) {
// Check if source is a local directory
if isLocalReference(source) {
return NewLocalInstaller(source, pluginsDirectory)
return NewLocalInstaller(source)
} else if isRemoteHTTPArchive(source) {
return NewHTTPInstaller(source, pluginsDirectory)
return NewHTTPInstaller(source)
}
return NewVCSInstaller(source, version, pluginsDirectory)
return NewVCSInstaller(source, version)
}
// FindSource determines the correct Installer for the given source.
func FindSource(location, pluginsDirectory string) (Installer, error) {
installer, err := existingVCSRepo(location, pluginsDirectory)
func FindSource(location string) (Installer, error) {
installer, err := existingVCSRepo(location)
if err != nil && err.Error() == "Cannot detect VCS" {
return installer, errors.New("cannot get information about plugin source")
}

@ -27,16 +27,20 @@ type LocalInstaller struct {
base
}
// NewLocalInstaller creates a new LocalInstaller.
func NewLocalInstaller(source, pluginsDirectory string) (*LocalInstaller, error) {
// NewLocalInstallerWithPluginsDirectory creates a new LocalInstaller with pluginsDirectory arg, it should be removed in helm v4.
func NewLocalInstallerWithPluginsDirectory(source, pluginsDirectory string) (*LocalInstaller, error) {
src, err := filepath.Abs(source)
if err != nil {
return nil, errors.Wrap(err, "unable to get absolute path to plugin")
}
i := &LocalInstaller{
base: newBase(src, pluginsDirectory),
i, err := NewLocalInstaller(source)
i.base = newBase(pluginsDirectory, src)
return i, err
}
return i, nil
// NewLocalInstaller creates a new LocalInstaller.
func NewLocalInstaller(source string) (*LocalInstaller, error) {
return &LocalInstaller{}, nil
}
// Install creates a symlink to the plugin directory.

@ -40,7 +40,7 @@ func TestLocalInstaller(t *testing.T) {
}
source := "../testdata/plugdir/good/echo"
i, err := NewForSource(source, "", settings.PluginsDirectory)
i, err := NewForSourceWithPluginsDirectory(source, "", settings.PluginsDirectory)
if err != nil {
t.Fatalf("unexpected error: %s", err)
}

@ -24,6 +24,7 @@ import (
"github.com/pkg/errors"
"helm.sh/helm/v3/internal/third_party/dep/fs"
"helm.sh/helm/v3/pkg/cli"
"helm.sh/helm/v3/pkg/helmpath"
"helm.sh/helm/v3/pkg/plugin/cache"
)
@ -35,20 +36,32 @@ type VCSInstaller struct {
base
}
func existingVCSRepo(location, pluginsDirectory string) (Installer, error) {
func existingVCSRepo(location string) (Installer, error) {
repo, err := vcs.NewRepo("", location)
if err != nil {
return nil, err
}
settings := cli.New()
i := &VCSInstaller{
Repo: repo,
base: newBase(repo.Remote(), pluginsDirectory),
base: newBase(repo.Remote(), settings.PluginsDirectory),
}
return i, nil
}
// NewVCSInstallerWithPluginsDirectory creates a new VCSInstaller with pluginsDirector arg, it should be removed in helm v4.
func NewVCSInstallerWithPluginsDirectory(source, version, pluginsDirectory string) (*VCSInstaller, error) {
i, err := NewVCSInstaller(source, version)
if err != nil {
return nil, err
}
i.base = newBase(source, pluginsDirectory)
return i, nil
}
// NewVCSInstaller creates a new VCSInstaller.
func NewVCSInstaller(source, version, pluginsDirectory string) (*VCSInstaller, error) {
func NewVCSInstaller(source, version string) (*VCSInstaller, error) {
key, err := cache.Key(source)
if err != nil {
return nil, err
@ -61,7 +74,6 @@ func NewVCSInstaller(source, version, pluginsDirectory string) (*VCSInstaller, e
i := &VCSInstaller{
Repo: repo,
Version: version,
base: newBase(source, pluginsDirectory),
}
return i, err
}

@ -64,7 +64,7 @@ func TestVCSInstaller(t *testing.T) {
tags: []string{"0.1.0", "0.1.1"},
}
i, err := NewForSource(source, "~0.1.0", settings.PluginsDirectory)
i, err := NewForSourceWithPluginsDirectory(source, "~0.1.0", settings.PluginsDirectory)
if err != nil {
t.Fatalf("unexpected error: %s", err)
}
@ -96,7 +96,7 @@ func TestVCSInstaller(t *testing.T) {
}
// Testing FindSource method, expect error because plugin code is not a cloned repository
if _, err := FindSource(i.Path(), settings.PluginsDirectory); err == nil {
if _, err := FindSource(i.Path()); err == nil {
t.Fatalf("expected error for inability to find plugin source, got none")
} else if err.Error() != "cannot get information about plugin source" {
t.Fatalf("expected error for inability to find plugin source, got (%v)", err)
@ -110,7 +110,7 @@ func TestVCSInstallerNonExistentVersion(t *testing.T) {
version := "0.2.0"
settings := cli.New()
i, err := NewForSource(source, version, settings.PluginsDirectory)
i, err := NewForSourceWithPluginsDirectory(source, version, settings.PluginsDirectory)
if err != nil {
t.Fatalf("unexpected error: %s", err)
}
@ -132,7 +132,7 @@ func TestVCSInstallerUpdate(t *testing.T) {
source := "https://github.com/adamreese/helm-env"
settings := cli.New()
i, err := NewForSource(source, "", settings.PluginsDirectory)
i, err := NewForSourceWithPluginsDirectory(source, "", settings.PluginsDirectory)
if err != nil {
t.Fatalf("unexpected error: %s", err)
}
@ -154,7 +154,7 @@ func TestVCSInstallerUpdate(t *testing.T) {
}
// Test FindSource method for positive result
pluginInfo, err := FindSource(i.Path(), settings.PluginsDirectory)
pluginInfo, err := FindSource(i.Path())
if err != nil {
t.Fatal(err)
}

Loading…
Cancel
Save