From 1064934105b22450362bb85d963f4afe6578910b Mon Sep 17 00:00:00 2001 From: steven-sheehy Date: Sat, 13 Jul 2019 00:02:45 -0500 Subject: [PATCH] Add sub-command support to plugin downloader Signed-off-by: steven-sheehy --- docs/plugins.md | 5 +++++ pkg/getter/plugingetter.go | 6 ++++-- pkg/getter/plugingetter_test.go | 28 ++++++++++++++++++++++++++++ 3 files changed, 37 insertions(+), 2 deletions(-) diff --git a/docs/plugins.md b/docs/plugins.md index babab3f07..23bd9a3b4 100644 --- a/docs/plugins.md +++ b/docs/plugins.md @@ -170,6 +170,11 @@ The defined command will be invoked with the following scheme: repo definition, stored in `$HELM_HOME/repository/repositories.yaml`. Downloader plugin is expected to dump the raw content to stdout and report errors on stderr. +The downloader command also supports sub-commands or arguments, allowing you to specify +for example `bin/mydownloader subcommand -d` in the `plugin.yaml`. This is useful +if you want to use the same executable for the main plugin command and the downloader +command, but with a different sub-command for each. + ## Environment Variables When Helm executes a plugin, it passes the outer environment to the plugin, and diff --git a/pkg/getter/plugingetter.go b/pkg/getter/plugingetter.go index fff5193b2..77f09f22d 100644 --- a/pkg/getter/plugingetter.go +++ b/pkg/getter/plugingetter.go @@ -20,6 +20,7 @@ import ( "os" "os/exec" "path/filepath" + "strings" "github.com/pkg/errors" @@ -63,8 +64,9 @@ type pluginGetter struct { // Get runs downloader plugin command func (p *pluginGetter) Get(href string) (*bytes.Buffer, error) { - argv := []string{p.opts.certFile, p.opts.keyFile, p.opts.caFile, href} - prog := exec.Command(filepath.Join(p.base, p.command), argv...) + commands := strings.Split(p.command, " ") + argv := append(commands[1:], p.opts.certFile, p.opts.keyFile, p.opts.caFile, href) + prog := exec.Command(filepath.Join(p.base, commands[0]), argv...) plugin.SetupPluginEnv(p.settings, p.name, p.base) prog.Env = os.Environ() buf := bytes.NewBuffer(nil) diff --git a/pkg/getter/plugingetter_test.go b/pkg/getter/plugingetter_test.go index 8c3ec6e51..341b88e7a 100644 --- a/pkg/getter/plugingetter_test.go +++ b/pkg/getter/plugingetter_test.go @@ -94,3 +94,31 @@ func TestPluginGetter(t *testing.T) { t.Errorf("Expected %q, got %q", expect, got) } } + +func TestPluginSubCommands(t *testing.T) { + if runtime.GOOS == "windows" { + t.Skip("TODO: refactor this test to work on windows") + } + + oldhh := os.Getenv("HELM_HOME") + defer os.Setenv("HELM_HOME", oldhh) + os.Setenv("HELM_HOME", "") + + env := hh(false) + pg := newPluginGetter("echo -n", env, "test", ".") + g, err := pg() + if err != nil { + t.Fatal(err) + } + + data, err := g.Get("test://foo/bar") + if err != nil { + t.Fatal(err) + } + + expect := " test://foo/bar" + got := data.String() + if got != expect { + t.Errorf("Expected %q, got %q", expect, got) + } +}