add PluginsDirectory in base struct

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

@ -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
}

@ -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
}

@ -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))
}

@ -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")
}

@ -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,
}

@ -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)
}

@ -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")
}

@ -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
}

@ -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)
}

@ -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
}

@ -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)
}

Loading…
Cancel
Save