Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Linting #359

Merged
merged 38 commits into from
Aug 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
38 commits
Select commit Hold shift + click to select a range
8b8f7e8
chore: bump golangci lint version to 1.54.1
Choraden Aug 14, 2023
c939729
golangci: disable depguard linter
Choraden Aug 14, 2023
a45b624
golangci: disable deprecated linters
Choraden Aug 14, 2023
a10586e
golangci: enable linting in httpbin
Choraden Aug 14, 2023
50dab0b
chore: fix revive linter issues
Choraden Aug 14, 2023
50e8f32
chore: fix gocritic linter issues
Choraden Aug 14, 2023
6ec4a38
cmd/forwarder/httpbin: fix goheader linter issue
Choraden Aug 14, 2023
031c335
utils/httpbin: disable linting for line with io.CopyBuffer
Choraden Aug 14, 2023
b7b984a
golangci: enable linting at internal/martian
Choraden Aug 14, 2023
b47e65f
golangci: disable goheader linter at internal/martian
Choraden Aug 14, 2023
454e49b
golangci: disable gochecknoglobals linter at internal/martian
Choraden Aug 14, 2023
3edbdb9
golangci: disable bodyclose linter
Choraden Aug 16, 2023
9043433
golangci: disable ireturn linter
Choraden Aug 14, 2023
c869381
golangci: disable errcheck linter in martian dir
Choraden Aug 16, 2023
ed51261
golangci: disable gosec linter in martian dir
Choraden Aug 16, 2023
00985ff
golangci: disable forbidigo (use of fmt.Print) at internal/martian/h2…
Choraden Aug 14, 2023
7af05d4
golangci: disable context-key-type check
Choraden Aug 14, 2023
0a05bb5
golangci(funlen linter): tweak settings
Choraden Aug 14, 2023
a8fcee7
golangci(gocognit linter): increase min-complexity to 50
Choraden Aug 16, 2023
c3fbb15
golangci(gocritic linter): add exclusion rule for shadow of url package
Choraden Aug 16, 2023
d6b7dc9
golangci(gocritic linter): disable commentedOutCode check
Choraden Aug 16, 2023
c1ce9f8
golangci(nestif linter): increase min-complexity to 11
Choraden Aug 16, 2023
0bb3cea
martian: fix usestdlibvars linter issues
Choraden Aug 14, 2023
879bbe7
martian: fix gosimple linter issues
Choraden Aug 14, 2023
9fbd1b0
martian: fix staticcheck linter issues
Choraden Aug 14, 2023
5d7405f
martian: fix linter issues in comments
Choraden Aug 14, 2023
ff95a5d
martian: fix errorlint linter issues
Choraden Aug 14, 2023
a0d5d2b
martian: fix nakedret linter issues
Choraden Aug 14, 2023
3f60085
martian: fix gocritic linter issues
Choraden Aug 14, 2023
b6ae613
martian: fix goconst linter issue
Choraden Aug 14, 2023
6438fe8
martian: fix revive linter issues
Choraden Aug 16, 2023
6300ef1
martian: fix unconvert linter issues
Choraden Aug 16, 2023
a4862f6
martian: fix prealloc linter issues
Choraden Aug 16, 2023
a2a90b7
martian: silence force type asserts
Choraden Aug 16, 2023
beb8cb3
martian/context: remove unused setConn method
Choraden Aug 16, 2023
a104817
maritan/mitm: remove unused getCertificate field
Choraden Aug 16, 2023
5efb898
martian/context: fix typo in context key
Choraden Aug 16, 2023
0271af4
ci: remove setup-go action from lint job
Choraden Aug 16, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 0 additions & 5 deletions .github/workflows/go.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,6 @@ jobs:
run: |
cat .version >> $GITHUB_ENV

- name: Setup Go
uses: actions/setup-go@v4
with:
go-version: "${{env.GO_VERSION}}"

- name: Run golangci-lint
uses: golangci/golangci-lint-action@v3
with:
Expand Down
37 changes: 30 additions & 7 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,31 +8,39 @@ run:
build-tags:
- e2e
skip-files:
- httpbin/httpbin.go
- middleware/delegator.go
- utils/cobrautil/templates
- utils/cobrautil/term
- internal/martian
silent: true

