1
0
mirror of https://github.com/rancher/cli.git synced 2026-02-05 18:48:50 +01:00

Update inspect to be case insensitive

Problem:
Inspect requires a type to be camelcase if that's how it shows in the
schema

Solution:
Make inspect note care about case
This commit is contained in:
Dan Ramich
2018-05-09 15:06:18 -07:00
committed by Denise
parent c8a4601aff
commit ab6d47bca0
2 changed files with 49 additions and 9 deletions

View File

@@ -253,20 +253,56 @@ func GetClient(ctx *cli.Context) (*cliclient.MasterClient, error) {
return mc, nil
}
// GetResourceType maps an incoming resource type to a valid one from the schema
func GetResourceType(c *cliclient.MasterClient, resource string) (string, error) {
if c.ManagementClient != nil {
for key := range c.ManagementClient.APIBaseClient.Types {
if strings.ToLower(key) == strings.ToLower(resource) {
return key, nil
}
}
}
if c.ProjectClient != nil {
for key := range c.ProjectClient.APIBaseClient.Types {
if strings.ToLower(key) == strings.ToLower(resource) {
return key, nil
}
}
}
if c.ClusterClient != nil {
for key := range c.ClusterClient.APIBaseClient.Types {
if strings.ToLower(key) == strings.ToLower(resource) {
return key, nil
}
}
}
return "", fmt.Errorf("unknown resource type: %s", resource)
}
func Lookup(c *cliclient.MasterClient, name string, types ...string) (*ntypes.Resource, error) {
var byName *ntypes.Resource
for _, schemaType := range types {
rt, err := GetResourceType(c, schemaType)
if err != nil {
return nil, err
}
var schemaClient clientbase.APIBaseClientInterface
// the schemaType dictates which client we need to use
if _, ok := c.ManagementClient.APIBaseClient.Types[schemaType]; ok {
schemaClient = c.ManagementClient
} else if _, ok := c.ProjectClient.APIBaseClient.Types[schemaType]; ok {
schemaClient = c.ProjectClient
} else if _, ok := c.ClusterClient.APIBaseClient.Types[schemaType]; ok {
schemaClient = c.ClusterClient
} else {
return nil, errors.New("unknown resource type")
if c.ManagementClient != nil {
if _, ok := c.ManagementClient.APIBaseClient.Types[rt]; ok {
schemaClient = c.ManagementClient
}
}
if c.ProjectClient != nil {
if _, ok := c.ProjectClient.APIBaseClient.Types[rt]; ok {
schemaClient = c.ProjectClient
}
}
if c.ClusterClient != nil {
if _, ok := c.ClusterClient.APIBaseClient.Types[rt]; ok {
schemaClient = c.ClusterClient
}
}
// Attempt to get the resource by ID

View File

@@ -56,7 +56,11 @@ func inspectResources(ctx *cli.Context) error {
t := ctx.String("type")
types := []string{}
if t != "" {
types = append(types, t)
rt, err := GetResourceType(c, t)
if err != nil {
return err
}
types = append(types, rt)
} else {
types = listAllRoles()
}