mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-08-04 09:49:50 +00:00
Handle aliases in comma-separated args
This commit is contained in:
parent
ddda661991
commit
4fcb4b7f8d
@ -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).
|
// 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).
|
// The allowEmptySelector permits to select all the resources (via Everything func).
|
||||||
func (b *Builder) ResourceTypeOrNameArgs(allowEmptySelector bool, args ...string) *Builder {
|
func (b *Builder) ResourceTypeOrNameArgs(allowEmptySelector bool, args ...string) *Builder {
|
||||||
args = b.replaceAliases(args)
|
|
||||||
if ok, err := hasCombinedTypeArgs(args); ok {
|
if ok, err := hasCombinedTypeArgs(args); ok {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
b.errs = append(b.errs, err)
|
b.errs = append(b.errs, err)
|
||||||
@ -277,6 +276,10 @@ func (b *Builder) ResourceTypeOrNameArgs(allowEmptySelector bool, args ...string
|
|||||||
}
|
}
|
||||||
return b
|
return b
|
||||||
}
|
}
|
||||||
|
if len(args) > 0 {
|
||||||
|
// Try replacing aliases only in types
|
||||||
|
args[0] = b.replaceAliases(args[0])
|
||||||
|
}
|
||||||
switch {
|
switch {
|
||||||
case len(args) > 2:
|
case len(args) > 2:
|
||||||
b.names = append(b.names, args[1:]...)
|
b.names = append(b.names, args[1:]...)
|
||||||
@ -296,16 +299,17 @@ func (b *Builder) ResourceTypeOrNameArgs(allowEmptySelector bool, args ...string
|
|||||||
return b
|
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{}
|
replaced := []string{}
|
||||||
for _, arg := range args {
|
for _, arg := range strings.Split(input, ",") {
|
||||||
if aliases, ok := b.mapper.AliasesForResource(arg); ok {
|
if aliases, ok := b.mapper.AliasesForResource(arg); ok {
|
||||||
arg = strings.Join(aliases, ",")
|
arg = strings.Join(aliases, ",")
|
||||||
}
|
}
|
||||||
replaced = append(replaced, arg)
|
replaced = append(replaced, arg)
|
||||||
}
|
}
|
||||||
|
return strings.Join(replaced, ",")
|
||||||
return replaced
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func hasCombinedTypeArgs(args []string) (bool, error) {
|
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
|
// 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
|
// compatible types into individual entries and give them their own items. The original object
|
||||||
// is not passed to any visitors.
|
// is not passed to any visitors.
|
||||||
|
@ -923,32 +923,32 @@ func TestReceiveMultipleErrors(t *testing.T) {
|
|||||||
func TestReplaceAliases(t *testing.T) {
|
func TestReplaceAliases(t *testing.T) {
|
||||||
tests := []struct {
|
tests := []struct {
|
||||||
name string
|
name string
|
||||||
args []string
|
arg string
|
||||||
expected []string
|
expected string
|
||||||
}{
|
}{
|
||||||
{
|
{
|
||||||
name: "no-replacement",
|
name: "no-replacement",
|
||||||
args: []string{"service", "pods", "rc"},
|
arg: "service",
|
||||||
expected: []string{"service", "pods", "rc"},
|
expected: "service",
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "all-replacement",
|
name: "all-replacement",
|
||||||
args: []string{"all"},
|
arg: "all",
|
||||||
expected: []string{"rc,svc,pods,pvc"},
|
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())
|
b := NewBuilder(latest.RESTMapper, api.Scheme, fakeClient())
|
||||||
|
|
||||||
for _, test := range tests {
|
for _, test := range tests {
|
||||||
replaced := b.replaceAliases(test.args)
|
replaced := b.replaceAliases(test.arg)
|
||||||
if len(replaced) != len(test.expected) {
|
if replaced != test.expected {
|
||||||
t.Errorf("%s: unexpected args length: expected %d, got %d", test.name, len(test.expected), len(replaced))
|
t.Errorf("%s: unexpected argument: expected %s, got %s", test.name, test.expected, 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])
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user