linters:
enable-all: true
disable:
- bodyclose
- contextcheck
- cyclop
- deadcode
- depguard
- exhaustive
- exhaustivestruct
- exhaustruct
- gochecknoinits
- godox
- goerr113
- golint
- gomnd
- gomoddirectives
- ifshort
- interfacer
- ireturn
- maligned
- nlreturn
- nonamedreturns
- nosnakecase
- scopelint
- structcheck
- tagliatelle
- varcheck
- varnamelen
- wrapcheck
- wsl
Expand All @@ -42,9 +50,10 @@ linters-settings:
errcheck:
check-blank: true
funlen:
lines: 100
lines: 140
statements: 75
gocognit:
min-complexity: 40
min-complexity: 50
gocyclo:
min-complexity: 40
gocritic:
Expand All @@ -55,7 +64,7 @@ linters-settings:
lll:
line-length: 180
nestif:
min-complexity: 6
min-complexity: 11
goheader:
values:
regexp:
Expand All @@ -74,10 +83,12 @@ issues:
- "can be `expvar.Var`"
- "can be `fmt.Stringer`"
- "missing cases in switch of type Scheme: TunnelScheme"
- "returns interface \\(github.com/dop251/goja\\.Value\\)"
- "returns interface \\(github.com/saucelabs/forwarder/internal/martian\\.(Request|Response)+Modifier\\)"
- "shadow of imported from 'github.com/saucelabs/forwarder/log' package 'log'"
- "string `https?` has \\d+ occurrences"
- "context-keys-type: should not use basic type string as key in context.WithValue"
- "SA1029: should not use built-in type string as key for value; define your own type to avoid collisions"
- "importShadow: shadow of imported package 'url'"
- "commentedOutCode: may want to remove commented-out code"
exclude-rules:
- path: cmd/forwarder/version/version.go
linters:
Expand Down Expand Up @@ -111,6 +122,18 @@ issues:
linters:
- gochecknoglobals

- path: internal/martian
linters:
- goheader
- gochecknoglobals
- errcheck
- gosec

