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 8 commits
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
32 changes: 24 additions & 8 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,36 @@ linters-settings:
errcheck:
ignore: fmt:.*,go.uber.org/zap/zapcore:^Add.*
ignoretests: true
gci:
sections:
- standard # Standard section: captures all standard packages.
- default # Default section: contains all imports that could not be matched to another section type.
- prefix(github.com/caddyserver/caddy/v2/cmd) # ensure that this is always at the top and always has a line break.
- prefix(github.com/caddyserver/caddy) # Custom section: groups all imports with the specified Prefix.
# Skip generated files.
# Default: true
skip-generated: true
# Enable custom order of sections.
# If `true`, make the section order the same as the order of `sections`.
# Default: false
custom-order: true

linters:
disable-all: true
enable:
- bodyclose
- errcheck
- gofmt
- goimports
- gci
- gofumpt
- gosec
- gosimple
- govet
- ineffassign
- misspell
- prealloc
- revive
- staticcheck
- thelper
- typecheck
- unconvert
- unused
Expand All @@ -38,7 +53,6 @@ linters:
# - godot
# - godox
# - goerr113
# - gofumpt
# - goheader
# - golint
# - gomnd
Expand Down Expand Up @@ -75,25 +89,27 @@ output:
print-linter-name: true

issues:
max-issues-per-linter: 10000
max-same-issues: 10000
exclude-rules:
# we aren't calling unknown URL
- text: "G107" # G107: Url provided to HTTP request as taint input
- text: 'G107' # G107: Url provided to HTTP request as taint input
linters:
- gosec
# as a web server that's expected to handle any template, this is totally in the hands of the user.
- text: "G203" # G203: Use of unescaped data in HTML templates
- text: 'G203' # G203: Use of unescaped data in HTML templates
linters:
- gosec
# we're shelling out to known commands, not relying on user-defined input.
- text: "G204" # G204: Audit use of command execution
- text: 'G204' # G204: Audit use of command execution
linters:
- gosec
# the choice of weakrand is deliberate, hence the named import "weakrand"
- path: modules/caddyhttp/reverseproxy/selectionpolicies.go
text: "G404" # G404: Insecure random number source (rand)
text: 'G404' # G404: Insecure random number source (rand)
linters:
- gosec
- path: modules/caddyhttp/reverseproxy/streaming.go
text: "G404" # G404: Insecure random number source (rand)
text: 'G404' # G404: Insecure random number source (rand)
linters:
- gosec
6 changes: 3 additions & 3 deletions admin.go
Original file line number Diff line number Diff line change
Expand Up @@ -1022,7 +1022,7 @@ func handleConfig(w http.ResponseWriter, r *http.Request) error {
return nil
}

