refactor: fix golangci-lint findings

This commit is contained in:
Peter 2022-06-09 22:40:32 +02:00
parent f9e3c2f4f1
commit 1b005c3444
Signed by: prskr
GPG key ID: C1DB5D2E8DB512F9
15 changed files with 31 additions and 19 deletions

View file

@ -56,12 +56,7 @@ linters-settings:
line-length: 140 line-length: 140
misspell: misspell:
locale: US locale: US
nolintlint:
allow-leading-space: true # don't require machine-readable nolint directives (i.e. with no leading space)
allow-unused: false # report any unused nolint directives
require-explanation: false # don't require an explanation for nolint directives
require-specific: true
linters: linters:
disable-all: true disable-all: true
enable: enable:
@ -98,7 +93,6 @@ linters:
- nestif - nestif
- nilnil - nilnil
- noctx - noctx
- nolintlint
- nosprintfhostport - nosprintfhostport
- paralleltest - paralleltest
- prealloc - prealloc

View file

@ -30,6 +30,6 @@ func (c CheckHandler) ServeHTTP(writer http.ResponseWriter, request *http.Reques
return return
} }
writer.WriteHeader(200) writer.WriteHeader(http.StatusOK)
return return
} }

View file

@ -9,7 +9,7 @@ import (
"github.com/baez90/nurse/grammar" "github.com/baez90/nurse/grammar"
) )
var _ SystemChecker = (Collection)(nil) var _ SystemChecker = Collection(nil)
type Collection []SystemChecker type Collection []SystemChecker

View file