- path: internal/martian/h2/testing
linters:
- forbidigo
text: use of `fmt.Print

- linters:
- thelper
source: "configure: func\\(t \\*testing.T"
Expand Down
2 changes: 1 addition & 1 deletion .version
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
GO_VERSION=1.21.0

X_TOOLS_VERSION=v0.6.0
GOLANGCI_LINT_VERSION=v1.51.1
GOLANGCI_LINT_VERSION=v1.54.1
GORELEASER_VERSION=v1.17.2
GO_LICENSES_VERSION=v1.6.0
10 changes: 5 additions & 5 deletions api.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ func NewAPIHandler(r prometheus.Gatherer, ready func(ctx context.Context) bool,
return a
}

func (h *APIHandler) healthz(w http.ResponseWriter, r *http.Request) {
func (h *APIHandler) healthz(w http.ResponseWriter, _ *http.Request) {
w.WriteHeader(http.StatusOK)
w.Header().Set("Content-Type", "text/plain")
w.Write([]byte("OK"))
Expand All @@ -84,18 +84,18 @@ func (h *APIHandler) readyz(w http.ResponseWriter, r *http.Request) {
}
}

func (h *APIHandler) configz(w http.ResponseWriter, r *http.Request) {
func (h *APIHandler) configz(w http.ResponseWriter, _ *http.Request) {
w.WriteHeader(http.StatusOK)
w.Header().Set("Content-Type", "text/plain")
w.Write([]byte(h.config))
}

func (h *APIHandler) pac(w http.ResponseWriter, r *http.Request) {
func (h *APIHandler) pac(w http.ResponseWriter, _ *http.Request) {
w.Header().Set("Content-Type", "application/x-ns-proxy-autoconfig")
w.Write([]byte(h.script))
}

func (h *APIHandler) version(w http.ResponseWriter, r *http.Request) {
func (h *APIHandler) version(w http.ResponseWriter, _ *http.Request) {
w.Header().Set("Content-Type", "application/json")
v := struct {
Version string `json:"version"`
Expand Down Expand Up @@ -133,7 +133,7 @@ const indexTemplate = `<!DOCTYPE html>
</html>
`

func (h *APIHandler) index(w http.ResponseWriter, r *http.Request) {
func (h *APIHandler) index(w http.ResponseWriter, _ *http.Request) {
w.WriteHeader(http.StatusOK)
w.Header().Set("Content-Type", "text/html")

Expand Down
8 changes: 7 additions & 1 deletion cmd/forwarder/httpbin/httpbin.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
// Copyright 2023 Sauce Labs Inc. All rights reserved.
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at https://mozilla.org/MPL/2.0/.

package httpbin

import (
Expand All @@ -17,7 +23,7 @@ type command struct {
logConfig *log.Config
}

func (c *command) RunE(cmd *cobra.Command, args []string) error {
func (c *command) RunE(cmd *cobra.Command, _ []string) error {
config := bind.DescribeFlags(cmd.Flags())

if f := c.logConfig.File; f != nil {
Expand Down
2 changes: 1 addition & 1 deletion cmd/forwarder/pac/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ type command struct {
logConfig *log.Config
}

func (c *command) RunE(cmd *cobra.Command, args []string) error {
func (c *command) RunE(cmd *cobra.Command, _ []string) error {
config := bind.DescribeFlags(cmd.Flags())

if f := c.logConfig.File; f != nil {
Expand Down
2 changes: 1 addition & 1 deletion cmd/forwarder/ready/ready.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ type command struct {
apiAddr string
}

func (c *command) RunE(cmd *cobra.Command, args []string) error {
func (c *command) RunE(cmd *cobra.Command, _ []string) error {
resp, err := http.Get("http://" + c.apiAddr + "/readyz") //nolint:noctx // net/http.Get must not be called
if err != nil {
return err
Expand Down
2 changes: 1 addition & 1 deletion cmd/forwarder/run/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ type command struct {
goleak bool
}

func (c *command) RunE(cmd *cobra.Command, args []string) error {
func (c *command) RunE(cmd *cobra.Command, _ []string) error {
config := bind.DescribeFlags(cmd.Flags())

if f := c.logConfig.File; f != nil {
Expand Down
2 changes: 1 addition & 1 deletion dialvia/socks5.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func SOCKS5Proxy(dial ContextDialerFunc, proxyURL *url.URL) *SOCKS5ProxyDialer {
}
}

func (d *SOCKS5ProxyDialer) DialContext(ctx context.Context, network, addr string) (net.Conn, error) {
func (d *SOCKS5ProxyDialer) DialContext(_ context.Context, network, addr string) (net.Conn, error) {
u := d.proxyURL.User
var auth *proxy.Auth
if u != nil {
Expand Down
2 changes: 1 addition & 1 deletion header/header.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ var (
func ParseHeader(val string) (Header, error) {
var h Header

if strings.HasPrefix(val, "-") { //nolint:nestif // the branches are very similar
if strings.HasPrefix(val, "-") {
if strings.HasSuffix(val, "*") {
h.Name = val[1 : len(val)-1]
h.Action = RemoveByPrefix
Expand Down
2 changes: 1 addition & 1 deletion http_proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -525,6 +525,6 @@ func (hp *HTTPProxy) Addr() string {
}

// Ready returns true if the server is running and ready to accept requests.
func (hp *HTTPProxy) Ready(ctx context.Context) bool {
func (hp *HTTPProxy) Ready(_ context.Context) bool {
return hp.Addr() != ""
}
2 changes: 1 addition & 1 deletion http_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,6 @@ func (hs *HTTPServer) Addr() string {
}

// Ready returns true if the server is running and ready to accept requests.
func (hs *HTTPServer) Ready(ctx context.Context) bool {
func (hs *HTTPServer) Ready(_ context.Context) bool {
return hs.Addr() != ""
}
18 changes: 4 additions & 14 deletions internal/martian/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,15 +51,15 @@ type Session struct {
vals map[string]any
}

const marianKey string = "martian.Context"
const martianKey string = "martian.Context"

// NewContext returns a context for the in-flight HTTP request.
func NewContext(req *http.Request) *Context {
v := req.Context().Value(marianKey)
v := req.Context().Value(martianKey)
if v == nil {
return nil
}
return v.(*Context)
return v.(*Context) //nolint:forcetypeassert // We know the type.
}

// TestContext builds a new session and associated context and returns the context.
Expand Down Expand Up @@ -150,16 +150,6 @@ func (s *Session) Hijacked() bool {
return s.hijacked
}

// setConn resets the underlying connection and bufio.ReadWriter of the
// session. Used by the proxy when the connection is upgraded to TLS.
func (s *Session) setConn(conn net.Conn, brw *bufio.ReadWriter) {
s.mu.Lock()
defer s.mu.Unlock()

s.conn = conn
s.brw = brw
}

// Get takes key and returns the associated value from the session.
func (s *Session) Get(key string) (any, bool) {
s.mu.RLock()
Expand Down Expand Up @@ -188,7 +178,7 @@ func (ctx *Context) addToContext(rctx context.Context) context.Context {
if rctx == nil {
rctx = context.Background()
}
return context.WithValue(rctx, marianKey, ctx)
return context.WithValue(rctx, martianKey, ctx)
}

// Session returns the session for the context.
Expand Down
13 changes: 6 additions & 7 deletions internal/martian/context_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,14 @@ package martian
import (
"bufio"
"bytes"
"io/ioutil"
"io"
"net"
"net/http"
"testing"
)

func TestContexts(t *testing.T) {
req, err := http.NewRequest("GET", "http://example.com", nil)
req, err := http.NewRequest(http.MethodGet, "http://example.com", http.NoBody)
if err != nil {
t.Fatalf("http.NewRequest(): got %v, want no error", err)
}
Expand Down Expand Up @@ -72,8 +72,7 @@ func TestContexts(t *testing.T) {

func TestContextHijack(t *testing.T) {
rc, wc := net.Pipe()

req, err := http.NewRequest("GET", "http://example.com", nil)
req, err := http.NewRequest(http.MethodGet, "http://example.com", http.NoBody)
if err != nil {
t.Fatalf("http.NewRequest(): got %v, want no error", err)
}
Expand All @@ -99,14 +98,14 @@ func TestContextHijack(t *testing.T) {
}

go func() {
brw.Write([]byte("test message"))
brw.WriteString("test message")
brw.Flush()
conn.Close()
}()

got, err := ioutil.ReadAll(wc)
got, err := io.ReadAll(wc)
if err != nil {
t.Fatalf("ioutil.ReadAll(): got %v, want no error", err)
t.Fatalf("io.ReadAll(): got %v, want no error", err)
}

if want := []byte("test message"); !bytes.Equal(got, want) {
Expand Down
16 changes: 8 additions & 8 deletions internal/martian/fifo/fifo_group_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ func TestModifyRequest(t *testing.T) {

fg.AddRequestModifier(tm)

req, err := http.NewRequest("GET", "/", nil)
req, err := http.NewRequest(http.MethodGet, "/", http.NoBody)
if err != nil {
t.Fatalf("http.NewRequest(): got %v, want no error", err)
}
Expand All @@ -54,11 +54,11 @@ func TestModifyRequestHaltsOnError(t *testing.T) {
tm2 := martiantest.NewModifier()
fg.AddRequestModifier(tm2)

req, err := http.NewRequest("GET", "http://example.com/", nil)
req, err := http.NewRequest(http.MethodGet, "http://example.com/", http.NoBody)
if err != nil {
t.Fatalf("http.NewRequest(): got %v, want no error", err)
}
if err := fg.ModifyRequest(req); err != reqerr {
if err := fg.ModifyRequest(req); !errors.Is(err, reqerr) {
t.Fatalf("fg.ModifyRequest(): got %v, want %v", err, reqerr)
}

Expand All @@ -81,7 +81,7 @@ func TestModifyRequestAggregatesErrors(t *testing.T) {
tm2.RequestError(reqerr2)
fg.AddRequestModifier(tm2)

req, err := http.NewRequest("GET", "http://example.com/", nil)
req, err := http.NewRequest(http.MethodGet, "http://example.com/", http.NoBody)
if err != nil {
t.Fatalf("http.NewRequest(): got %v, want no error", err)
}
Expand Down Expand Up @@ -129,7 +129,7 @@ func TestModifyResponseHaltsOnError(t *testing.T) {
fg.AddResponseModifier(tm2)

res := proxyutil.NewResponse(200, nil, nil)
if err := fg.ModifyResponse(res); err != reserr {
if err := fg.ModifyResponse(res); !errors.Is(err, reserr) {
t.Fatalf("fg.ModifyResponse(): got %v, want %v", err, reserr)
}

Expand All @@ -152,7 +152,7 @@ func TestModifyResponseAggregatesErrors(t *testing.T) {
tm2.ResponseError(reserr2)
fg.AddResponseModifier(tm2)

req, err := http.NewRequest("GET", "http://example.com/", nil)
req, err := http.NewRequest(http.MethodGet, "http://example.com/", http.NoBody)
if err != nil {
t.Fatalf("http.NewRequest(): got %v, want no error", err)
}
Expand Down Expand Up @@ -203,7 +203,7 @@ func TestModifyResponseInlineGroupsAggregateErrors(t *testing.T) {
t.Fatalf("inner groups should be inlined")
}

req, err := http.NewRequest("GET", "http://example.com/", nil)
req, err := http.NewRequest(http.MethodGet, "http://example.com/", http.NoBody)
if err != nil {
t.Fatalf("http.NewRequest(): got %v, want no error", err)
}
Expand Down Expand Up @@ -255,7 +255,7 @@ func TestModifyRequestInlineGroupsAggregateErrors(t *testing.T) {
t.Fatalf("inner groups should be inlined")
}

req, err := http.NewRequest("GET", "http://example.com/", nil)
req, err := http.NewRequest(http.MethodGet, "http://example.com/", http.NoBody)
if err != nil {
t.Fatalf("http.NewRequest(): got %v, want no error", err)
}
Expand Down
Loading