diff --git a/.golangci.yml b/.golangci.yml index 39e52917b9..c0a2851b19 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -41,6 +41,12 @@ linters: govet: disable: - composites + staticcheck: + checks: + - all + - -QF1001 # De Morgan's law simplifications + - -QF1008 # Embedded field selector simplifications + - -ST1003 # Naming conventions (ID vs Id, URL vs Url, etc.) exclusions: generated: lax presets: diff --git a/cmd/src/batch_common.go b/cmd/src/batch_common.go index 157af55927..21fdce2e21 100644 --- a/cmd/src/batch_common.go +++ b/cmd/src/batch_common.go @@ -632,7 +632,7 @@ func getBatchSpecDirectory(file string) (string, error) { func checkExecutable(cmd string, args ...string) error { if err := exec.Command(cmd, args...).Run(); err != nil { return fmt.Errorf( - "failed to execute \"%s %s\":\n\t%s\n\n'src batch' requires %q to be available.", + "failed to execute \"%s %s\":\n\t%s\n\n'src batch' requires %q to be available", cmd, strings.Join(args, " "), err, diff --git a/cmd/src/search.go b/cmd/src/search.go index b4529da7c3..69ff1995b9 100644 --- a/cmd/src/search.go +++ b/cmd/src/search.go @@ -457,9 +457,10 @@ func applyHighlightsForFile(fileContent string, highlights []highlight) string { for _, highlight := range highlights { line := lines[highlight.line] for characterIndex, character := range []rune(line + "\n") { - if characterIndex == highlight.character { + switch characterIndex { + case highlight.character: result = append(result, []rune(start)...) - } else if characterIndex == highlight.character+highlight.length { + case highlight.character + highlight.length: result = append(result, []rune(end)...) } result = append(result, character) @@ -478,9 +479,10 @@ func applyHighlights(input string, highlights []highlight, start, end string) st for characterIndex, character := range []rune(line + "\n") { for _, highlight := range highlights { if highlight.line == lineNumber { - if characterIndex == highlight.character { + switch characterIndex { + case highlight.character: result = append(result, []rune(start)...) - } else if characterIndex == highlight.character+highlight.length { + case highlight.character + highlight.length: result = append(result, []rune(end)...) } } diff --git a/cmd/src/signature_fetch.go b/cmd/src/signature_fetch.go index e0bfa34e9f..4b9fd03a16 100644 --- a/cmd/src/signature_fetch.go +++ b/cmd/src/signature_fetch.go @@ -174,7 +174,7 @@ func (c cosignConfig) verifySignatureForImageHash(image string, hash string) err output, err := cmd.CombinedOutput() if err != nil { - return fmt.Errorf("Signature verification failed: %w\nOutput: %s", err, output) + return fmt.Errorf("signature verification failed: %w\nOutput: %s", err, output) } return nil diff --git a/internal/api/api.go b/internal/api/api.go index 63cef92b50..a31fae55e4 100644 --- a/internal/api/api.go +++ b/internal/api/api.go @@ -236,8 +236,7 @@ func (r *request) do(ctx context.Context, result interface{}) (bool, error) { // Check trace header before we potentially early exit if *r.client.opts.Flags.trace { - _, err := r.client.opts.Out.Write([]byte(fmt.Sprintf("x-trace: %s\n", resp.Header.Get("x-trace")))) - if err != nil { + if _, err := fmt.Fprintf(r.client.opts.Out, "x-trace: %s\n", resp.Header.Get("x-trace")); err != nil { return false, err } } diff --git a/internal/api/proxy.go b/internal/api/proxy.go index 5e5fbf30ef..3cf8673829 100644 --- a/internal/api/proxy.go +++ b/internal/api/proxy.go @@ -54,12 +54,12 @@ func applyProxy(transport *http.Transport, proxyURL *url.URL, proxyPath string) transport.Proxy = nil proxyApplied = true } else if proxyURL != nil { - if proxyURL.Scheme == "socks5" || - proxyURL.Scheme == "socks5h" { + switch proxyURL.Scheme { + case "socks5", "socks5h": // SOCKS proxies work out of the box - no need to manually dial transport.Proxy = http.ProxyURL(proxyURL) proxyApplied = true - } else if proxyURL.Scheme == "http" || proxyURL.Scheme == "https" { + case "http", "https": dial := func(ctx context.Context, network, addr string) (net.Conn, error) { // Dial the proxy d := net.Dialer{} diff --git a/internal/batches/debug.go b/internal/batches/debug.go index c5754d63e7..3a7310f664 100644 --- a/internal/batches/debug.go +++ b/internal/batches/debug.go @@ -1,5 +1,4 @@ //go:build debug -// +build debug package batches diff --git a/internal/batches/docker/version.go b/internal/batches/docker/version.go index 84cfdcd1e7..d6a858f508 100644 --- a/internal/batches/docker/version.go +++ b/internal/batches/docker/version.go @@ -12,7 +12,7 @@ func CheckVersion(ctx context.Context) error { _, err := executeFastCommand(ctx, "version") if err != nil { return fmt.Errorf( - "failed to execute \"docker version\":\n\t%s\n\n'src batch' requires \"docker\" to be available.", + "failed to execute \"docker version\":\n\t%s\n\n'src batch' requires \"docker\" to be available", err, ) } diff --git a/internal/batches/executor/execution_cache_test.go b/internal/batches/executor/execution_cache_test.go index d5ff754ffc..5357278420 100644 --- a/internal/batches/executor/execution_cache_test.go +++ b/internal/batches/executor/execution_cache_test.go @@ -6,14 +6,13 @@ import ( "github.com/google/go-cmp/cmp" - "github.com/sourcegraph/sourcegraph/lib/batches" batcheslib "github.com/sourcegraph/sourcegraph/lib/batches" "github.com/sourcegraph/sourcegraph/lib/batches/execution" "github.com/sourcegraph/sourcegraph/lib/batches/execution/cache" "github.com/sourcegraph/sourcegraph/lib/batches/git" ) -var cacheRepo1 = batches.Repository{ +var cacheRepo1 = batcheslib.Repository{ ID: "src-cli", Name: "github.com/sourcegraph/src-cli", BaseRef: "refs/heads/main", @@ -21,7 +20,7 @@ var cacheRepo1 = batches.Repository{ FileMatches: []string{"README.md", "main.go"}, } -var cacheRepo2 = batches.Repository{ +var cacheRepo2 = batcheslib.Repository{ ID: "sourcegraph", Name: "github.com/sourcegraph/sourcegraph", BaseRef: "refs/heads/main-2", diff --git a/internal/batches/workspace/bind_workspace_nonwin_test.go b/internal/batches/workspace/bind_workspace_nonwin_test.go index 2943dcd598..238a884f13 100644 --- a/internal/batches/workspace/bind_workspace_nonwin_test.go +++ b/internal/batches/workspace/bind_workspace_nonwin_test.go @@ -1,5 +1,4 @@ //go:build !windows -// +build !windows package workspace diff --git a/internal/batches/workspace/workspace.go b/internal/batches/workspace/workspace.go index b13f0b24dc..8aaf3b3234 100644 --- a/internal/batches/workspace/workspace.go +++ b/internal/batches/workspace/workspace.go @@ -56,11 +56,12 @@ const ( func NewCreator(ctx context.Context, preference, cacheDir, tempDir string, images map[string]docker.Image) (Creator, CreatorType) { var workspaceType CreatorType - if preference == "volume" { + switch preference { + case "volume": workspaceType = CreatorTypeVolume - } else if preference == "bind" { + case "bind": workspaceType = CreatorTypeBind - } else { + default: workspaceType = BestCreatorType(ctx, images) } diff --git a/internal/validate/kube/kube.go b/internal/validate/kube/kube.go index a1382e9116..ae7555b323 100644 --- a/internal/validate/kube/kube.go +++ b/internal/validate/kube/kube.go @@ -379,13 +379,14 @@ func CurrentContextSetTo(clusterService string) error { return err } - if clusterService == "gke" { + switch clusterService { + case "gke": got := strings.Split(currentContext, "_")[0] want := clusterService if got != want { return errors.New("no gke cluster configured") } - } else if clusterService == "eks" { + case "eks": got := strings.Split(currentContext, ":") want := []string{"arn", "aws", clusterService} @@ -399,7 +400,7 @@ func CurrentContextSetTo(clusterService string) error { return errors.New("no eks cluster configured") } } - } else if clusterService == "aks" { + case "aks": colons := strings.Split(currentContext, ":") // aws string underscores := strings.Split(currentContext, "_") // gke string