Handle aliases in comma-separated args

This commit is contained in:
kargakis 2015-07-20 10:33:50 +02:00
parent ddda661991
commit 4fcb4b7f8d
2 changed files with 23 additions and 33 deletions

View File

@ -256,7 +256,6 @@ func (b *Builder) SelectAllParam(selectAll bool) *Builder {
// When two or more arguments are received, they must be a single type and resource name(s).
// The allowEmptySelector permits to select all the resources (via Everything func).
func (b *Builder) ResourceTypeOrNameArgs(allowEmptySelector bool, args ...string) *Builder {
args = b.replaceAliases(args)
if ok, err := hasCombinedTypeArgs(args); ok {
if err != nil {
b.errs = append(b.errs, err)
@ -277,6 +276,10 @@ func (b *Builder) ResourceTypeOrNameArgs(allowEmptySelector bool, args ...string
}
return b
}
if len(args) > 0 {
// Try replacing aliases only in types
args[0] = b.replaceAliases(args[0])
}
switch {
case len(args) > 2:
b.names = append(b.names, args[1:]...)
@ -296,16 +299,17 @@ func (b *Builder) ResourceTypeOrNameArgs(allowEmptySelector bool, args ...string
return b
}
func (b *Builder) replaceAliases(args []string) []string {
// replaceAliases accepts an argument and tries to expand any existing
// aliases found in it
func (b *Builder) replaceAliases(input string) string {
replaced := []string{}
for _, arg := range args {
for _, arg := range strings.Split(input, ",") {
if aliases, ok := b.mapper.AliasesForResource(arg); ok {
arg = strings.Join(aliases, ",")
}
replaced = append(replaced, arg)
}
return replaced
return strings.Join(replaced, ",")
}
func hasCombinedTypeArgs(args []string) (bool, error) {
@ -325,20 +329,6 @@ func hasCombinedTypeArgs(args []string) (bool, error) {
}
}
// ResourceTypeAndNameArgs expects two arguments, a resource type, and a resource name. The resource
// matching that type and and name will be retrieved from the server.
func (b *Builder) ResourceTypeAndNameArgs(args ...string) *Builder {
switch len(args) {
case 2:
b.names = append(b.names, args[1])
b.ResourceTypes(SplitResourceArgument(args[0])...)
case 0:
default:
b.errs = append(b.errs, fmt.Errorf("when passing arguments, must be resource and name"))
}
return b
}
// Flatten will convert any objects with a field named "Items" that is an array of runtime.Object
// compatible types into individual entries and give them their own items. The original object
// is not passed to any visitors.

View File

@ -923,32 +923,32 @@ func TestReceiveMultipleErrors(t *testing.T) {
func TestReplaceAliases(t *testing.T) {
tests := []struct {
name string
args []string
expected []string
arg string
expected string
}{
{
name: "no-replacement",
args: []string{"service", "pods", "rc"},
expected: []string{"service", "pods", "rc"},
arg: "service",
expected: "service",
},
{
name: "all-replacement",
args: []string{"all"},
expected: []string{"rc,svc,pods,pvc"},
arg: "all",
expected: "rc,svc,pods,pvc",
},
{
name: "alias-in-comma-separated-arg",
arg: "all,secrets",
expected: "rc,svc,pods,pvc,secrets",
},
}
b := NewBuilder(latest.RESTMapper, api.Scheme, fakeClient())
for _, test := range tests {
replaced := b.replaceAliases(test.args)
if len(replaced) != len(test.expected) {
t.Errorf("%s: unexpected args length: expected %d, got %d", test.name, len(test.expected), len(replaced))
}
for i, arg := range test.expected {
if arg != replaced[i] {
t.Errorf("%s: unexpected argument: expected %s, got %s", test.name, arg, replaced[i])
}
replaced := b.replaceAliases(test.arg)
if replaced != test.expected {
t.Errorf("%s: unexpected argument: expected %s, got %s", test.name, test.expected, replaced)
}
}
}