Skip to content

Commit 3cf1e63

Browse files
authored
build: address all lint issues by v2 (#20804)
* fix QF1011: could omit type *os.File from declaration; it will be inferred from the right-hand side * fix QF1012: Use fmt.Fprintf(x, ...) instead of x.Write(fmt.Sprintf(...)) * fix QF1001: could apply De Morgan's law * fix QF1003: could use tagged switch * fix weakCond: suspicious ; nil check may not be enough, check for len (gocritic) * fix docStub: silencing go lint doc-comment warnings is unadvised * fix builtinShadow: shadowing of predeclared identifier: error * fix importShadow: shadow of imported package * fix nestingReduce: invert if cond, replace body with , move old body after the statement * useless-break: useless break in case clause (revive) * Clear the redundant content in golangci.yaml file
1 parent f89d46d commit 3cf1e63

File tree

79 files changed

+546
-517
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

79 files changed

+546
-517
lines changed

.golangci.yaml

Lines changed: 6 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -23,23 +23,8 @@ linters:
2323
- "-ST1020"
2424
- "-ST1021"
2525
- "-ST1022"
26-
##### TODO: fix and enable these
27-
# 4 occurrences.
28-
# Use fmt.Fprintf(x, ...) instead of x.Write(fmt.Sprintf(...)) https://staticcheck.dev/docs/checks#QF1012
29-
- "-QF1012"
30-
# 3 occurrences.
31-
# Apply De Morgan’s law https://staticcheck.dev/docs/checks#QF1001
32-
- "-QF1001"
33-
# 9 occurrences.
34-
# Convert if/else-if chain to tagged switch https://staticcheck.dev/docs/checks#QF1003
35-
- "-QF1003"
36-
# 1 occurrence.
37-
# could omit type *os.File from declaration; it will be inferred from the right-hand side
38-
- "-QF1011"
39-
##### These have been vetted to be disabled.
40-
# 19 occurrences. Omit embedded fields from selector expression https://staticcheck.dev/docs/checks#QF1008
41-
# Usefulness is questionable.
4226
- "-QF1008"
27+
4328
revive:
4429
enable-all-rules: true
4530
rules:
@@ -150,23 +135,22 @@ linters:
150135
# - yodaStyleExpr
151136
# - typeUnparen
152137

153-
##### TODO: fix and enable these
154138
# We enabled these and we pass
155139
- nilValReturn
156-
# - weakCond # pkg/minikube/config/profile.go:61:9: weakCond: suspicious `cc.Nodes != nil && cc.Nodes[0].Name == node.Name`; nil check may not be enough, check for len (gocritic)
140+
- weakCond
157141
- indexAlloc
158142
- rangeExprCopy
159143
- boolExprSimplify
160144
- commentedOutImport
161-
# - docStub # pkg/minikube/tunnel/kic/service_tunnel.go:51:1: docStub: silencing go lint doc-comment warnings is unadvised (gocritic)
145+
- docStub
162146
- emptyFallthrough
163147
- hexLiteral
164148
- typeAssertChain
165149
- unlabelStmt
166-
# - builtinShadow # cmd/minikube/cmd/delete.go:89:7: builtinShadow: shadowing of predeclared identifier: error (gocritic)
167-
# - importShadow # pkg/storage/storage_provisioner.go:60:2: importShadow: shadow of imported package 'path' (gocritic)
150+
- builtinShadow
151+
- importShadow
168152
- initClause
169-
# - nestingReduce # pkg/minikube/tunnel/registry.go:94:3: nestingReduce: invert if cond, replace body with `continue`, move old body after the statement (gocritic)
153+
- nestingReduce
170154
- unnecessaryBlock
171155

172156
exclusions:
@@ -181,7 +165,3 @@ linters:
181165
- path: '(.+)\.go$'
182166
text: "Error return value of `.*` is not checked"
183167
linter: errcheck
184-
# This code is doubtful and I don't understand it. Location: Line 456
185-
- path: 'cmd/minikube/cmd/docker-env.go'
186-
text: "useless-break: useless break in case clause"
187-
linter: revive

cmd/minikube/cmd/completion.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ func GenerateBashCompletion(w io.Writer, cmd *cobra.Command) error {
162162
}
163163

164164
// GenerateZshCompletion generates the completion for the zsh shell
165-
func GenerateZshCompletion(out io.Writer, cmd *cobra.Command) error {
165+
func GenerateZshCompletion(w io.Writer, cmd *cobra.Command) error {
166166
zshAutoloadTag := `#compdef minikube
167167
`
168168

@@ -300,17 +300,17 @@ __minikube_convert_bash_to_zsh() {
300300
<<'BASH_COMPLETION_EOF'
301301
`
302302

303-
_, err := out.Write([]byte(zshAutoloadTag))
303+
_, err := w.Write([]byte(zshAutoloadTag))
304304
if err != nil {
305305
return err
306306
}
307307

308-
_, err = out.Write([]byte(boilerPlate))
308+
_, err = w.Write([]byte(boilerPlate))
309309
if err != nil {
310310
return err
311311
}
312312

313-
_, err = out.Write([]byte(zshInitialization))
313+
_, err = w.Write([]byte(zshInitialization))
314314
if err != nil {
315315
return err
316316
}
@@ -320,7 +320,7 @@ __minikube_convert_bash_to_zsh() {
320320
if err != nil {
321321
return errors.Wrap(err, "Error generating zsh completion")
322322
}
323-
_, err = out.Write(buf.Bytes())
323+
_, err = w.Write(buf.Bytes())
324324
if err != nil {
325325
return err
326326
}
@@ -330,7 +330,7 @@ BASH_COMPLETION_EOF
330330
}
331331
__minikube_bash_source <(__minikube_convert_bash_to_zsh)
332332
`
333-
_, err = out.Write([]byte(zshTail))
333+
_, err = w.Write([]byte(zshTail))
334334
if err != nil {
335335
return err
336336
}

cmd/minikube/cmd/config/configure.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ func loadAddonConfigFile(addon, configFilePath string) (ac *addonConfig) {
133133
type configFile struct {
134134
Addons addonConfig `json:"addons"`
135135
}
136-
var config configFile
136+
var cf configFile
137137

138138
if configFilePath != "" {
139139
out.Ln("Reading %s configs from %s", addon, configFilePath)
@@ -150,14 +150,14 @@ func loadAddonConfigFile(addon, configFilePath string) (ac *addonConfig) {
150150
fmt.Sprintf("error opening config file: %s", configFilePath))
151151
}
152152

153-
if err = json.Unmarshal(confData, &config); err != nil {
153+
if err = json.Unmarshal(confData, &cf); err != nil {
154154
// err = errors2.Wrapf(err, "error reading config file (%s)", configFilePath)
155155
klog.Errorf("error reading config file (%s): %v", configFilePath, err)
156156
exit.Message(reason.Kind{ExitCode: reason.ExProgramConfig, Advice: "provide a valid config file"},
157157
fmt.Sprintf("error reading config file: %v", err))
158158
}
159159

160-
return &config.Addons
160+
return &cf.Addons
161161
}
162162
return nil
163163
}

cmd/minikube/cmd/config/configure_registry_creds.go

Lines changed: 24 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,9 @@ func processRegistryCredsConfig(profile string, ac *addonConfig) {
9292

9393
regCredsConf := &ac.RegistryCreds
9494
awsEcrAction := regCredsConf.EnableAWSEcr // regCredsConf. "enableAWSEcr")
95-
if awsEcrAction == "prompt" || awsEcrAction == "" {
95+
96+
switch awsEcrAction {
97+
case "prompt", "":
9698
enableAWSECR := AskForYesNoConfirmation("\nDo you want to enable AWS Elastic Container Registry?", posResponses, negResponses)
9799
if enableAWSECR {
98100
awsAccessID = AskForStaticValue("-- Enter AWS Access Key ID: ")
@@ -102,7 +104,7 @@ func processRegistryCredsConfig(profile string, ac *addonConfig) {
102104
awsAccount = AskForStaticValue("-- Enter 12 digit AWS Account ID (Comma separated list): ")
103105
awsRole = AskForStaticValueOptional("-- (Optional) Enter ARN of AWS role to assume: ")
104106
}
105-
} else if awsEcrAction == "enable" {
107+
case "enable":
106108
out.Ln("Loading AWS ECR configs from: %s", addonConfigFile)
107109
// Then read the configs
108110
awsAccessID = regCredsConf.EcrConfigs.AccessID
@@ -111,15 +113,17 @@ func processRegistryCredsConfig(profile string, ac *addonConfig) {
111113
awsRegion = regCredsConf.EcrConfigs.Region
112114
awsAccount = regCredsConf.EcrConfigs.Account
113115
awsRole = regCredsConf.EcrConfigs.Role
114-
} else if awsEcrAction == "disable" {
116+
case "disable":
115117
out.Ln("Ignoring AWS ECR configs")
116-
} else {
118+
default:
117119
out.Ln("Disabling AWS ECR. Invalid value for enableAWSEcr (%s). Must be one of 'disable', 'enable' or 'prompt'", awsEcrAction)
118120
}
119121

120122
gcrPath := ""
121123
gcrAction := regCredsConf.EnableGCR
122-
if gcrAction == "prompt" || gcrAction == "" {
124+
125+
switch gcrAction {
126+
case "prompt", "":
123127
enableGCR := AskForYesNoConfirmation("\nDo you want to enable Google Container Registry?", posResponses, negResponses)
124128
if enableGCR {
125129
gcrPath = AskForStaticValue("-- Enter path to credentials (e.g. /home/user/.config/gcloud/application_default_credentials.json):")
@@ -129,14 +133,14 @@ func processRegistryCredsConfig(profile string, ac *addonConfig) {
129133
gcrURL = AskForStaticValue("-- Enter GCR URL (e.g. https://asia.gcr.io):")
130134
}
131135
}
132-
} else if gcrAction == "enable" {
136+
case "enable":
133137
out.Ln("Loading GCR configs from: %s", addonConfigFile)
134138
// Then read the configs
135139
gcrPath = regCredsConf.GcrConfigs.GcrPath
136140
gcrURL = regCredsConf.GcrConfigs.GcrURL
137-
} else if gcrAction == "disable" {
141+
case "disable":
138142
out.Ln("Ignoring GCR configs")
139-
} else {
143+
default:
140144
out.Ln("Disabling GCR. Invalid value for enableGCR (%s). Must be one of 'disable', 'enable' or 'prompt'", gcrAction)
141145
}
142146

@@ -152,40 +156,44 @@ func processRegistryCredsConfig(profile string, ac *addonConfig) {
152156
}
153157

154158
dockerRegistryAction := regCredsConf.EnableDockerRegistry
155-
if dockerRegistryAction == "prompt" || dockerRegistryAction == "" {
159+
160+
switch dockerRegistryAction {
161+
case "prompt", "":
156162
enableDR := AskForYesNoConfirmation("\nDo you want to enable Docker Registry?", posResponses, negResponses)
157163
if enableDR {
158164
dockerServer = AskForStaticValue("-- Enter docker registry server url: ")
159165
dockerUser = AskForStaticValue("-- Enter docker registry username: ")
160166
dockerPass = AskForPasswordValue("-- Enter docker registry password: ")
161167
}
162-
} else if dockerRegistryAction == "enable" {
168+
case "enable":
163169
out.Ln("Loading Docker Registry configs from: %s", addonConfigFile)
164170
dockerServer = regCredsConf.DockerConfigs.DockerServer
165171
dockerUser = regCredsConf.DockerConfigs.DockerUser
166172
dockerPass = regCredsConf.DockerConfigs.DockerPass
167-
} else if dockerRegistryAction == "disable" {
173+
case "disable":
168174
out.Ln("Ignoring Docker Registry configs")
169-
} else {
175+
default:
170176
out.Ln("Disabling Docker Registry. Invalid value for enableDockerRegistry (%s). Must be one of 'disable', 'enable' or 'prompt'", dockerRegistryAction)
171177
}
172178

173179
acrAction := regCredsConf.EnableACR
174-
if acrAction == "prompt" || acrAction == "" {
180+
181+
switch acrAction {
182+
case "prompt", "":
175183
enableACR := AskForYesNoConfirmation("\nDo you want to enable Azure Container Registry?", posResponses, negResponses)
176184
if enableACR {
177185
acrURL = AskForStaticValue("-- Enter Azure Container Registry (ACR) URL: ")
178186
acrClientID = AskForStaticValue("-- Enter client ID (service principal ID) to access ACR: ")
179187
acrPassword = AskForPasswordValue("-- Enter service principal password to access Azure Container Registry: ")
180188
}
181-
} else if acrAction == "enable" {
189+
case "enable":
182190
out.Ln("Loading ACR configs from: ", addonConfigFile)
183191
acrURL = regCredsConf.AcrConfigs.AcrURL
184192
acrClientID = regCredsConf.AcrConfigs.AcrClientID
185193
acrPassword = regCredsConf.AcrConfigs.AcrPassword
186-
} else if acrAction == "disable" {
194+
case "disable":
187195
out.Ln("Ignoring ACR configs")
188-
} else {
196+
default:
189197
out.Stringf("Disabling ACR. Invalid value for enableACR (%s). Must be one of 'disable', 'enable' or 'prompt'", acrAction)
190198
}
191199

cmd/minikube/cmd/dashboard.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ func kubectlProxy(kubectlVersion string, binaryURL string, contextName string, p
157157
klog.Infof("Waiting for kubectl to output host:port ...")
158158
reader := bufio.NewReader(stdoutPipe)
159159

160-
var out []byte
160+
var outData []byte
161161
for {
162162
r, timedOut, err := readByteWithTimeout(reader, 5*time.Second)
163163
if err != nil {
@@ -170,10 +170,10 @@ func kubectlProxy(kubectlVersion string, binaryURL string, contextName string, p
170170
klog.Infof("timed out waiting for input: possibly due to an old kubectl version.")
171171
break
172172
}
173-
out = append(out, r)
173+
outData = append(outData, r)
174174
}
175-
klog.Infof("proxy stdout: %s", string(out))
176-
return cmd, hostPortRe.FindString(string(out)), nil
175+
klog.Infof("proxy stdout: %s", string(outData))
176+
return cmd, hostPortRe.FindString(string(outData)), nil
177177
}
178178

179179
// readByteWithTimeout returns a byte from a reader or an indicator that a timeout has occurred.
@@ -203,9 +203,9 @@ func readByteWithTimeout(r io.ByteReader, timeout time.Duration) (byte, bool, er
203203
}
204204

205205
// dashboardURL generates a URL for accessing the dashboard service
206-
func dashboardURL(proxy string, ns string, svc string) string {
206+
func dashboardURL(addr string, ns string, svc string) string {
207207
// Reference: https://github.com/kubernetes/dashboard/wiki/Accessing-Dashboard---1.7.X-and-above
208-
return fmt.Sprintf("http://%s/api/v1/namespaces/%s/services/http:%s:/proxy/", proxy, ns, svc)
208+
return fmt.Sprintf("http://%s/api/v1/namespaces/%s/services/http:%s:/proxy/", addr, ns, svc)
209209
}
210210

211211
// checkURL checks if a URL returns 200 HTTP OK

cmd/minikube/cmd/delete.go

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -86,8 +86,8 @@ type DeletionError struct {
8686
Errtype typeOfError
8787
}
8888

89-
func (error DeletionError) Error() string {
90-
return error.Err.Error()
89+
func (deletionError DeletionError) Error() string {
90+
return deletionError.Err.Error()
9191
}
9292

9393
var hostAndDirsDeleter = func(api libmachine.API, cc *config.ClusterConfig, profileName string) error {
@@ -527,11 +527,11 @@ func uninstallKubernetes(api libmachine.API, cc config.ClusterConfig, n config.N
527527
}
528528

529529
// HandleDeletionErrors handles deletion errors from DeleteProfiles
530-
func HandleDeletionErrors(errors []error) {
531-
if len(errors) == 1 {
532-
handleSingleDeletionError(errors[0])
530+
func HandleDeletionErrors(errs []error) {
531+
if len(errs) == 1 {
532+
handleSingleDeletionError(errs[0])
533533
} else {
534-
handleMultipleDeletionErrors(errors)
534+
handleMultipleDeletionErrors(errs)
535535
}
536536
}
537537

@@ -556,10 +556,10 @@ func handleSingleDeletionError(err error) {
556556
}
557557
}
558558

559-
func handleMultipleDeletionErrors(errors []error) {
559+
func handleMultipleDeletionErrors(errs []error) {
560560
out.ErrT(style.Sad, "Multiple errors deleting profiles")
561561

562-
for _, err := range errors {
562+
for _, err := range errs {
563563
deletionError, ok := err.(DeletionError)
564564

565565
if ok {
@@ -706,14 +706,14 @@ var isMinikubeProcess = func(pid int) (bool, error) {
706706
// getPids opens the file at PATH and tries to read
707707
// one or more space separated pids
708708
func getPids(path string) ([]int, error) {
709-
out, err := os.ReadFile(path)
709+
data, err := os.ReadFile(path)
710710
if err != nil {
711711
return nil, errors.Wrap(err, "ReadFile")
712712
}
713-
klog.Infof("pidfile contents: %s", out)
713+
klog.Infof("pidfile contents: %s", data)
714714

715715
pids := []int{}
716-
strPids := strings.Fields(string(out))
716+
strPids := strings.Fields(string(data))
717717
for _, p := range strPids {
718718
intPid, err := strconv.Atoi(p)
719719
if err != nil {

0 commit comments

Comments
 (0)