pull/3268/merge
Federico Gimenez 8 years ago committed by GitHub
commit 91c619b790
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -90,7 +90,9 @@ Restrictions on `name`:
The `ignoreFlags` switch tells Helm to _not_ pass flags to the plugin. So if a The `ignoreFlags` switch tells Helm to _not_ pass flags to the plugin. So if a
plugin is called with `helm myplugin --foo` and `ignoreFlags: true`, then `--foo` plugin is called with `helm myplugin --foo` and `ignoreFlags: true`, then `--foo`
is silently discarded. is silently discarded. The arguments provided until the first tag are kept, so
`helm myplugin myarg --foo` and `ignoreFlags: true` will be equivalent to `helm
myplugin myarg`.
The `useTunnel` switch indicates that the plugin needs a tunnel to Tiller. This The `useTunnel` switch indicates that the plugin needs a tunnel to Tiller. This
should be set to `true` _anytime a plugin talks to Tiller_. It will cause Helm should be set to `true` _anytime a plugin talks to Tiller_. It will cause Helm

@ -105,9 +105,18 @@ func (p *Plugin) PrepareCommand(extraArgs []string) (string, []string) {
if len(parts) > 1 { if len(parts) > 1 {
baseArgs = parts[1:] baseArgs = parts[1:]
} }
if !p.Metadata.IgnoreFlags { extraIndex := len(extraArgs)
baseArgs = append(baseArgs, extraArgs...) // determine the index of the first flag if we need to ignore them
if p.Metadata.IgnoreFlags {
for i, arg := range extraArgs {
if strings.HasPrefix(arg, "-") {
extraIndex = i
break
}
}
} }
baseArgs = append(baseArgs, extraArgs[0:extraIndex]...)
return main, baseArgs return main, baseArgs
} }

@ -28,38 +28,71 @@ func TestPrepareCommand(t *testing.T) {
Command: "echo -n foo", Command: "echo -n foo",
}, },
} }
argv := []string{"--debug", "--foo", "bar"} testCases := []struct {
description string
cmd, args := p.PrepareCommand(argv) argv []string
if cmd != "echo" { expect []string
t.Errorf("Expected echo, got %q", cmd) ignoreFlags bool
} }{
{
if l := len(args); l != 5 { description: "no extra args",
t.Errorf("expected 5 args, got %d", l) argv: []string{},
} expect: []string{"-n", "foo"},
},
expect := []string{"-n", "foo", "--debug", "--foo", "bar"} {
for i := 0; i < len(args); i++ { description: "extra args, all long flags, keep flags",
if expect[i] != args[i] { argv: []string{"--debug", "--foo", "bar"},
t.Errorf("Expected arg=%q, got %q", expect[i], args[i]) expect: []string{"-n", "foo", "--debug", "--foo", "bar"},
} },
} {
description: "extra args, all long flags, ignore flags",
// Test with IgnoreFlags. This should omit --debug, --foo, bar argv: []string{"--debug", "--foo", "bar"},
p.Metadata.IgnoreFlags = true expect: []string{"-n", "foo"},
cmd, args = p.PrepareCommand(argv) ignoreFlags: true,
if cmd != "echo" { },
t.Errorf("Expected echo, got %q", cmd) {
} description: "extra args, arguments, long flags, ignore flags",
if l := len(args); l != 2 { argv: []string{"arg1", "arg2", "--debug", "--foo", "bar", "arg3"},
t.Errorf("expected 2 args, got %d", l) expect: []string{"-n", "foo", "arg1", "arg2"},
ignoreFlags: true,
},
{
description: "extra args, short (first) and long flags, ignore flags",
argv: []string{"-s", "--debug", "--foo", "bar", "arg3"},
expect: []string{"-n", "foo"},
ignoreFlags: true,
},
{
description: "extra args, arguments, short (first) and long flags, ignore flags",
argv: []string{"arg1", "arg2", "-d", "--foo", "bar", "arg3"},
expect: []string{"-n", "foo", "arg1", "arg2"},
ignoreFlags: true,
},
{
description: "extra args, long (first) and short flags, ignore flags",
argv: []string{"--debug", "-s", "--foo", "bar", "arg3"},
expect: []string{"-n", "foo"},
ignoreFlags: true,
},
} }
expect = []string{"-n", "foo"} for _, tc := range testCases {
for i := 0; i < len(args); i++ { t.Run(tc.description, func(t *testing.T) {
if expect[i] != args[i] { p.Metadata.IgnoreFlags = tc.ignoreFlags
t.Errorf("Expected arg=%q, got %q", expect[i], args[i]) cmd, args := p.PrepareCommand(tc.argv)
} if cmd != "echo" {
t.Errorf("Expected echo, got %q", cmd)
}
if l, le := len(args), len(tc.expect); l != le {
t.Errorf("expected %d args, got %d", le, l)
}
for i := 0; i < len(args); i++ {
if tc.expect[i] != args[i] {
t.Errorf("Expected arg=%q, got %q", tc.expect[i], args[i])
}
}
})
} }
} }

Loading…
Cancel
Save