mirror of
https://github.com/rancher/cli.git
synced 2026-02-05 09:48:36 +01:00
Use staticcheck linter (#511)
- Enable staticcheck linter - Fix identified issues
This commit is contained in:
committed by
GitHub
parent
5cf2106b2d
commit
e4c778e8dd
@@ -9,6 +9,7 @@
|
||||
"enable": [
|
||||
"govet",
|
||||
"ineffassign",
|
||||
"staticcheck",
|
||||
"unused"
|
||||
]
|
||||
},
|
||||
|
||||
@@ -162,11 +162,11 @@ func (mc *MasterClient) newCAPIClient() error {
|
||||
func (mc *MasterClient) ByID(resource *ntypes.Resource, respObject interface{}) error {
|
||||
if strings.HasPrefix(resource.Type, "cluster.x-k8s.io") {
|
||||
return mc.CAPIClient.ByID(resource.Type, resource.ID, &respObject)
|
||||
} else if _, ok := mc.ManagementClient.APIBaseClient.Types[resource.Type]; ok {
|
||||
} else if _, ok := mc.ManagementClient.Types[resource.Type]; ok {
|
||||
return mc.ManagementClient.ByID(resource.Type, resource.ID, &respObject)
|
||||
} else if _, ok := mc.ProjectClient.APIBaseClient.Types[resource.Type]; ok {
|
||||
} else if _, ok := mc.ProjectClient.Types[resource.Type]; ok {
|
||||
return mc.ProjectClient.ByID(resource.Type, resource.ID, &respObject)
|
||||
} else if _, ok := mc.ClusterClient.APIBaseClient.Types[resource.Type]; ok {
|
||||
} else if _, ok := mc.ClusterClient.Types[resource.Type]; ok {
|
||||
return mc.ClusterClient.ByID(resource.Type, resource.ID, &respObject)
|
||||
}
|
||||
return fmt.Errorf("MasterClient - unknown resource type %v", resource.Type)
|
||||
|
||||
@@ -742,7 +742,7 @@ func getClusterRAM(cluster managementClient.Cluster) string {
|
||||
// parseResourceString returns GB for Ki and Mi and CPU cores from 'm'
|
||||
func parseResourceString(mem string) string {
|
||||
if strings.HasSuffix(mem, "Ki") {
|
||||
num, err := strconv.ParseFloat(strings.Replace(mem, "Ki", "", -1), 64)
|
||||
num, err := strconv.ParseFloat(strings.ReplaceAll(mem, "Ki", ""), 64)
|
||||
if err != nil {
|
||||
return mem
|
||||
}
|
||||
@@ -750,7 +750,7 @@ func parseResourceString(mem string) string {
|
||||
return strings.TrimSuffix(fmt.Sprintf("%.2f", num), ".0")
|
||||
}
|
||||
if strings.HasSuffix(mem, "Mi") {
|
||||
num, err := strconv.ParseFloat(strings.Replace(mem, "Mi", "", -1), 64)
|
||||
num, err := strconv.ParseFloat(strings.ReplaceAll(mem, "Mi", ""), 64)
|
||||
if err != nil {
|
||||
return mem
|
||||
}
|
||||
@@ -758,7 +758,7 @@ func parseResourceString(mem string) string {
|
||||
return strings.TrimSuffix(fmt.Sprintf("%.2f", num), ".0")
|
||||
}
|
||||
if strings.HasSuffix(mem, "m") {
|
||||
num, err := strconv.ParseFloat(strings.Replace(mem, "m", "", -1), 64)
|
||||
num, err := strconv.ParseFloat(strings.ReplaceAll(mem, "m", ""), 64)
|
||||
if err != nil {
|
||||
return mem
|
||||
}
|
||||
|
||||
@@ -261,7 +261,7 @@ func loadAndVerifyCert(path string) (string, error) {
|
||||
|
||||
func verifyCert(caCert []byte) (string, error) {
|
||||
// replace the escaped version of the line break
|
||||
caCert = bytes.Replace(caCert, []byte(`\n`), []byte("\n"), -1)
|
||||
caCert = bytes.ReplaceAll(caCert, []byte(`\n`), []byte("\n"))
|
||||
|
||||
block, _ := pem.Decode(caCert)
|
||||
|
||||
@@ -322,28 +322,28 @@ func GetClient(ctx *cli.Context) (*cliclient.MasterClient, error) {
|
||||
// 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 {
|
||||
for key := range c.ManagementClient.Types {
|
||||
if strings.EqualFold(key, resource) {
|
||||
return key, nil
|
||||
}
|
||||
}
|
||||
}
|
||||
if c.ProjectClient != nil {
|
||||
for key := range c.ProjectClient.APIBaseClient.Types {
|
||||
for key := range c.ProjectClient.Types {
|
||||
if strings.EqualFold(key, resource) {
|
||||
return key, nil
|
||||
}
|
||||
}
|
||||
}
|
||||
if c.ClusterClient != nil {
|
||||
for key := range c.ClusterClient.APIBaseClient.Types {
|
||||
for key := range c.ClusterClient.Types {
|
||||
if strings.EqualFold(key, resource) {
|
||||
return key, nil
|
||||
}
|
||||
}
|
||||
}
|
||||
if c.CAPIClient != nil {
|
||||
for key := range c.CAPIClient.APIBaseClient.Types {
|
||||
for key := range c.CAPIClient.Types {
|
||||
lowerKey := strings.ToLower(key)
|
||||
if strings.HasPrefix(lowerKey, "cluster.x-k8s.io") && lowerKey == strings.ToLower(resource) {
|
||||
return key, nil
|
||||
@@ -370,17 +370,17 @@ func Lookup(c *cliclient.MasterClient, name string, types ...string) (*ntypes.Re
|
||||
}
|
||||
}
|
||||
if c.ManagementClient != nil {
|
||||
if _, ok := c.ManagementClient.APIBaseClient.Types[rt]; ok {
|
||||
if _, ok := c.ManagementClient.Types[rt]; ok {
|
||||
schemaClient = c.ManagementClient
|
||||
}
|
||||
}
|
||||
if c.ProjectClient != nil {
|
||||
if _, ok := c.ProjectClient.APIBaseClient.Types[rt]; ok {
|
||||
if _, ok := c.ProjectClient.Types[rt]; ok {
|
||||
schemaClient = c.ProjectClient
|
||||
}
|
||||
}
|
||||
if c.ClusterClient != nil {
|
||||
if _, ok := c.ClusterClient.APIBaseClient.Types[rt]; ok {
|
||||
if _, ok := c.ClusterClient.Types[rt]; ok {
|
||||
schemaClient = c.ClusterClient
|
||||
}
|
||||
}
|
||||
@@ -415,7 +415,7 @@ func Lookup(c *cliclient.MasterClient, name string, types ...string) (*ntypes.Re
|
||||
for _, data := range collection.Data {
|
||||
ids = append(ids, data.ID)
|
||||
}
|
||||
return nil, fmt.Errorf("Multiple resources of type %s found for name %s: %v", schemaType, name, ids)
|
||||
return nil, fmt.Errorf("multiple resources of type %s found for name %s: %v", schemaType, name, ids)
|
||||
}
|
||||
|
||||
// No matches for this schemaType, try the next one
|
||||
@@ -424,7 +424,7 @@ func Lookup(c *cliclient.MasterClient, name string, types ...string) (*ntypes.Re
|
||||
}
|
||||
|
||||
if byName != nil {
|
||||
return nil, fmt.Errorf("Multiple resources named %s: %s:%s, %s:%s", name, collection.Data[0].Type,
|
||||
return nil, fmt.Errorf("multiple resources named %s: %s:%s, %s:%s", name, collection.Data[0].Type,
|
||||
collection.Data[0].ID, byName.Type, byName.ID)
|
||||
}
|
||||
|
||||
@@ -433,7 +433,7 @@ func Lookup(c *cliclient.MasterClient, name string, types ...string) (*ntypes.Re
|
||||
}
|
||||
|
||||
if byName == nil {
|
||||
return nil, fmt.Errorf("Not found: %s", name)
|
||||
return nil, fmt.Errorf("not found: %s", name)
|
||||
}
|
||||
|
||||
return byName, nil
|
||||
@@ -546,7 +546,7 @@ func parseClusterAndProjectID(id string) (string, string, error) {
|
||||
parts := SplitOnColon(id)
|
||||
return parts[0], parts[1], nil
|
||||
}
|
||||
return "", "", fmt.Errorf("Unable to extract clusterid and projectid from [%s]", id)
|
||||
return "", "", fmt.Errorf("unable to extract clusterid and projectid from [%s]", id)
|
||||
}
|
||||
|
||||
// Return a JSON blob of the file at path
|
||||
|
||||
@@ -241,7 +241,7 @@ func loadCachedCredential(ctx *cli.Context, serverConfig *config.ServerConfig, k
|
||||
return cred, nil
|
||||
}
|
||||
ts := cred.Status.ExpirationTimestamp
|
||||
if ts != nil && ts.Time.Before(time.Now()) {
|
||||
if ts != nil && ts.Before(time.Now()) {
|
||||
cf, err := loadConfig(ctx)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
|
||||
@@ -165,5 +165,5 @@ func TestCacheCredential(t *testing.T) {
|
||||
|
||||
expirationTimestamp := cfg.Servers["rancher.example.com"].KubeCredentials["dev-server"].Status.ExpirationTimestamp
|
||||
require.NotNil(t, expirationTimestamp)
|
||||
assert.True(t, expirationTimestamp.Time.Equal(expires.Time))
|
||||
assert.True(t, expirationTimestamp.Equal(expires.Time))
|
||||
}
|
||||
|
||||
13
cmd/login.go
13
cmd/login.go
@@ -85,7 +85,7 @@ func loginSetup(ctx *cli.Context) error {
|
||||
// Validate the url and drop the path
|
||||
u, err := url.ParseRequestURI(ctx.Args().First())
|
||||
if err != nil {
|
||||
return fmt.Errorf("Failed to parse SERVERURL (%s), make sure it is a valid HTTPS URL (e.g. https://rancher.yourdomain.com or https://1.1.1.1). Error: %s", ctx.Args().First(), err)
|
||||
return fmt.Errorf("failed to parse SERVERURL (%s), make sure it is a valid HTTPS URL (e.g. https://rancher.yourdomain.com or https://1.1.1.1). Error: %s", ctx.Args().First(), err)
|
||||
}
|
||||
|
||||
u.Path = ""
|
||||
@@ -152,12 +152,12 @@ func getProjectContext(ctx *cli.Context, c *cliclient.MasterClient) (string, err
|
||||
// Check if given context is in valid format
|
||||
_, _, err := parseClusterAndProjectID(context)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("Unable to parse context (%s). Please provide context as local:p-xxxxx, c-xxxxx:p-xxxxx, c-xxxxx:project-xxxxx, c-m-xxxxxxxx:p-xxxxx or c-m-xxxxxxxx:project-xxxxx", context)
|
||||
return "", fmt.Errorf("unable to parse context (%s). Please provide context as local:p-xxxxx, c-xxxxx:p-xxxxx, c-xxxxx:project-xxxxx, c-m-xxxxxxxx:p-xxxxx or c-m-xxxxxxxx:project-xxxxx", context)
|
||||
}
|
||||
// Check if context exists
|
||||
_, err = Lookup(c, context, "project")
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("Unable to find context (%s). Make sure the context exists and you have permissions to use it. Error: %s", context, err)
|
||||
return "", fmt.Errorf("unable to find context (%s). Make sure the context exists and you have permissions to use it. Error: %s", context, err)
|
||||
}
|
||||
return context, nil
|
||||
}
|
||||
@@ -281,7 +281,7 @@ func getCertFromServer(ctx *cli.Context, serverConfig *config.ServerConfig) (*cl
|
||||
var certReponse *CACertResponse
|
||||
err = json.Unmarshal(content, &certReponse)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("Unable to parse response from %s/v3/settings/cacerts\nError: %s\nResponse:\n%s", serverConfig.URL, err, content)
|
||||
return nil, fmt.Errorf("unable to parse response from %s/v3/settings/cacerts\nError: %s\nResponse:\n%s", serverConfig.URL, err, content)
|
||||
}
|
||||
|
||||
cert, err := verifyCert([]byte(certReponse.Value))
|
||||
@@ -317,9 +317,10 @@ func verifyUserAcceptsCert(certs []string, url string) bool {
|
||||
input := scanner.Text()
|
||||
input = strings.ToLower(strings.TrimSpace(input))
|
||||
|
||||
if input == "yes" || input == "y" {
|
||||
switch input {
|
||||
case "yes", "y":
|
||||
return true
|
||||
} else if input == "no" || input == "n" {
|
||||
case "no", "n":
|
||||
return false
|
||||
}
|
||||
fmt.Printf("Please type 'yes' or 'no': ")
|
||||
|
||||
@@ -67,7 +67,7 @@ func wait(ctx *cli.Context) error {
|
||||
for {
|
||||
select {
|
||||
case <-timeout:
|
||||
return fmt.Errorf("Timeout reached %v:%v transitioningMessage: %v", resource.Type, resource.ID, mapResource["transitioningMessage"])
|
||||
return fmt.Errorf("timeout reached %v:%v transitioningMessage: %v", resource.Type, resource.ID, mapResource["transitioningMessage"])
|
||||
case <-ticker.C:
|
||||
err = c.ByID(resource, &mapResource)
|
||||
if err != nil {
|
||||
|
||||
@@ -93,21 +93,22 @@ func (t *TableWriter) Write(obj interface{}) {
|
||||
return
|
||||
}
|
||||
|
||||
if t.ValueFormat == "json" {
|
||||
switch t.ValueFormat {
|
||||
case "json":
|
||||
content, err := json.Marshal(obj)
|
||||
t.err = err
|
||||
if t.err != nil {
|
||||
return
|
||||
}
|
||||
_, t.err = t.Writer.Write(append(content, byte('\n')))
|
||||
} else if t.ValueFormat == "yaml" {
|
||||
case "yaml":
|
||||
content, err := yaml.Marshal(obj)
|
||||
t.err = err
|
||||
if t.err != nil {
|
||||
return
|
||||
}
|
||||
_, t.err = t.Writer.Write(append(content, byte('\n')))
|
||||
} else {
|
||||
default:
|
||||
t.err = printTemplate(t.Writer, t.ValueFormat, obj)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user