@ -5,7 +5,7 @@ import (
"github.com/baez90/nurse/grammar" "github.com/baez90/nurse/grammar"
) )
func CheckForScript(script []grammar.Check, lkp ModuleLookup, srvLookup config.ServerLookup) (SystemChecker, error) { func CheckForScript(script []grammar.Check, lkp ModuleLookup, srvLookup config.ServerLookup) (Collection, error) {
compiledChecks := make([]SystemChecker, 0, len(script)) compiledChecks := make([]SystemChecker, 0, len(script))
for i := range script { for i := range script {

View file

@ -27,10 +27,12 @@ func (f ModuleOptionFunc) Apply(m *Module) error {
return f(m) return f(m)
} }
//nolint:ireturn // required to implement interface
func (f FactoryFunc) New() SystemChecker { func (f FactoryFunc) New() SystemChecker {
return f() return f()
} }
//nolint:ireturn // required to implement interface
func WithCheck(name string, factory Factory) ModuleOption { func WithCheck(name string, factory Factory) ModuleOption {
return ModuleOptionFunc(func(m *Module) error { return ModuleOptionFunc(func(m *Module) error {
return m.Register(name, factory) return m.Register(name, factory)
@ -62,6 +64,7 @@ func (m *Module) Name() string {
return m.name return m.name
} }
//nolint:ireturn // required to implement interface
func (m *Module) Lookup(c grammar.Check, srvLookup config.ServerLookup) (SystemChecker, error) { func (m *Module) Lookup(c grammar.Check, srvLookup config.ServerLookup) (SystemChecker, error) {
m.lock.RLock() m.lock.RLock()
defer m.lock.RUnlock() defer m.lock.RUnlock()

View file

@ -39,6 +39,7 @@ func (r *Registry) Register(module *Module) error {
return nil return nil
} }
//nolint:ireturn // required to implement interface
func (r *Registry) Lookup(modName string) (CheckerLookup, error) { func (r *Registry) Lookup(modName string) (CheckerLookup, error) {
r.lock.RLock() r.lock.RLock()
defer r.lock.RUnlock() defer r.lock.RUnlock()

View file

@ -16,6 +16,7 @@ type configDecoder interface {
DecodeConfig(into *Nurse) error DecodeConfig(into *Nurse) error
} }
//nolint:ireturn // is required to fulfill a common function signature
func newJSONDecoder(r io.Reader) configDecoder { func newJSONDecoder(r io.Reader) configDecoder {
return &jsonDecoder{decoder: json.NewDecoder(r)} return &jsonDecoder{decoder: json.NewDecoder(r)}
} }
@ -28,6 +29,7 @@ func (j jsonDecoder) DecodeConfig(into *Nurse) error {
return j.decoder.Decode(into) return j.decoder.Decode(into)
} }
//nolint:ireturn // is required to fulfill a common function signature
func newYAMLDecoder(r io.Reader) configDecoder { func newYAMLDecoder(r io.Reader) configDecoder {
return &yamlDecoder{decoder: yaml.NewDecoder(r)} return &yamlDecoder{decoder: yaml.NewDecoder(r)}
} }

View file

@ -115,6 +115,7 @@ func TestEndpointsFromEnv(t *testing.T) {
{ {
name: "Single endpoint - sub-route", name: "Single endpoint - sub-route",
env: map[string]string{ env: map[string]string{
//nolint:lll // checks might become rather long lines
fmt.Sprintf("%s_READINESS_REDIS", config.EndpointKeyPrefix): `redis.PING("local_redis");redis.GET("local_redis", "serving") => String("ok")`, fmt.Sprintf("%s_READINESS_REDIS", config.EndpointKeyPrefix): `redis.PING("local_redis");redis.GET("local_redis", "serving") => String("ok")`,
}, },
want: td.Map(make(map[config.Route]config.EndpointSpec), td.MapEntries{ want: td.Map(make(map[config.Route]config.EndpointSpec), td.MapEntries{

View file

@ -21,6 +21,7 @@ func ConfigureFlags(cfg *Nurse) *flag.FlagSet {
return set return set
} }
//nolint:ireturn // false positive
func LookupEnvOr[T any](envKey string, fallback T, parse func(envVal string) (T, error)) T { func LookupEnvOr[T any](envKey string, fallback T, parse func(envVal string) (T, error)) T {
envVal := os.Getenv(envKey) envVal := os.Getenv(envKey)
if envVal == "" { if envVal == "" {
@ -34,6 +35,7 @@ func LookupEnvOr[T any](envKey string, fallback T, parse func(envVal string) (T,
} }
} }
//nolint:ireturn // false positive
func Identity[T any](in T) (T, error) { func Identity[T any](in T) (T, error) {
return in, nil return in, nil
} }

View file

@ -7,6 +7,7 @@ import (
"go.uber.org/zap" "go.uber.org/zap"
) )
//nolint:ireturn // required for interface implementation
func WithServersFromEnv() Option { func WithServersFromEnv() Option {
return OptionFunc(func(n Nurse) (Nurse, error) { return OptionFunc(func(n Nurse) (Nurse, error) {
envServers, err := ServersFromEnv() envServers, err := ServersFromEnv()
@ -29,6 +30,7 @@ func WithServersFromEnv() Option {
}) })
} }
//nolint:ireturn // required for interface implementation
func WithEndpointsFromEnv() Option { func WithEndpointsFromEnv() Option {
return OptionFunc(func(n Nurse) (Nurse, error) { return OptionFunc(func(n Nurse) (Nurse, error) {
envEndpoints, err := EndpointsFromEnv() envEndpoints, err := EndpointsFromEnv()
@ -51,12 +53,14 @@ func WithEndpointsFromEnv() Option {
}) })
} }
//nolint:ireturn // required for interface implementation
func WithValuesFrom(other Nurse) Option { func WithValuesFrom(other Nurse) Option {
return OptionFunc(func(n Nurse) (Nurse, error) { return OptionFunc(func(n Nurse) (Nurse, error) {
return n.Merge(other), nil return n.Merge(other), nil
}) })
} }
//nolint:ireturn // required to implement interface
func WithConfigFile(configFilePath string) Option { func WithConfigFile(configFilePath string) Option {
logger := zap.L() logger := zap.L()
return OptionFunc(func(n Nurse) (Nurse, error) { return OptionFunc(func(n Nurse) (Nurse, error) {

View file

@ -104,7 +104,7 @@ func (s *Server) UnmarshalURL(rawUrl string) error {
if err != nil { if err != nil {
return err return err
} }
if err = s.unmarshalPath(parsedUrl); err != nil { if err := s.unmarshalPath(parsedUrl); err != nil {
return err return err
} }
} else { } else {

View file

@ -35,7 +35,6 @@ func main() {
config.WithServersFromEnv(), config.WithServersFromEnv(),
config.WithEndpointsFromEnv(), config.WithEndpointsFromEnv(),
) )
if err != nil { if err != nil {
logger.Fatal("Failed to load config from environment", zap.Error(err)) logger.Fatal("Failed to load config from environment", zap.Error(err))
} }

View file

@ -17,12 +17,16 @@ type JSONPathValidator struct {
validator *validation.JSONPathValidator validator *validation.JSONPathValidator
} }
func (j *JSONPathValidator) UnmarshalCall(c grammar.Call) (err error) { func (j *JSONPathValidator) UnmarshalCall(c grammar.Call) error {
if err = grammar.ValidateParameterCount(c.Params, 2); err != nil { const pathAndWantArgsCount = 2
if err := grammar.ValidateParameterCount(c.Params, pathAndWantArgsCount); err != nil {
return err return err
} }
var jsonPath string var (
jsonPath string
err error
)
if jsonPath, err = c.Params[0].AsString(); err != nil { if jsonPath, err = c.Params[0].AsString(); err != nil {
return err return err
@ -41,7 +45,7 @@ func (j *JSONPathValidator) UnmarshalCall(c grammar.Call) (err error) {
return errors.New("param type unknown") return errors.New("param type unknown")
} }
return nil return err
} }
func (j *JSONPathValidator) Validate(resp *http.Response) error { func (j *JSONPathValidator) Validate(resp *http.Response) error {

View file

@ -10,6 +10,7 @@ import (
"github.com/baez90/nurse/grammar" "github.com/baez90/nurse/grammar"
) )
//nolint:ireturn // no other choice
func clientFromParam(p grammar.Param, srvLookup config.ServerLookup) (redis.UniversalClient, error) { func clientFromParam(p grammar.Param, srvLookup config.ServerLookup) (redis.UniversalClient, error) {
if srvName, err := p.AsString(); err != nil { if srvName, err := p.AsString(); err != nil {
return nil, err return nil, err
@ -22,6 +23,7 @@ func clientFromParam(p grammar.Param, srvLookup config.ServerLookup) (redis.Univ
} }
} }
//nolint:ireturn // no other choice
func ClientForServer(srv *config.Server) (redis.UniversalClient, error) { func ClientForServer(srv *config.Server) (redis.UniversalClient, error) {
opts := &redis.UniversalOptions{ opts := &redis.UniversalOptions{
Addrs: srv.Hosts, Addrs: srv.Hosts,

View file

@ -58,6 +58,8 @@ func (g *GenericCmdValidator) UnmarshalCall(c grammar.Call) error {
if g.comparator, err = validation.JSONValueComparatorFor(*c.Params[0].String); err != nil { if g.comparator, err = validation.JSONValueComparatorFor(*c.Params[0].String); err != nil {
return err return err
} }
case grammar.ParamTypeUnknown:
fallthrough
default: default:
return errors.New("param type is unknown") return errors.New("param type is unknown")
} }
@ -70,13 +72,11 @@ func (g *GenericCmdValidator) Validate(cmder redis.Cmder) error {
return err return err
} }
switch in := cmder.(type) { if in, ok := cmder.(*redis.StringCmd); ok {
case *redis.StringCmd:
res, err := in.Result() res, err := in.Result()
if err != nil { if err != nil {
return err return err
} }
return g.comparator.Equals(res) return g.comparator.Equals(res)
} }