Skip to content

Commit 152b766

Browse files
committed
New lint rules
1 parent 4e6d656 commit 152b766

Some content is hidden

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

85 files changed

+385
-259
lines changed

backend/.golangci.yml

Lines changed: 163 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -1,63 +1,166 @@
1+
---
12
linters:
2-
enable:
3-
- bodyclose
4-
- errcheck
5-
- gosimple
6-
- govet
7-
- gosec
8-
- goconst
9-
- gocritic
10-
- gocyclo
11-
- gofmt
12-
- goimports
13-
- ineffassign
14-
- misspell
15-
- nakedret
16-
- prealloc
17-
#- revive
18-
- staticcheck
19-
- typecheck
20-
- unused
21-
- unconvert
22-
- unparam
3+
enable:
4+
# Prevents against memory leaks in production caused by not closing
5+
# file handle
6+
- bodyclose
7+
# Detects cloned code. DRY is good programming practice. Can cause issues
8+
# with testing code where simplicity is preferred over duplication.
9+
# Disabled for test code.
10+
# - dupl
11+
# Detects unchecked errors in go programs. These unchecked errors can be
12+
# critical bugs in some cases.
13+
- errcheck
14+
# Simplifies go code.
15+
- gosimple
16+
# Controls Go package import order and makes it always deterministic.
17+
- gci
18+
# Reports suspicious constructs, maintained by goteam. e.g. Printf unused
19+
# params not caught at compile time.
20+
- govet
21+
# Detect security issues with gocode. Use of secrets in code or obsolete
22+
# security algorithms. It's imaged heuristic methods are used in finding
23+
# problems. If issues with rules are found particular rules can be disabled
24+
# as required. Could possibility cause issues with testing.
25+
# Disabled for test code.
26+
- gosec
27+
# Detect repeated strings that could be replaced by a constant
28+
- goconst
29+
# Misc linters missing from other projects. Grouped into 3 categories
30+
# diagnostics, style and performance
31+
- gocritic
32+
# Limits code cyclomatic complexity
33+
- gocyclo
34+
# Detects if code needs to be gofmt'd
35+
- gofmt
36+
# Detects unused go package imports
37+
- goimports
38+
# Detects style mistakes not correctness. Golint is meant to carry out the
39+
# stylistic conventions put forth in Effective Go and CodeReviewComments.
40+
# golint has false positives and false negatives and can be tweaked.
41+
- revive
42+
# Detects ineffectual assignments in code
43+
- ineffassign
44+
# Reports long lines
45+
# - lll
46+
# Detect commonly misspelled english words in comments
47+
- misspell
48+
# Detect naked returns on non-trivial functions, and conform with
49+
# Go CodeReviewComments
50+
- nakedret
51+
# Detect slice allocations that can be preallocated
52+
- prealloc
53+
# Misc collection of static analysis tools
54+
- staticcheck
55+
# Detects unused struct fields
56+
# - structcheck
57+
# Parses and typechecks the code like the go compiler
58+
- typecheck
59+
# Detects unused constants, variables, functions and types
60+
- unused
61+
# Remove unnecessary type conversions
62+
- unconvert
63+
# Remove unnecessary(unused) function parameters
64+
- unparam
2365
linters-settings:
24-
gosec:
25-
excludes:
26-
- G115
27-
errcheck:
28-
exclude-functions:
29-
- fmt.Fprint
30-
- fmt.Fprintf
31-
goconst:
32-
# minimal length of string constant
33-
# default: 3
34-
min-len: 2
35-
# minimum number of occurrences of string constant
36-
# default: 3
37-
min-occurences: 2
38-
misspell:
39-
locale: UK
40-
ignore-words:
41-
- color
66+
errcheck:
67+
exclude-functions:
68+
- fmt.Fprint
69+
- fmt.Fprintf
70+
gci:
71+
sections:
72+
- standard # Standard section: captures all standard packages.
73+
- localmodule # Local module section: contains all local packages.
74+
# - prefix(gogs.jc21.com) # Prefixed gerrit.lan packages (jumgo).
75+
- default # Everything else (github.com, golang.org, etc).
76+
- blank # Blank section: contains all blank imports.
77+
custom-order: true
78+
goconst:
79+
# minimal length of string constant
80+
# default: 3
81+
min-len: 2
82+
# minimum number of occurrences of string constant
83+
# default: 3
84+
min-occurences: 2
85+
revive:
86+
enable-all-rules: true
87+
rules:
88+
- name: unchecked-type-assertion
89+
disabled: true
90+
# handled by goconst
91+
- name: add-constant
92+
disabled: true
93+
# cant limit this arbitrarily
94+
- name: argument-limit
95+
disabled: true
96+
# handled by gocyclo
97+
- name: cognitive-complexity
98+
disabled: true
99+
# false positive for Exported vs non-exported functions of the same name
100+
- name: confusing-naming
101+
disabled: true
102+
# false positives for "" - which is the nil value of a string (also 0)
103+
- name: confusing-results
104+
disabled: true
105+
# handled by gocyclo
106+
- name: cyclomatic
107+
disabled: true
108+
# have comments on exported functions but not on vars/types/constants
109+
- name: exported
110+
arguments:
111+
- "disableChecksOnConstants"
112+
- "disableChecksOnTypes"
113+
- "disableChecksOnVariables"
114+
# false positives on bool params
115+
- name: flag-parameter
116+
disabled: true
117+
# extreme verticalization can happen
118+
- name: function-length
119+
disabled: true
120+
# can false positive for non-getters
121+
- name: get-return
122+
disabled: true
123+
# only allows lowercase names
124+
- name: import-alias-naming
125+
disabled: true
126+
# handled by lll
127+
- name: line-length-limit
128+
disabled: true
129+
# don't want to arbitrarily limit this
130+
# many places have specific model.go files to contain all structs
131+
- name: max-public-structs
132+
disabled: true
133+
# disable package-comments
134+
- name: package-comments
135+
disabled: true
136+
# this is handled by errcheck
137+
- name: unhandled-error
138+
disabled: true
139+
- name: function-result-limit
140+
disabled: true
42141
issues:
43-
# Maximum count of issues with the same text. Set to 0 to disable. Default is 3.
44-
# We have chosen an arbitrary value that works based on practical usage.
45-
max-same: 20
46-
# See cmdline flag documentation for more info about default excludes --exclude-use-default
47-
# Nothing is excluded by default
48-
exclude-use-default: false
49-
# Excluding configuration per-path, per-linter, per-text and per-source
50-
exclude-rules:
51-
# Exclude some linters from running on tests files. # TODO: Add examples why this is good
52-
53-
- path: _test\.go
54-
linters:
55-
# Tests should be simple? Add example why this is good?
56-
- gocyclo
57-
# Error checking adds verbosity and complexity for minimal value
58-
- errcheck
59-
# Table test encourage duplication in defining the table tests.
60-
- dupl
61-
# Hard coded example tokens, SQL injection and other bad practices may
62-
# want to be tested
63-
- gosec
142+
# Maximum count of issues with the same text. Set to 0 to disable. Default
143+
# is 3. We have chosen an arbitrary value that works based on practical usage.
144+
max-same: 20
145+
# See cmdline flag documentation for more info about default excludes
146+
# --exclude-use-default. Nothing is excluded by default
147+
exclude-use-default: false
148+
# Excluding configuration per-path, per-linter, per-text and per-source
149+
exclude-rules:
150+
# Exclude some linters from running on tests files.
151+
# TODO: Add examples why this is good
152+
- path: _test\.go
153+
linters:
154+
# Tests should be simple? Add example why this is good?
155+
- gocyclo
156+
# Error checking adds verbosity and complexity for minimal value
157+
- errcheck
158+
# Table test encourage duplication in defining the table tests.
159+
- dupl
160+
# Hard coded example tokens, SQL injection and other bad practices may
161+
# want to be tested
162+
- gosec
163+
# Test data can long
164+
# - lll
165+
run:
166+
go: '1.23'

