From 3f75449824ef025dfd1a4c3963ca924986be382e Mon Sep 17 00:00:00 2001 From: yxxhero Date: Sat, 17 Jul 2021 13:16:00 +0800 Subject: [PATCH] add PluginsDirectory in base struct Signed-off-by: yxxhero --- cmd/helm/plugin_install.go | 2 +- cmd/helm/plugin_update.go | 2 +- pkg/plugin/installer/base.go | 18 ++++++++---------- pkg/plugin/installer/base_test.go | 6 +----- pkg/plugin/installer/http_installer.go | 4 ++-- pkg/plugin/installer/http_installer_test.go | 10 +++++++--- pkg/plugin/installer/installer.go | 12 ++++++------ pkg/plugin/installer/local_installer.go | 4 ++-- pkg/plugin/installer/local_installer_test.go | 4 +++- pkg/plugin/installer/vcs_installer.go | 8 ++++---- pkg/plugin/installer/vcs_installer_test.go | 14 +++++++++----- 11 files changed, 44 insertions(+), 40 deletions(-) diff --git a/cmd/helm/plugin_install.go b/cmd/helm/plugin_install.go index 4e8ee327b..deccfd2fe 100644 --- a/cmd/helm/plugin_install.go +++ b/cmd/helm/plugin_install.go @@ -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) + i, err := installer.NewForSource(o.source, o.version, settings.PluginsDirectory) if err != nil { return err } diff --git a/cmd/helm/plugin_update.go b/cmd/helm/plugin_update.go index 4515acdbb..b2ea68fc8 100644 --- a/cmd/helm/plugin_update.go +++ b/cmd/helm/plugin_update.go @@ -96,7 +96,7 @@ func updatePlugin(p *plugin.Plugin) error { return err } - i, err := installer.FindSource(absExactLocation) + i, err := installer.FindSource(absExactLocation, settings.PluginsDirectory) if err != nil { return err } diff --git a/pkg/plugin/installer/base.go b/pkg/plugin/installer/base.go index 585282ea3..ec88e7fae 100644 --- a/pkg/plugin/installer/base.go +++ b/pkg/plugin/installer/base.go @@ -16,19 +16,21 @@ limitations under the License. package installer // import "helm.sh/helm/v3/pkg/plugin/installer" import ( - "os" "path/filepath" - - "helm.sh/helm/v3/pkg/helmpath" ) type base struct { // Source is the reference to a plugin Source string + // PluginsDirectory is the directory where plugins are installed + PluginsDirectory string } -func newBase(source string) base { - return base{source} +func newBase(source, pluginsDirectory string) base { + return base{ + Source: source, + PluginsDirectory: pluginsDirectory, + } } // Path is where the plugin will be installed. @@ -36,9 +38,5 @@ func (b *base) Path() string { if b.Source == "" { return "" } - helmPluginsDir := os.Getenv("HELM_PLUGINS") - if helmPluginsDir != "" { - return filepath.Join(helmPluginsDir, filepath.Base(b.Source)) - } - return helmpath.DataPath("plugins", filepath.Base(b.Source)) + return filepath.Join(b.PluginsDirectory, filepath.Base(b.Source)) } diff --git a/pkg/plugin/installer/base_test.go b/pkg/plugin/installer/base_test.go index 90b7f5692..e5bc1d75f 100644 --- a/pkg/plugin/installer/base_test.go +++ b/pkg/plugin/installer/base_test.go @@ -16,7 +16,6 @@ limitations under the License. package installer // import "helm.sh/helm/v3/pkg/plugin/installer" import ( - "os" "testing" ) @@ -39,13 +38,10 @@ func TestPath(t *testing.T) { for _, tt := range tests { - baseIns := newBase(tt.source) - os.Setenv("HELM_PLUGINS", tt.helmPluginsDir) + baseIns := newBase(tt.source, tt.helmPluginsDir) baseInsPath := baseIns.Path() if baseInsPath != tt.expectPath { t.Errorf("expected name %s, got %s", tt.expectPath, baseInsPath) } } - - defer os.Unsetenv("HELM_PLUGINS") } diff --git a/pkg/plugin/installer/http_installer.go b/pkg/plugin/installer/http_installer.go index bcbcbde93..09d5d1754 100644 --- a/pkg/plugin/installer/http_installer.go +++ b/pkg/plugin/installer/http_installer.go @@ -82,7 +82,7 @@ func NewExtractor(source string) (Extractor, error) { } // NewHTTPInstaller creates a new HttpInstaller. -func NewHTTPInstaller(source string) (*HTTPInstaller, error) { +func NewHTTPInstaller(source, pluginsDirectory string) (*HTTPInstaller, error) { key, err := cache.Key(source) if err != nil { return nil, err @@ -101,7 +101,7 @@ func NewHTTPInstaller(source string) (*HTTPInstaller, error) { i := &HTTPInstaller{ CacheDir: helmpath.CachePath("plugins", key), PluginName: stripPluginName(filepath.Base(source)), - base: newBase(source), + base: newBase(source, pluginsDirectory), extractor: extractor, getter: get, } diff --git a/pkg/plugin/installer/http_installer_test.go b/pkg/plugin/installer/http_installer_test.go index e89fea29d..7268e361f 100644 --- a/pkg/plugin/installer/http_installer_test.go +++ b/pkg/plugin/installer/http_installer_test.go @@ -33,6 +33,7 @@ import ( "github.com/pkg/errors" "helm.sh/helm/v3/internal/test/ensure" + "helm.sh/helm/v3/pkg/cli" "helm.sh/helm/v3/pkg/getter" "helm.sh/helm/v3/pkg/helmpath" ) @@ -83,6 +84,7 @@ func TestHTTPInstaller(t *testing.T) { defer ensure.HelmHome(t)() srv := mockArchiveServer() + settings := cli.New() defer srv.Close() source := srv.URL + "/plugins/fake-plugin-0.0.1.tar.gz" @@ -90,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") + i, err := NewForSource(source, "0.0.1", settings.PluginsDirectory) if err != nil { t.Fatalf("unexpected error: %s", err) } @@ -130,6 +132,7 @@ func TestHTTPInstaller(t *testing.T) { func TestHTTPInstallerNonExistentVersion(t *testing.T) { defer ensure.HelmHome(t)() + settings := cli.New() srv := mockArchiveServer() defer srv.Close() source := srv.URL + "/plugins/fake-plugin-0.0.1.tar.gz" @@ -138,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") + i, err := NewForSource(source, "0.0.2", settings.PluginsDirectory) if err != nil { t.Fatalf("unexpected error: %s", err) } @@ -163,6 +166,7 @@ func TestHTTPInstallerNonExistentVersion(t *testing.T) { func TestHTTPInstallerUpdate(t *testing.T) { srv := mockArchiveServer() + settings := cli.New() defer srv.Close() source := srv.URL + "/plugins/fake-plugin-0.0.1.tar.gz" defer ensure.HelmHome(t)() @@ -171,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") + i, err := NewForSource(source, "0.0.1", settings.PluginsDirectory) if err != nil { t.Fatalf("unexpected error: %s", err) } diff --git a/pkg/plugin/installer/installer.go b/pkg/plugin/installer/installer.go index 6f01494e5..9cf28a962 100644 --- a/pkg/plugin/installer/installer.go +++ b/pkg/plugin/installer/installer.go @@ -64,19 +64,19 @@ func Update(i Installer) error { } // NewForSource determines the correct Installer for the given source. -func NewForSource(source, version string) (Installer, error) { +func NewForSource(source, version, pluginsDirectory string) (Installer, error) { // Check if source is a local directory if isLocalReference(source) { - return NewLocalInstaller(source) + return NewLocalInstaller(source, pluginsDirectory) } else if isRemoteHTTPArchive(source) { - return NewHTTPInstaller(source) + return NewHTTPInstaller(source, pluginsDirectory) } - return NewVCSInstaller(source, version) + return NewVCSInstaller(source, version, pluginsDirectory) } // FindSource determines the correct Installer for the given source. -func FindSource(location string) (Installer, error) { - installer, err := existingVCSRepo(location) +func FindSource(location, pluginsDirectory string) (Installer, error) { + installer, err := existingVCSRepo(location, pluginsDirectory) if err != nil && err.Error() == "Cannot detect VCS" { return installer, errors.New("cannot get information about plugin source") } diff --git a/pkg/plugin/installer/local_installer.go b/pkg/plugin/installer/local_installer.go index c92bc3fb0..d6c238242 100644 --- a/pkg/plugin/installer/local_installer.go +++ b/pkg/plugin/installer/local_installer.go @@ -28,13 +28,13 @@ type LocalInstaller struct { } // NewLocalInstaller creates a new LocalInstaller. -func NewLocalInstaller(source string) (*LocalInstaller, error) { +func NewLocalInstaller(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), + base: newBase(src, pluginsDirectory), } return i, nil } diff --git a/pkg/plugin/installer/local_installer_test.go b/pkg/plugin/installer/local_installer_test.go index 0d3de11d1..0c4d0ed35 100644 --- a/pkg/plugin/installer/local_installer_test.go +++ b/pkg/plugin/installer/local_installer_test.go @@ -21,6 +21,7 @@ import ( "path/filepath" "testing" + "helm.sh/helm/v3/pkg/cli" "helm.sh/helm/v3/pkg/helmpath" ) @@ -32,13 +33,14 @@ func TestLocalInstaller(t *testing.T) { if err != nil { t.Fatal(err) } + settings := cli.New() defer os.RemoveAll(tdir) if err := ioutil.WriteFile(filepath.Join(tdir, "plugin.yaml"), []byte{}, 0644); err != nil { t.Fatal(err) } source := "../testdata/plugdir/good/echo" - i, err := NewForSource(source, "") + i, err := NewForSource(source, "", settings.PluginsDirectory) if err != nil { t.Fatalf("unexpected error: %s", err) } diff --git a/pkg/plugin/installer/vcs_installer.go b/pkg/plugin/installer/vcs_installer.go index f7df5b322..8217f1325 100644 --- a/pkg/plugin/installer/vcs_installer.go +++ b/pkg/plugin/installer/vcs_installer.go @@ -35,20 +35,20 @@ type VCSInstaller struct { base } -func existingVCSRepo(location string) (Installer, error) { +func existingVCSRepo(location, pluginsDirectory string) (Installer, error) { repo, err := vcs.NewRepo("", location) if err != nil { return nil, err } i := &VCSInstaller{ Repo: repo, - base: newBase(repo.Remote()), + base: newBase(repo.Remote(), pluginsDirectory), } return i, nil } // NewVCSInstaller creates a new VCSInstaller. -func NewVCSInstaller(source, version string) (*VCSInstaller, error) { +func NewVCSInstaller(source, version, pluginsDirectory string) (*VCSInstaller, error) { key, err := cache.Key(source) if err != nil { return nil, err @@ -61,7 +61,7 @@ func NewVCSInstaller(source, version string) (*VCSInstaller, error) { i := &VCSInstaller{ Repo: repo, Version: version, - base: newBase(source), + base: newBase(source, pluginsDirectory), } return i, err } diff --git a/pkg/plugin/installer/vcs_installer_test.go b/pkg/plugin/installer/vcs_installer_test.go index 6785264b3..72e7e0ec3 100644 --- a/pkg/plugin/installer/vcs_installer_test.go +++ b/pkg/plugin/installer/vcs_installer_test.go @@ -24,6 +24,7 @@ import ( "github.com/Masterminds/vcs" "helm.sh/helm/v3/internal/test/ensure" + "helm.sh/helm/v3/pkg/cli" "helm.sh/helm/v3/pkg/helmpath" ) @@ -50,6 +51,7 @@ func (r *testRepo) UpdateVersion(version string) error { func TestVCSInstaller(t *testing.T) { defer ensure.HelmHome(t)() + settings := cli.New() if err := os.MkdirAll(helmpath.DataPath("plugins"), 0755); err != nil { t.Fatalf("Could not create %s: %s", helmpath.DataPath("plugins"), err) @@ -62,7 +64,7 @@ func TestVCSInstaller(t *testing.T) { tags: []string{"0.1.0", "0.1.1"}, } - i, err := NewForSource(source, "~0.1.0") + i, err := NewForSource(source, "~0.1.0", settings.PluginsDirectory) if err != nil { t.Fatalf("unexpected error: %s", err) } @@ -94,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()); err == nil { + if _, err := FindSource(i.Path(), settings.PluginsDirectory); 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) @@ -106,8 +108,9 @@ func TestVCSInstallerNonExistentVersion(t *testing.T) { source := "https://github.com/adamreese/helm-env" version := "0.2.0" + settings := cli.New() - i, err := NewForSource(source, version) + i, err := NewForSource(source, version, settings.PluginsDirectory) if err != nil { t.Fatalf("unexpected error: %s", err) } @@ -127,8 +130,9 @@ func TestVCSInstallerUpdate(t *testing.T) { defer ensure.HelmHome(t)() source := "https://github.com/adamreese/helm-env" + settings := cli.New() - i, err := NewForSource(source, "") + i, err := NewForSource(source, "", settings.PluginsDirectory) if err != nil { t.Fatalf("unexpected error: %s", err) } @@ -150,7 +154,7 @@ func TestVCSInstallerUpdate(t *testing.T) { } // Test FindSource method for positive result - pluginInfo, err := FindSource(i.Path()) + pluginInfo, err := FindSource(i.Path(), settings.PluginsDirectory) if err != nil { t.Fatal(err) }