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

chore: use revive linter #5712

Closed
wants to merge 11 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
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
1 change: 1 addition & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ linters:
- ineffassign
- misspell
- prealloc
- revive
- staticcheck
- typecheck
- unconvert
Expand Down
4 changes: 2 additions & 2 deletions caddyconfig/caddyfile/dispenser.go
Original file line number Diff line number Diff line change
Expand Up @@ -236,8 +236,8 @@ func (d *Dispenser) ScalarVal() any {
if num, err := strconv.ParseFloat(text, 64); err == nil {
return num
}
if bool, err := strconv.ParseBool(text); err == nil {
return bool
if boolean, err := strconv.ParseBool(text); err == nil {
return boolean
}
return text
}
Expand Down
6 changes: 3 additions & 3 deletions caddyconfig/caddyfile/formatter.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,10 +81,10 @@ func Format(input []byte) []byte {
space = true
nextLine()
continue
} else {
write(ch)
continue
}
write(ch)
continue

}

if !escaped && ch == '\\' {
Expand Down
8 changes: 4 additions & 4 deletions caddyconfig/caddyfile/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -446,7 +446,7 @@ func (p *parser) doImport(nesting int) error {

var (
maybeSnippet bool
maybeSnippetId bool
maybeSnippetID bool
index int
)

Expand All @@ -472,16 +472,16 @@ func (p *parser) doImport(nesting int) error {
}

if index == 0 && len(token.Text) >= 3 && strings.HasPrefix(token.Text, "(") && strings.HasSuffix(token.Text, ")") {
maybeSnippetId = true
maybeSnippetID = true
}
}

switch token.Text {
case "{":
nesting++
if index == 1 && maybeSnippetId && nesting == 1 {
if index == 1 && maybeSnippetID && nesting == 1 {
maybeSnippet = true
maybeSnippetId = false
maybeSnippetID = false
}
case "}":
nesting--
Expand Down
8 changes: 4 additions & 4 deletions caddyconfig/httploader.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ func (hl HTTPLoader) LoadConfig(ctx caddy.Context) ([]byte, error) {
}
}

resp, err := doHttpCallWithRetries(ctx, client, req)
resp, err := doHTTPCallWithRetries(ctx, client, req)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -135,7 +135,7 @@ func (hl HTTPLoader) LoadConfig(ctx caddy.Context) ([]byte, error) {
return result, nil
}

func attemptHttpCall(client *http.Client, request *http.Request) (*http.Response, error) {
func attemptHTTPCall(client *http.Client, request *http.Request) (*http.Response, error) {
resp, err := client.Do(request)
if err != nil {
return nil, fmt.Errorf("problem calling http loader url: %v", err)
Expand All @@ -146,13 +146,13 @@ func attemptHttpCall(client *http.Client, request *http.Request) (*http.Response
return resp, nil
}

func doHttpCallWithRetries(ctx caddy.Context, client *http.Client, request *http.Request) (*http.Response, error) {
func doHTTPCallWithRetries(ctx caddy.Context, client *http.Client, request *http.Request) (*http.Response, error) {
var resp *http.Response
var err error
const maxAttempts = 10

for i := 0; i < maxAttempts; i++ {
resp, err = attemptHttpCall(client, request)
resp, err = attemptHTTPCall(client, request)
if err != nil && i < maxAttempts-1 {
select {
case <-time.After(time.Millisecond * 500):
Expand Down
2 changes: 1 addition & 1 deletion cmd/commandfuncs.go
Original file line number Diff line number Diff line change
Expand Up @@ -630,7 +630,7 @@ func AdminAPIRequest(adminAddr, method, uri string, headers http.Header, body io
if err != nil {
return nil, fmt.Errorf("making request: %v", err)
}
if parsedAddr.IsUnixNetwork() {
if parsedAddr.IsUnixNetwork() { //nolint:revive
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What happens otherwise?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the revive linter prompts us to remove the empty code block that is currently just comments.

// We used to conform to RFC 2616 Section 14.26 which requires
// an empty host header when there is no host, as is the case
// with unix sockets. However, Go required a Host value so we
Expand Down
14 changes: 7 additions & 7 deletions listeners.go
Original file line number Diff line number Diff line change
Expand Up @@ -479,8 +479,8 @@ func ListenQUIC(ln net.PacketConn, tlsConf *tls.Config, activeRequests *int64) (
sqtc := newSharedQUICTLSConfig(tlsConf)
// http3.ConfigureTLSConfig only uses this field and tls App sets this field as well
//nolint:gosec
quicTlsConfig := &tls.Config{GetConfigForClient: sqtc.getConfigForClient}
earlyLn, err := quic.ListenEarly(ln, http3.ConfigureTLSConfig(quicTlsConfig), &quic.Config{
quicTLSConfig := &tls.Config{GetConfigForClient: sqtc.getConfigForClient}
earlyLn, err := quic.ListenEarly(ln, http3.ConfigureTLSConfig(quicTLSConfig), &quic.Config{
Allow0RTT: true,
RequireAddressValidation: func(clientAddr net.Addr) bool {
var highLoad bool
Expand Down Expand Up @@ -539,14 +539,14 @@ type contextAndCancelFunc struct {
type sharedQUICTLSConfig struct {
rmu sync.RWMutex
tlsConfs map[*tls.Config]contextAndCancelFunc
activeTlsConf *tls.Config
activeTLSConf *tls.Config
}

// newSharedQUICTLSConfig creates a new sharedQUICTLSConfig
func newSharedQUICTLSConfig(tlsConfig *tls.Config) *sharedQUICTLSConfig {
sqtc := &sharedQUICTLSConfig{
tlsConfs: make(map[*tls.Config]contextAndCancelFunc),
activeTlsConf: tlsConfig,
activeTLSConf: tlsConfig,
}
sqtc.addTLSConfig(tlsConfig)
return sqtc
Expand All @@ -556,7 +556,7 @@ func newSharedQUICTLSConfig(tlsConfig *tls.Config) *sharedQUICTLSConfig {
func (sqtc *sharedQUICTLSConfig) getConfigForClient(ch *tls.ClientHelloInfo) (*tls.Config, error) {
sqtc.rmu.RLock()
defer sqtc.rmu.RUnlock()
return sqtc.activeTlsConf.GetConfigForClient(ch)
return sqtc.activeTLSConf.GetConfigForClient(ch)
}

// addTLSConfig adds tls.Config to the map if not present and returns the corresponding context and its cancelFunc
Expand All @@ -577,11 +577,11 @@ func (sqtc *sharedQUICTLSConfig) addTLSConfig(tlsConfig *tls.Config) (context.Co
defer sqtc.rmu.Unlock()

delete(sqtc.tlsConfs, tlsConfig)
if sqtc.activeTlsConf == tlsConfig {
if sqtc.activeTLSConf == tlsConfig {
// select another tls.Config, if there is none,
// related sharedQuicListener will be destroyed anyway
for tc := range sqtc.tlsConfs {
sqtc.activeTlsConf = tc
sqtc.activeTLSConf = tc
break
}
}
Expand Down
3 changes: 1 addition & 2 deletions modules/caddyhttp/encode/encode.go
Original file line number Diff line number Diff line change
Expand Up @@ -269,9 +269,8 @@ func (rw *responseWriter) Write(p []byte) (int, error) {

if rw.w != nil {
return rw.w.Write(p)
} else {
return rw.ResponseWriter.Write(p)
}
return rw.ResponseWriter.Write(p)
}

// Close writes any remaining buffered response and
Expand Down
6 changes: 3 additions & 3 deletions modules/caddyhttp/ip_range.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,16 +86,16 @@ func (s *StaticIPRange) GetIPRanges(_ *http.Request) []netip.Prefix {
}

// UnmarshalCaddyfile implements caddyfile.Unmarshaler.
func (m *StaticIPRange) UnmarshalCaddyfile(d *caddyfile.Dispenser) error {
func (f *StaticIPRange) UnmarshalCaddyfile(d *caddyfile.Dispenser) error {
faddat marked this conversation as resolved.
Show resolved Hide resolved
if !d.Next() {
return nil
}
for d.NextArg() {
if d.Val() == "private_ranges" {
m.Ranges = append(m.Ranges, PrivateRangesCIDR()...)
f.Ranges = append(f.Ranges, PrivateRangesCIDR()...)
continue
}
m.Ranges = append(m.Ranges, d.Val())
f.Ranges = append(f.Ranges, d.Val())
}
return nil
}
Expand Down
7 changes: 2 additions & 5 deletions modules/caddyhttp/push/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,11 +122,8 @@ func (h Handler) ServeHTTP(w http.ResponseWriter, r *http.Request, next caddyhtt
}

// serve only after pushing!
if err := next.ServeHTTP(lp, r); err != nil {
return err
}

return nil
err := next.ServeHTTP(lp, r)
return err
francislavoie marked this conversation as resolved.
Show resolved Hide resolved
}

func (h Handler) initializePushHeaders(r *http.Request, repl *caddy.Replacer) http.Header {
Expand Down
4 changes: 2 additions & 2 deletions modules/caddyhttp/reverseproxy/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -284,12 +284,12 @@ func cmdReverseProxy(fs caddycmd.Flags) (int, error) {
appsRaw["tls"] = caddyconfig.JSON(tlsApp, nil)
}

var false bool
var falseBool bool
cfg := &caddy.Config{
Admin: &caddy.AdminConfig{
Disabled: true,
Config: &caddy.ConfigSettings{
Persist: &false,
Persist: &falseBool,
},
},
AppsRaw: appsRaw,
Expand Down
2 changes: 1 addition & 1 deletion modules/caddyhttp/reverseproxy/selectionpolicies.go
Original file line number Diff line number Diff line change
Expand Up @@ -770,7 +770,7 @@ func leastRequests(upstreams []*Upstream) *Upstream {
return nil
}
var best []*Upstream
var bestReqs int = -1
bestReqs := -1
Comment on lines 772 to +773
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nitpick but this is actually less symmetrical so it looks less nice. But whatever, it's fine.

for _, upstream := range upstreams {
if upstream == nil {
continue
Expand Down
4 changes: 2 additions & 2 deletions modules/caddyhttp/staticresp.go
Original file line number Diff line number Diff line change
Expand Up @@ -394,12 +394,12 @@ func cmdRespond(fl caddycmd.Flags) (int, error) {
}

// finish building the config
var false bool
var falseBool bool
cfg := &caddy.Config{
Admin: &caddy.AdminConfig{
Disabled: true,
Config: &caddy.ConfigSettings{
Persist: &false,
Persist: &falseBool,
},
},
AppsRaw: caddy.ModuleMap{
Expand Down
22 changes: 11 additions & 11 deletions modules/logging/filters.go
Original file line number Diff line number Diff line change
Expand Up @@ -322,7 +322,7 @@ func (QueryFilter) CaddyModule() caddy.ModuleInfo {
}

// UnmarshalCaddyfile sets up the module from Caddyfile tokens.
func (m *QueryFilter) UnmarshalCaddyfile(d *caddyfile.Dispenser) error {
func (f *QueryFilter) UnmarshalCaddyfile(d *caddyfile.Dispenser) error {
for d.Next() {
for d.NextBlock(0) {
qfa := queryFilterAction{}
Expand Down Expand Up @@ -360,21 +360,21 @@ func (m *QueryFilter) UnmarshalCaddyfile(d *caddyfile.Dispenser) error {
return d.Errf("unrecognized subdirective %s", d.Val())
}

m.Actions = append(m.Actions, qfa)
f.Actions = append(f.Actions, qfa)
}
}
return nil
}

// Filter filters the input field.
func (m QueryFilter) Filter(in zapcore.Field) zapcore.Field {
func (f QueryFilter) Filter(in zapcore.Field) zapcore.Field {
u, err := url.Parse(in.String)
if err != nil {
return in
}

q := u.Query()
for _, a := range m.Actions {
for _, a := range f.Actions {
switch a.Type {
case replaceAction:
for i := range q[a.Parameter] {
Expand Down Expand Up @@ -442,7 +442,7 @@ func (CookieFilter) CaddyModule() caddy.ModuleInfo {
}

// UnmarshalCaddyfile sets up the module from Caddyfile tokens.
func (m *CookieFilter) UnmarshalCaddyfile(d *caddyfile.Dispenser) error {
func (f *CookieFilter) UnmarshalCaddyfile(d *caddyfile.Dispenser) error {
for d.Next() {
for d.NextBlock(0) {
cfa := cookieFilterAction{}
Expand Down Expand Up @@ -480,14 +480,14 @@ func (m *CookieFilter) UnmarshalCaddyfile(d *caddyfile.Dispenser) error {
return d.Errf("unrecognized subdirective %s", d.Val())
}

m.Actions = append(m.Actions, cfa)
f.Actions = append(f.Actions, cfa)
}
}
return nil
}

// Filter filters the input field.
func (m CookieFilter) Filter(in zapcore.Field) zapcore.Field {
func (f CookieFilter) Filter(in zapcore.Field) zapcore.Field {
cookiesSlice, ok := in.Interface.(caddyhttp.LoggableStringArray)
if !ok {
return in
Expand All @@ -500,7 +500,7 @@ func (m CookieFilter) Filter(in zapcore.Field) zapcore.Field {

OUTER:
for _, c := range cookies {
for _, a := range m.Actions {
for _, a := range f.Actions {
if c.Name != a.Name {
continue
}
Expand Down Expand Up @@ -566,13 +566,13 @@ func (f *RegexpFilter) UnmarshalCaddyfile(d *caddyfile.Dispenser) error {
}

// Provision compiles m's regexp.
func (m *RegexpFilter) Provision(ctx caddy.Context) error {
r, err := regexp.Compile(m.RawRegexp)
func (f *RegexpFilter) Provision(ctx caddy.Context) error {
r, err := regexp.Compile(f.RawRegexp)
if err != nil {
return err
}

m.regexp = r
f.regexp = r

return nil
}
Expand Down