backend/internal/api/handler/auth.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,17 @@ package handler
33
import (
44
"encoding/json"
55
"net/http"
6-
h "npm/internal/api/http"
7-
"npm/internal/errors"
8-
"npm/internal/logger"
96
"slices"
107
"time"
118

129
c "npm/internal/api/context"
10+
h "npm/internal/api/http"
1311
"npm/internal/entity/auth"
1412
"npm/internal/entity/setting"
1513
"npm/internal/entity/user"
14+
"npm/internal/errors"
1615
njwt "npm/internal/jwt"
16+
"npm/internal/logger"
1717

1818
"gorm.io/gorm"
1919
)

backend/internal/api/handler/certificates.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ func getCertificateFromRequest(w http.ResponseWriter, r *http.Request) *certific
151151

152152
// fillObjectFromBody has some reusable code for all endpoints that
153153
// have a certificate id in the url. it will write errors to the output.
154-
func fillObjectFromBody(w http.ResponseWriter, r *http.Request, validationSchema string, o interface{}) bool {
154+
func fillObjectFromBody(w http.ResponseWriter, r *http.Request, validationSchema string, o any) bool {
155155
bodyBytes, _ := r.Context().Value(c.BodyCtxKey).([]byte)
156156

157157
if validationSchema != "" {
@@ -176,10 +176,10 @@ func fillObjectFromBody(w http.ResponseWriter, r *http.Request, validationSchema
176176
return true
177177
}
178178

179-
func configureCertificate(c certificate.Model) {
179+
func configureCertificate(cert certificate.Model) {
180180
err := jobqueue.AddJob(jobqueue.Job{
181181
Name: "RequestCertificate",
182-
Action: c.Request,
182+
Action: cert.Request,
183183
})
184184
if err != nil {
185185
logger.Error("ConfigureCertificateError", err)

backend/internal/api/handler/config.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package handler
22

33
import (
44
"net/http"
5+
56
h "npm/internal/api/http"
67
"npm/internal/config"
78
)

backend/internal/api/handler/health.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package handler
22

33
import (
44
"net/http"
5+
56
"npm/internal/acme"
67
h "npm/internal/api/http"
78
"npm/internal/config"

backend/internal/api/handler/helpers.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ func getQueryVarInt(r *http.Request, varName string, required bool, defaultValue
5656
}
5757

5858
func getURLParamInt(r *http.Request, varName string) (uint, error) {
59-
var defaultValue uint = 0
59+
var defaultValue uint
6060

6161
required := true
6262
paramStr := chi.URLParam(r, varName)

backend/internal/api/handler/helpers_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,10 @@ package handler
33
import (
44
"net/http"
55
"net/http/httptest"
6-
"npm/internal/model"
76
"testing"
87

8+
"npm/internal/model"
9+
910
"github.com/stretchr/testify/assert"
1011
)
1112

backend/internal/api/handler/hosts.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,7 @@ func GetHostNginxConfig(format string) func(http.ResponseWriter, *http.Request)
192192
return
193193
}
194194
if format == "text" {
195-
h.ResultResponseText(w, r, http.StatusOK, content)
195+
h.ResultResponseText(w, http.StatusOK, content)
196196
return
197197
}
198198
h.ResultResponseJSON(w, r, http.StatusOK, content)
@@ -202,11 +202,11 @@ func GetHostNginxConfig(format string) func(http.ResponseWriter, *http.Request)
202202
}
203203
}
204204

205-
func configureHost(h host.Model) {
205+
func configureHost(hst host.Model) {
206206
err := jobqueue.AddJob(jobqueue.Job{
207207
Name: "NginxConfigureHost",
208208
Action: func() error {
209-
return nginx.ConfigureHost(h)
209+
return nginx.ConfigureHost(hst)
210210
},
211211
})
212212
if err != nil {

backend/internal/api/handler/schema.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import (
1212
"npm/internal/config"
1313
"npm/internal/logger"
1414

15-
jsref "github.com/jc21/jsref"
15+
"github.com/jc21/jsref"
1616
"github.com/jc21/jsref/provider"
1717
)
1818

@@ -24,7 +24,7 @@ var (
2424
// Schema simply reads the swagger schema from disk and returns is raw
2525
// Route: GET /schema
2626
func Schema() func(http.ResponseWriter, *http.Request) {
27-
return func(w http.ResponseWriter, r *http.Request) {
27+
return func(w http.ResponseWriter, _ *http.Request) {
2828
w.Header().Set("Content-Type", "application/json; charset=utf-8")
2929
w.WriteHeader(http.StatusOK)
3030
fmt.Fprint(w, string(getSchema()))
@@ -42,8 +42,8 @@ func getSchema() []byte {
4242
swaggerSchema = []byte(strings.ReplaceAll(string(swaggerSchema), "{{VERSION}}", config.Version))
4343

4444
// Dereference the JSON Schema:
45-
var schema interface{}
46-
if err := json.Unmarshal(swaggerSchema, &schema); err != nil {
45+
var sch any
46+
if err := json.Unmarshal(swaggerSchema, &sch); err != nil {
4747
logger.Error("SwaggerUnmarshalError", err)
4848
return nil
4949
}
@@ -55,7 +55,7 @@ func getSchema() []byte {
5555
logger.Error("SchemaProviderError", err)
5656
}
5757

58-
result, err := resolver.Resolve(schema, "", []jsref.Option{jsref.WithRecursiveResolution(true)}...)
58+
result, err := resolver.Resolve(sch, "", []jsref.Option{jsref.WithRecursiveResolution(true)}...)
5959
if err != nil {
6060
logger.Error("SwaggerResolveError", err)
6161
} else {

backend/internal/api/handler/upstreams.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ func CreateUpstream() func(http.ResponseWriter, *http.Request) {
9595
}
9696
}
9797

98-
// UpdateHost updates a host
98+
// UpdateUpstream updates a stream
9999
// Route: PUT /upstreams/{upstreamID}
100100
func UpdateUpstream() func(http.ResponseWriter, *http.Request) {
101101
return func(w http.ResponseWriter, r *http.Request) {
@@ -167,7 +167,7 @@ func DeleteUpstream() func(http.ResponseWriter, *http.Request) {
167167
}
168168
}
169169

170-
// GetHostNginxConfig will return a Host's nginx config from disk
170+
// GetUpstreamNginxConfig will return a Host's nginx config from disk
171171
// Route: GET /upstreams/{upstreamID}/nginx-config
172172
// Route: GET /upstreams/{upstreamID}/nginx-config.txt
173173
func GetUpstreamNginxConfig(format string) func(http.ResponseWriter, *http.Request) {
@@ -191,7 +191,7 @@ func GetUpstreamNginxConfig(format string) func(http.ResponseWriter, *http.Reque
191191
return
192192
}
193193
if format == "text" {
194-
h.ResultResponseText(w, r, http.StatusOK, content)
194+
h.ResultResponseText(w, http.StatusOK, content)
195195
return
196196
}
197197
h.ResultResponseJSON(w, r, http.StatusOK, content)

0 commit comments

Comments
 (0)