From cb3024c5ff33fdc393028ff27ebda021fb0de488 Mon Sep 17 00:00:00 2001 From: Matthieu MOREL Date: Mon, 6 Jan 2025 17:41:38 +0100 Subject: [PATCH] chore: enable superfluous-else from revive (#21373) Signed-off-by: Matthieu MOREL --- .golangci.yaml | 3 +- cmd/argocd-dex/commands/argocd_dex.go | 3 +- cmd/argocd/commands/admin/settings_rbac.go | 17 ++++----- cmd/argocd/commands/app.go | 42 +++++++++++----------- cmd/argocd/commands/cert.go | 3 +- cmd/argocd/commands/project.go | 21 +++++------ cmd/util/project.go | 20 +++++------ reposerver/gpgwatcher.go | 7 ++-- util/cli/cli.go | 3 +- util/git/client.go | 6 ++-- 10 files changed, 57 insertions(+), 68 deletions(-) diff --git a/.golangci.yaml b/.golangci.yaml index 54a58363ef87f..f1ea2c2676081 100644 --- a/.golangci.yaml +++ b/.golangci.yaml @@ -110,6 +110,7 @@ linters-settings: disabled: true # for better readability, the errors should be last in the list of returned values by a function. - name: error-return + disabled: false # for better readability, error messages should not be capitalized or end with punctuation or a newline. - name: error-strings disabled: true @@ -135,7 +136,7 @@ linters-settings: disabled: false # redundant else-blocks that can be eliminated from the code. - name: superfluous-else - disabled: true + disabled: false # prevent confusing name for variables when using `time` package - name: time-naming disabled: true diff --git a/cmd/argocd-dex/commands/argocd_dex.go b/cmd/argocd-dex/commands/argocd_dex.go index 5bdde7e572bdb..332e79b6af8a4 100644 --- a/cmd/argocd-dex/commands/argocd_dex.go +++ b/cmd/argocd-dex/commands/argocd_dex.go @@ -136,9 +136,8 @@ func NewRunDexCommand() *cobra.Command { errors.CheckError(err) } break - } else { - log.Infof("dex config unmodified") } + log.Infof("dex config unmodified") } } }, diff --git a/cmd/argocd/commands/admin/settings_rbac.go b/cmd/argocd/commands/admin/settings_rbac.go index b2d3ee2abd1e0..50246c22849f8 100644 --- a/cmd/argocd/commands/admin/settings_rbac.go +++ b/cmd/argocd/commands/admin/settings_rbac.go @@ -248,12 +248,11 @@ argocd admin settings rbac can someuser create application 'default/app' --defau fmt.Println("Yes") } os.Exit(0) - } else { - if !quiet { - fmt.Println("No") - } - os.Exit(1) } + if !quiet { + fmt.Println("No") + } + os.Exit(1) }, } clientConfig = cli.AddKubectlFlagsToCmd(command) @@ -321,13 +320,11 @@ argocd admin settings rbac validate --namespace argocd if err := rbac.ValidatePolicy(userPolicy); err == nil { fmt.Printf("Policy is valid.\n") os.Exit(0) - } else { - fmt.Printf("Policy is invalid: %v\n", err) - os.Exit(1) } - } else { - log.Fatalf("Policy is empty or could not be loaded.") + fmt.Printf("Policy is invalid: %v\n", err) + os.Exit(1) } + log.Fatalf("Policy is empty or could not be loaded.") }, } clientConfig = cli.AddKubectlFlagsToCmd(command) diff --git a/cmd/argocd/commands/app.go b/cmd/argocd/commands/app.go index 9a4ca5c977dbd..127dc96240b4a 100644 --- a/cmd/argocd/commands/app.go +++ b/cmd/argocd/commands/app.go @@ -405,11 +405,11 @@ func NewApplicationGetCommand(clientOpts *argocdclient.ClientOptions) *cobra.Com if sourceName != "" { sourceNameToPosition := getSourceNameToPositionMap(app) - if pos, ok := sourceNameToPosition[sourceName]; !ok { + pos, ok := sourceNameToPosition[sourceName] + if !ok { log.Fatalf("Unknown source name '%s'", sourceName) - } else { - sourcePosition = int(pos) } + sourcePosition = int(pos) } // check for source position if --show-params is set @@ -840,11 +840,11 @@ func NewApplicationSetCommand(clientOpts *argocdclient.ClientOptions) *cobra.Com if sourceName != "" { sourceNameToPosition := getSourceNameToPositionMap(app) - if pos, ok := sourceNameToPosition[sourceName]; !ok { + pos, ok := sourceNameToPosition[sourceName] + if !ok { log.Fatalf("Unknown source name '%s'", sourceName) - } else { - sourcePosition = int(pos) } + sourcePosition = int(pos) } if app.Spec.HasMultipleSources() { @@ -953,11 +953,11 @@ func NewApplicationUnsetCommand(clientOpts *argocdclient.ClientOptions) *cobra.C if sourceName != "" { sourceNameToPosition := getSourceNameToPositionMap(app) - if pos, ok := sourceNameToPosition[sourceName]; !ok { + pos, ok := sourceNameToPosition[sourceName] + if !ok { log.Fatalf("Unknown source name '%s'", sourceName) - } else { - sourcePosition = int(pos) } + sourcePosition = int(pos) } if app.Spec.HasMultipleSources() { @@ -1271,11 +1271,11 @@ func NewApplicationDiffCommand(clientOpts *argocdclient.ClientOptions) *cobra.Co sourceNameToPosition := getSourceNameToPositionMap(app) for _, name := range sourceNames { - if pos, ok := sourceNameToPosition[name]; !ok { + pos, ok := sourceNameToPosition[name] + if !ok { log.Fatalf("Unknown source name '%s'", name) - } else { - sourcePositions = append(sourcePositions, pos) } + sourcePositions = append(sourcePositions, pos) } } @@ -2033,11 +2033,11 @@ func NewApplicationSyncCommand(clientOpts *argocdclient.ClientOptions) *cobra.Co sourceNameToPosition := getSourceNameToPositionMap(app) for _, name := range sourceNames { - if pos, ok := sourceNameToPosition[name]; !ok { + pos, ok := sourceNameToPosition[name] + if !ok { log.Fatalf("Unknown source name '%s'", name) - } else { - sourcePositions = append(sourcePositions, pos) } + sourcePositions = append(sourcePositions, pos) } } @@ -3004,11 +3004,11 @@ func NewApplicationManifestsCommand(clientOpts *argocdclient.ClientOptions) *cob sourceNameToPosition := getSourceNameToPositionMap(app) for _, name := range sourceNames { - if pos, ok := sourceNameToPosition[name]; !ok { + pos, ok := sourceNameToPosition[name] + if !ok { log.Fatalf("Unknown source name '%s'", name) - } else { - sourcePositions = append(sourcePositions, pos) } + sourcePositions = append(sourcePositions, pos) } } @@ -3339,11 +3339,11 @@ func NewApplicationRemoveSourceCommand(clientOpts *argocdclient.ClientOptions) * if sourceName != "" { sourceNameToPosition := getSourceNameToPositionMap(app) - if pos, ok := sourceNameToPosition[sourceName]; !ok { + pos, ok := sourceNameToPosition[sourceName] + if !ok { log.Fatalf("Unknown source name '%s'", sourceName) - } else { - sourcePosition = int(pos) } + sourcePosition = int(pos) } if !app.Spec.HasMultipleSources() { diff --git a/cmd/argocd/commands/cert.go b/cmd/argocd/commands/cert.go index e20243d55c141..45c78a04830ba 100644 --- a/cmd/argocd/commands/cert.go +++ b/cmd/argocd/commands/cert.go @@ -106,9 +106,8 @@ func NewCertAddTLSCommand(clientOpts *argocdclient.ClientOptions) *cobra.Command if subjectMap[x509cert.Subject.String()] != nil { fmt.Printf("ERROR: Cert with subject '%s' already seen in the input stream.\n", x509cert.Subject.String()) continue - } else { - subjectMap[x509cert.Subject.String()] = x509cert } + subjectMap[x509cert.Subject.String()] = x509cert } serverName := args[0] diff --git a/cmd/argocd/commands/project.go b/cmd/argocd/commands/project.go index 2eaa156414c10..ae1f1365d1d31 100644 --- a/cmd/argocd/commands/project.go +++ b/cmd/argocd/commands/project.go @@ -254,11 +254,10 @@ func NewProjectRemoveSignatureKeyCommand(clientOpts *argocdclient.ClientOptions) } if index == -1 { log.Fatal("Specified signature key is not configured for project") - } else { - proj.Spec.SignatureKeys = append(proj.Spec.SignatureKeys[:index], proj.Spec.SignatureKeys[index+1:]...) - _, err = projIf.Update(ctx, &projectpkg.ProjectUpdateRequest{Project: proj}) - errors.CheckError(err) } + proj.Spec.SignatureKeys = append(proj.Spec.SignatureKeys[:index], proj.Spec.SignatureKeys[index+1:]...) + _, err = projIf.Update(ctx, &projectpkg.ProjectUpdateRequest{Project: proj}) + errors.CheckError(err) }, } @@ -352,11 +351,10 @@ func NewProjectRemoveDestinationCommand(clientOpts *argocdclient.ClientOptions) } if index == -1 { log.Fatal("Specified destination does not exist in project") - } else { - proj.Spec.Destinations = append(proj.Spec.Destinations[:index], proj.Spec.Destinations[index+1:]...) - _, err = projIf.Update(ctx, &projectpkg.ProjectUpdateRequest{Project: proj}) - errors.CheckError(err) } + proj.Spec.Destinations = append(proj.Spec.Destinations[:index], proj.Spec.Destinations[index+1:]...) + _, err = projIf.Update(ctx, &projectpkg.ProjectUpdateRequest{Project: proj}) + errors.CheckError(err) }, } @@ -456,11 +454,10 @@ func NewProjectRemoveOrphanedIgnoreCommand(clientOpts *argocdclient.ClientOption } if index == -1 { log.Fatal("Specified resource does not exist in the orphaned ignore of project") - } else { - proj.Spec.OrphanedResources.Ignore = append(proj.Spec.OrphanedResources.Ignore[:index], proj.Spec.OrphanedResources.Ignore[index+1:]...) - _, err = projIf.Update(ctx, &projectpkg.ProjectUpdateRequest{Project: proj}) - errors.CheckError(err) } + proj.Spec.OrphanedResources.Ignore = append(proj.Spec.OrphanedResources.Ignore[:index], proj.Spec.OrphanedResources.Ignore[index+1:]...) + _, err = projIf.Update(ctx, &projectpkg.ProjectUpdateRequest{Project: proj}) + errors.CheckError(err) }, } command.Flags().StringVar(&name, "name", "", "Resource name pattern") diff --git a/cmd/util/project.go b/cmd/util/project.go index 82e6b14a542bf..d54db89ad35ee 100644 --- a/cmd/util/project.go +++ b/cmd/util/project.go @@ -86,12 +86,11 @@ func (opts *ProjectOpts) GetDestinations() []v1alpha1.ApplicationDestination { parts := strings.Split(destStr, ",") if len(parts) != 2 { log.Fatalf("Expected destination of the form: server,namespace. Received: %s", destStr) - } else { - destinations = append(destinations, v1alpha1.ApplicationDestination{ - Server: parts[0], - Namespace: parts[1], - }) } + destinations = append(destinations, v1alpha1.ApplicationDestination{ + Server: parts[0], + Namespace: parts[1], + }) } return destinations } @@ -102,13 +101,12 @@ func (opts *ProjectOpts) GetDestinationServiceAccounts() []v1alpha1.ApplicationD parts := strings.Split(destStr, ",") if len(parts) != 3 { log.Fatalf("Expected destination service account of the form: server,namespace, defaultServiceAccount. Received: %s", destStr) - } else { - destinationServiceAccounts = append(destinationServiceAccounts, v1alpha1.ApplicationDestinationServiceAccount{ - Server: parts[0], - Namespace: parts[1], - DefaultServiceAccount: parts[2], - }) } + destinationServiceAccounts = append(destinationServiceAccounts, v1alpha1.ApplicationDestinationServiceAccount{ + Server: parts[0], + Namespace: parts[1], + DefaultServiceAccount: parts[2], + }) } return destinationServiceAccounts } diff --git a/reposerver/gpgwatcher.go b/reposerver/gpgwatcher.go index 35495c36086c3..e23ffb728a295 100644 --- a/reposerver/gpgwatcher.go +++ b/reposerver/gpgwatcher.go @@ -51,11 +51,10 @@ func StartGPGWatcher(sourcePath string) error { log.Infof("Retrying to re-create watcher, attempt %d of %d", attempt, maxRecreateRetries) time.Sleep(1 * time.Second) continue - } else { - log.Errorf("Maximum retries exceeded.") - close(done) - return } + log.Errorf("Maximum retries exceeded.") + close(done) + return } break } diff --git a/util/cli/cli.go b/util/cli/cli.go index 1a6383d2e93ba..d1e8492ec4bfc 100644 --- a/util/cli/cli.go +++ b/util/cli/cli.go @@ -279,9 +279,8 @@ func InteractiveEdit(filePattern string, data []byte, save func(input []byte) er if string(updated) == "" || string(updated) == string(data) { errors.CheckError(stderrors.New("edit cancelled, no valid changes were saved")) break - } else { - data = stripComments(updated) } + data = stripComments(updated) err = save(data) if err == nil { diff --git a/util/git/client.go b/util/git/client.go index f45be86bffde9..300646e22cfcb 100644 --- a/util/git/client.go +++ b/util/git/client.go @@ -137,11 +137,11 @@ var ( func init() { if countStr := os.Getenv(common.EnvGitAttemptsCount); countStr != "" { - if cnt, err := strconv.Atoi(countStr); err != nil { + cnt, err := strconv.Atoi(countStr) + if err != nil { panic(fmt.Sprintf("Invalid value in %s env variable: %v", common.EnvGitAttemptsCount, err)) - } else { - maxAttemptsCount = int(math.Max(float64(cnt), 1)) } + maxAttemptsCount = int(math.Max(float64(cnt), 1)) } maxRetryDuration = env.ParseDurationFromEnv(common.EnvGitRetryMaxDuration, common.DefaultGitRetryMaxDuration, 0, math.MaxInt64)