Skip to content

Commit

Permalink
chore(lint): rework lint errors
Browse files Browse the repository at this point in the history
  • Loading branch information
fty4 committed Oct 1, 2024
1 parent 420442e commit 316e312
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 8 deletions.
11 changes: 11 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,17 @@ linters-settings:
- FIXME
gofumpt:
extra-rules: true
depguard:
rules:
testify:
files:
# - $all
- "$test"
allow:
- $gostd
- github.com/stretchr/testify/assert
- github.com/stretchr/testify/require


linters:
enable-all: true
Expand Down
17 changes: 12 additions & 5 deletions ddnswhitelist.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,13 @@ const (
xForwardedFor = "X-Forwarded-For"
)

// Define static error variable.
var (
errNoHostListProvided = errors.New("no host list provided")
errEmptyIPAddress = errors.New("empty IP address")
errParseIPAddress = errors.New("could not parse IP address")
)

// Config the plugin configuration.
type Config struct {
DdnsHostList []string `json:"ddnsHostList,omitempty"` // Add hosts to whitelist
Expand All @@ -38,12 +45,12 @@ type DdnsWhitelist struct {

// New created a new DDNSwhitelist plugin.
func New(_ context.Context, next http.Handler, config *Config, name string) (http.Handler, error) {
logger := NewLogger("info", name, typeName)
logger := newLogger("info", name, typeName)
logger.Debug("Creating middleware")

if len(config.DdnsHostList) == 0 {
logger.Error("no host list provided")
return nil, errors.New("no host list provided")
return nil, errNoHostListProvided
}

return &DdnsWhitelist{
Expand All @@ -55,7 +62,7 @@ func New(_ context.Context, next http.Handler, config *Config, name string) (htt

// ServeHTTP DDNSwhitelist.
func (a *DdnsWhitelist) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
logger := NewLogger("info", a.name, typeName)
logger := newLogger("info", a.name, typeName)

// TODO: this might be scheduled and not requested on every request
// get list of allowed IPs
Expand Down Expand Up @@ -87,12 +94,12 @@ func (a *DdnsWhitelist) ServeHTTP(rw http.ResponseWriter, req *http.Request) {

func (a *allowedIps) contains(ipString string) (bool, error) {
if len(ipString) == 0 {
return false, errors.New("empty IP address")
return false, errEmptyIPAddress
}

ipAddr := net.ParseIP(ipString)
if ipAddr == nil {
return false, fmt.Errorf("unable to parse IP address: %s", ipString)
return false, fmt.Errorf("%w: %s", errParseIPAddress, ipAddr.String())
}

for _, ip := range *a {
Expand Down
7 changes: 4 additions & 3 deletions logger.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Package ddnswhitelist
// Package ddnswhitelist implements logging methods for dnswhitelist
// source: https://github.com/traefik/plugindemo/issues/22#issuecomment-2329608616
package ddnswhitelist

Expand All @@ -8,7 +8,7 @@ import (
"strings"
)

// Logger will log messages with context
// Logger will log messages with context.
type Logger struct {
_info func(args ...interface{})
_debug func(args ...interface{})
Expand All @@ -19,7 +19,8 @@ type Logger struct {
context map[string]interface{}
}

func NewLogger(_logLevel string, middleware, middlewareType string) *Logger {
// newLogger is used to log messages with context in Traefik log format.
func newLogger(_logLevel, middleware, middlewareType string) *Logger {
logLevel := strings.ToLower(_logLevel)
// This globally sets the flags for the standard logger which is generally
// a bad practice, however, since Traefik is capturing the output of the
Expand Down

0 comments on commit 316e312

Please sign in to comment.