func handleConfigID(w http.ResponseWriter, r *http.Request) error {
func handleConfigID(_ http.ResponseWriter, r *http.Request) error {
idPath := r.URL.Path

parts := strings.Split(idPath, "/")
Expand Down Expand Up @@ -1058,7 +1058,7 @@ func handleConfigID(w http.ResponseWriter, r *http.Request) error {
return errInternalRedir
}

func handleStop(w http.ResponseWriter, r *http.Request) error {
func handleStop(_ http.ResponseWriter, r *http.Request) error {
if r.Method != http.MethodPost {
return APIError{
HTTPStatus: http.StatusMethodNotAllowed,
Expand Down Expand Up @@ -1346,7 +1346,7 @@ var (
// will get deleted before the process gracefully exits.
func PIDFile(filename string) error {
pid := []byte(strconv.Itoa(os.Getpid()) + "\n")
err := os.WriteFile(filename, pid, 0600)
err := os.WriteFile(filename, pid, 0o600)
if err != nil {
return err
}
Expand Down
9 changes: 5 additions & 4 deletions caddy.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,11 @@ import (
"sync/atomic"
"time"

"github.com/caddyserver/caddy/v2/notify"
"github.com/caddyserver/certmagic"
"github.com/google/uuid"
"go.uber.org/zap"

"github.com/caddyserver/caddy/v2/notify"
)

// Config is the top (or beginning) of the Caddy configuration structure.
Expand Down Expand Up @@ -356,13 +357,13 @@ func unsyncedDecodeAndRun(cfgJSON []byte, allowPersist bool) error {
newCfg.Admin.Config.Persist == nil ||
*newCfg.Admin.Config.Persist) {
dir := filepath.Dir(ConfigAutosavePath)
err := os.MkdirAll(dir, 0700)
err := os.MkdirAll(dir, 0o700)
if err != nil {
Log().Error("unable to create folder for config autosave",
zap.String("dir", dir),
zap.Error(err))
} else {
err := os.WriteFile(ConfigAutosavePath, cfgJSON, 0600)
err := os.WriteFile(ConfigAutosavePath, cfgJSON, 0o600)
if err == nil {
Log().Info("autosaved config (load with --resume flag)", zap.String("file", ConfigAutosavePath))
} else {
Expand Down Expand Up @@ -831,7 +832,7 @@ func InstanceID() (uuid.UUID, error) {
if err != nil {
return uuid, err
}
err = os.WriteFile(uuidFilePath, []byte(uuid.String()), 0600)
err = os.WriteFile(uuidFilePath, []byte(uuid.String()), 0o600)
return uuid, err
} else if err != nil {
return [16]byte{}, err
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
3 changes: 2 additions & 1 deletion caddyconfig/caddyfile/importargs.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,9 @@ import (
"strconv"
"strings"

"github.com/caddyserver/caddy/v2"
"go.uber.org/zap"

"github.com/caddyserver/caddy/v2"
)

// parseVariadic determines if the token is a variadic placeholder,
Expand Down
3 changes: 3 additions & 0 deletions caddyconfig/caddyfile/importgraph.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ func (i *importGraph) addNode(name string) {
}
i.nodes[name] = true
}

func (i *importGraph) addNodes(names []string) {
for _, name := range names {
i.addNode(name)
Expand All @@ -43,6 +44,7 @@ func (i *importGraph) addNodes(names []string) {
func (i *importGraph) removeNode(name string) {
delete(i.nodes, name)
}

func (i *importGraph) removeNodes(names []string) {
for _, name := range names {
i.removeNode(name)
Expand Down Expand Up @@ -73,6 +75,7 @@ func (i *importGraph) addEdge(from, to string) error {
i.edges[from] = append(i.edges[from], to)
return nil
}

func (i *importGraph) addEdges(from string, tos []string) error {
for _, to := range tos {
err := i.addEdge(from, to)
Expand Down
12 changes: 6 additions & 6 deletions caddyconfig/caddyfile/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,9 @@ import (
"path/filepath"
"strings"

"github.com/caddyserver/caddy/v2"
"go.uber.org/zap"

"github.com/caddyserver/caddy/v2"
)

// Parse parses the input just enough to group tokens, in
Expand Down Expand Up @@ -445,7 +446,7 @@ func (p *parser) doImport(nesting int) error {

var (
maybeSnippet bool
maybeSnippetId bool
maybeSnippetID bool
index int
)

Expand All @@ -471,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 Expand Up @@ -565,7 +566,6 @@ func (p *parser) doSingleImport(importFile string) ([]Token, error) {
// are loaded into the current server block for later use
// by directive setup functions.
func (p *parser) directive() error {

// a segment is a list of tokens associated with this directive
var segment Segment

Expand Down
9 changes: 6 additions & 3 deletions caddyconfig/httpcaddyfile/addresses.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,11 @@ import (
"strings"
"unicode"

"github.com/caddyserver/certmagic"

"github.com/caddyserver/caddy/v2"
"github.com/caddyserver/caddy/v2/caddyconfig/caddyfile"
"github.com/caddyserver/caddy/v2/modules/caddyhttp"
"github.com/caddyserver/certmagic"
)

// mapAddressToServerBlocks returns a map of listener address to list of server
Expand Down Expand Up @@ -77,7 +78,8 @@ import (
// multiple addresses to the same lists of server blocks (a many:many mapping).
// (Doing this is essentially a map-reduce technique.)
func (st *ServerType) mapAddressToServerBlocks(originalServerBlocks []serverBlock,
options map[string]any) (map[string][]serverBlock, error) {
options map[string]any,
) (map[string][]serverBlock, error) {
sbmap := make(map[string][]serverBlock)

for i, sblock := range originalServerBlocks {
Expand Down Expand Up @@ -187,7 +189,8 @@ func (st *ServerType) consolidateAddrMappings(addrToServerBlocks map[string][]se
// listenerAddrsForServerBlockKey essentially converts the Caddyfile
// site addresses to Caddy listener addresses for each server block.
func (st *ServerType) listenerAddrsForServerBlockKey(sblock serverBlock, key string,
options map[string]any) ([]string, error) {
options map[string]any,
) ([]string, error) {
addr, err := ParseAddress(key)
if err != nil {
return nil, fmt.Errorf("parsing key: %v", err)
Expand Down
7 changes: 4 additions & 3 deletions caddyconfig/httpcaddyfile/builtins.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,15 @@ import (
"strings"
"time"

"github.com/caddyserver/certmagic"
"github.com/mholt/acmez/acme"
"go.uber.org/zap/zapcore"

"github.com/caddyserver/caddy/v2"
"github.com/caddyserver/caddy/v2/caddyconfig"
"github.com/caddyserver/caddy/v2/caddyconfig/caddyfile"
"github.com/caddyserver/caddy/v2/modules/caddyhttp"
"github.com/caddyserver/caddy/v2/modules/caddytls"
"github.com/caddyserver/certmagic"
"github.com/mholt/acmez/acme"
"go.uber.org/zap/zapcore"
)

func init() {
Expand Down
3 changes: 2 additions & 1 deletion caddyconfig/httpcaddyfile/directives.go
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,8 @@ func (h Helper) ExtractMatcherSet() (caddy.ModuleMap, error) {

// NewRoute returns config values relevant to creating a new HTTP route.
func (h Helper) NewRoute(matcherSet caddy.ModuleMap,
handler caddyhttp.MiddlewareHandler) []ConfigValue {
handler caddyhttp.MiddlewareHandler,
) []ConfigValue {
mod, err := caddy.GetModule(caddy.GetModuleID(handler))
if err != nil {
*h.warnings = append(*h.warnings, caddyconfig.Warning{
Expand Down
18 changes: 10 additions & 8 deletions caddyconfig/httpcaddyfile/httptype.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,15 @@ import (
"strconv"
"strings"

"go.uber.org/zap"
"golang.org/x/exp/slices"

"github.com/caddyserver/caddy/v2"
"github.com/caddyserver/caddy/v2/caddyconfig"
"github.com/caddyserver/caddy/v2/caddyconfig/caddyfile"
"github.com/caddyserver/caddy/v2/modules/caddyhttp"
"github.com/caddyserver/caddy/v2/modules/caddypki"
"github.com/caddyserver/caddy/v2/modules/caddytls"
"go.uber.org/zap"
"golang.org/x/exp/slices"
)

func init() {
Expand All @@ -49,8 +50,7 @@ type App struct {
}

// ServerType can set up a config from an HTTP Caddyfile.
type ServerType struct {
}
type ServerType struct{}

// Setup makes a config from the tokens.
func (st ServerType) Setup(
Expand Down Expand Up @@ -1059,8 +1059,8 @@ func appendSubrouteToRouteList(routeList caddyhttp.RouteList,
subroute *caddyhttp.Subroute,
matcherSetsEnc []caddy.ModuleMap,
p sbAddrAssociation,
warnings *[]caddyconfig.Warning) caddyhttp.RouteList {

warnings *[]caddyconfig.Warning,
) caddyhttp.RouteList {
// nothing to do if... there's nothing to do
if len(matcherSetsEnc) == 0 && len(subroute.Routes) == 0 && subroute.Errors == nil {
return routeList
Expand Down Expand Up @@ -1608,8 +1608,10 @@ type sbAddrAssociation struct {
serverBlocks []serverBlock
}

const matcherPrefix = "@"
const namedRouteKey = "named_route"
const (
matcherPrefix = "@"
namedRouteKey = "named_route"
)

// Interface guard
var _ caddyfile.ServerType = (*ServerType)(nil)
7 changes: 4 additions & 3 deletions caddyconfig/httpcaddyfile/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,13 @@ package httpcaddyfile
import (
"strconv"

"github.com/caddyserver/certmagic"
"github.com/mholt/acmez/acme"

"github.com/caddyserver/caddy/v2"
"github.com/caddyserver/caddy/v2/caddyconfig"
"github.com/caddyserver/caddy/v2/caddyconfig/caddyfile"
"github.com/caddyserver/caddy/v2/modules/caddytls"
"github.com/caddyserver/certmagic"
"github.com/mholt/acmez/acme"
)

func init() {
Expand Down Expand Up @@ -58,7 +59,7 @@ func init() {
RegisterGlobalOption("persist_config", parseOptPersistConfig)
}

func parseOptTrue(d *caddyfile.Dispenser, _ any) (any, error) { return true, nil }
func parseOptTrue(_ *caddyfile.Dispenser, _ any) (any, error) { return true, nil }

func parseOptHTTPPort(d *caddyfile.Dispenser, _ any) (any, error) {
var httpPort int
Expand Down
Loading