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 { func (o *pluginInstallOptions) run(out io.Writer) error {
installer.Debug = settings.Debug 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 { if err != nil {
return err return err
} }

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

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

@ -92,7 +92,7 @@ func TestHTTPInstaller(t *testing.T) {
t.Fatalf("Could not create %s: %s", helmpath.DataPath("plugins"), err) 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 { if err != nil {
t.Fatalf("unexpected error: %s", err) 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) 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 { if err != nil {
t.Fatalf("unexpected error: %s", err) 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) 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 { if err != nil {
t.Fatalf("unexpected error: %s", err) t.Fatalf("unexpected error: %s", err)
} }

@ -63,20 +63,31 @@ func Update(i Installer) error {
return i.Update() 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. // 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 // Check if source is a local directory
if isLocalReference(source) { if isLocalReference(source) {
return NewLocalInstaller(source, pluginsDirectory) return NewLocalInstaller(source)
} else if isRemoteHTTPArchive(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. // FindSource determines the correct Installer for the given source.
func FindSource(location, pluginsDirectory string) (Installer, error) { func FindSource(location string) (Installer, error) {
installer, err := existingVCSRepo(location, pluginsDirectory) installer, err := existingVCSRepo(location)
if err != nil && err.Error() == "Cannot detect VCS" { if err != nil && err.Error() == "Cannot detect VCS" {
return installer, errors.New("cannot get information about plugin source") return installer, errors.New("cannot get information about plugin source")
} }

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

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

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

@ -64,7 +64,7 @@ func TestVCSInstaller(t *testing.T) {
tags: []string{"0.1.0", "0.1.1"}, 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 { if err != nil {
t.Fatalf("unexpected error: %s", err) 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 // 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") t.Fatalf("expected error for inability to find plugin source, got none")
} else if err.Error() != "cannot get information about plugin source" { } else if err.Error() != "cannot get information about plugin source" {
t.Fatalf("expected error for inability to find plugin source, got (%v)", err) 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" version := "0.2.0"
settings := cli.New() settings := cli.New()
i, err := NewForSource(source, version, settings.PluginsDirectory) i, err := NewForSourceWithPluginsDirectory(source, version, settings.PluginsDirectory)
if err != nil { if err != nil {
t.Fatalf("unexpected error: %s", err) t.Fatalf("unexpected error: %s", err)
} }
@ -132,7 +132,7 @@ func TestVCSInstallerUpdate(t *testing.T) {
source := "https://github.com/adamreese/helm-env" source := "https://github.com/adamreese/helm-env"
settings := cli.New() settings := cli.New()
i, err := NewForSource(source, "", settings.PluginsDirectory) i, err := NewForSourceWithPluginsDirectory(source, "", settings.PluginsDirectory)
if err != nil { if err != nil {
t.Fatalf("unexpected error: %s", err) t.Fatalf("unexpected error: %s", err)
} }
@ -154,7 +154,7 @@ func TestVCSInstallerUpdate(t *testing.T) {
} }
// Test FindSource method for positive result // Test FindSource method for positive result
pluginInfo, err := FindSource(i.Path(), settings.PluginsDirectory) pluginInfo, err := FindSource(i.Path())
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }

Loading…
Cancel
Save