From 1b005c3444148acf508669b75765664bef9dc620 Mon Sep 17 00:00:00 2001 From: Peter Kurfer Date: Thu, 9 Jun 2022 22:40:32 +0200 Subject: [PATCH] refactor: fix golangci-lint findings --- .golangci.yml | 8 +------- api/check_handler.go | 2 +- check/collection.go | 2 +- check/endpoint.go | 2 +- check/modules.go | 3 +++ check/registry.go | 1 + config/decoder.go | 2 ++ config/env_test.go | 1 + config/flags.go | 2 ++ config/options.go | 4 ++++ config/server.go | 2 +- main.go | 1 - protocols/http/json_validation.go | 12 ++++++++---- protocols/redis/client.go | 2 ++ protocols/redis/validation.go | 6 +++--- 15 files changed, 31 insertions(+), 19 deletions(-) diff --git a/.golangci.yml b/.golangci.yml index b130c49..005d40f 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -56,12 +56,7 @@ linters-settings: line-length: 140 misspell: 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: disable-all: true enable: @@ -98,7 +93,6 @@ linters: - nestif - nilnil - noctx - - nolintlint - nosprintfhostport - paralleltest - prealloc diff --git a/api/check_handler.go b/api/check_handler.go index cb3bdc8..46a2ae6 100644 --- a/api/check_handler.go +++ b/api/check_handler.go @@ -30,6 +30,6 @@ func (c CheckHandler) ServeHTTP(writer http.ResponseWriter, request *http.Reques return } - writer.WriteHeader(200) + writer.WriteHeader(http.StatusOK) return } diff --git a/check/collection.go b/check/collection.go index f5ab019..bef0328 100644 --- a/check/collection.go +++ b/check/collection.go @@ -9,7 +9,7 @@ import ( "github.com/baez90/nurse/grammar" ) -var _ SystemChecker = (Collection)(nil) +var _ SystemChecker = Collection(nil) type Collection []SystemChecker diff --git a/check/endpoint.go b/check/endpoint.go index eab63c0..10fc46a 100644 --- a/check/endpoint.go +++ b/check/endpoint.go @@ -5,7 +5,7 @@ import ( "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)) for i := range script { diff --git a/check/modules.go b/check/modules.go index 026c0ec..f520d0c 100644 --- a/check/modules.go +++ b/check/modules.go @@ -27,10 +27,12 @@ func (f ModuleOptionFunc) Apply(m *Module) error { return f(m) } +//nolint:ireturn // required to implement interface func (f FactoryFunc) New() SystemChecker { return f() } +//nolint:ireturn // required to implement interface func WithCheck(name string, factory Factory) ModuleOption { return ModuleOptionFunc(func(m *Module) error { return m.Register(name, factory) @@ -62,6 +64,7 @@ func (m *Module) Name() string { return m.name } +//nolint:ireturn // required to implement interface func (m *Module) Lookup(c grammar.Check, srvLookup config.ServerLookup) (SystemChecker, error) { m.lock.RLock() defer m.lock.RUnlock() diff --git a/check/registry.go b/check/registry.go index 6174ed7..b6bef82 100644 --- a/check/registry.go +++ b/check/registry.go @@ -39,6 +39,7 @@ func (r *Registry) Register(module *Module) error { return nil } +//nolint:ireturn // required to implement interface func (r *Registry) Lookup(modName string) (CheckerLookup, error) { r.lock.RLock() defer r.lock.RUnlock() diff --git a/config/decoder.go b/config/decoder.go index 3f29226..ac863bf 100644 --- a/config/decoder.go +++ b/config/decoder.go @@ -16,6 +16,7 @@ type configDecoder interface { DecodeConfig(into *Nurse) error } +//nolint:ireturn // is required to fulfill a common function signature func newJSONDecoder(r io.Reader) configDecoder { return &jsonDecoder{decoder: json.NewDecoder(r)} } @@ -28,6 +29,7 @@ func (j jsonDecoder) DecodeConfig(into *Nurse) error { return j.decoder.Decode(into) } +//nolint:ireturn // is required to fulfill a common function signature func newYAMLDecoder(r io.Reader) configDecoder { return &yamlDecoder{decoder: yaml.NewDecoder(r)} } diff --git a/config/env_test.go b/config/env_test.go index 1deb3d4..5c86a42 100644 --- a/config/env_test.go +++ b/config/env_test.go @@ -115,6 +115,7 @@ func TestEndpointsFromEnv(t *testing.T) { { name: "Single endpoint - sub-route", 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")`, }, want: td.Map(make(map[config.Route]config.EndpointSpec), td.MapEntries{ diff --git a/config/flags.go b/config/flags.go index e693d60..e563a11 100644 --- a/config/flags.go +++ b/config/flags.go @@ -21,6 +21,7 @@ func ConfigureFlags(cfg *Nurse) *flag.FlagSet { return set } +//nolint:ireturn // false positive func LookupEnvOr[T any](envKey string, fallback T, parse func(envVal string) (T, error)) T { envVal := os.Getenv(envKey) 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) { return in, nil } diff --git a/config/options.go b/config/options.go index 4e86af3..2310799 100644 --- a/config/options.go +++ b/config/options.go @@ -7,6 +7,7 @@ import ( "go.uber.org/zap" ) +//nolint:ireturn // required for interface implementation func WithServersFromEnv() Option { return OptionFunc(func(n Nurse) (Nurse, error) { envServers, err := ServersFromEnv() @@ -29,6 +30,7 @@ func WithServersFromEnv() Option { }) } +//nolint:ireturn // required for interface implementation func WithEndpointsFromEnv() Option { return OptionFunc(func(n Nurse) (Nurse, error) { envEndpoints, err := EndpointsFromEnv() @@ -51,12 +53,14 @@ func WithEndpointsFromEnv() Option { }) } +//nolint:ireturn // required for interface implementation func WithValuesFrom(other Nurse) Option { return OptionFunc(func(n Nurse) (Nurse, error) { return n.Merge(other), nil }) } +//nolint:ireturn // required to implement interface func WithConfigFile(configFilePath string) Option { logger := zap.L() return OptionFunc(func(n Nurse) (Nurse, error) { diff --git a/config/server.go b/config/server.go index 7051299..35772c6 100644 --- a/config/server.go +++ b/config/server.go @@ -104,7 +104,7 @@ func (s *Server) UnmarshalURL(rawUrl string) error { if err != nil { return err } - if err = s.unmarshalPath(parsedUrl); err != nil { + if err := s.unmarshalPath(parsedUrl); err != nil { return err } } else { diff --git a/main.go b/main.go index 839bfb0..3d586f3 100644 --- a/main.go +++ b/main.go @@ -35,7 +35,6 @@ func main() { config.WithServersFromEnv(), config.WithEndpointsFromEnv(), ) - if err != nil { logger.Fatal("Failed to load config from environment", zap.Error(err)) } diff --git a/protocols/http/json_validation.go b/protocols/http/json_validation.go index 044b609..ebe0dcb 100644 --- a/protocols/http/json_validation.go +++ b/protocols/http/json_validation.go @@ -17,12 +17,16 @@ type JSONPathValidator struct { validator *validation.JSONPathValidator } -func (j *JSONPathValidator) UnmarshalCall(c grammar.Call) (err error) { - if err = grammar.ValidateParameterCount(c.Params, 2); err != nil { +func (j *JSONPathValidator) UnmarshalCall(c grammar.Call) error { + const pathAndWantArgsCount = 2 + if err := grammar.ValidateParameterCount(c.Params, pathAndWantArgsCount); err != nil { return err } - var jsonPath string + var ( + jsonPath string + err error + ) if jsonPath, err = c.Params[0].AsString(); err != nil { return err @@ -41,7 +45,7 @@ func (j *JSONPathValidator) UnmarshalCall(c grammar.Call) (err error) { return errors.New("param type unknown") } - return nil + return err } func (j *JSONPathValidator) Validate(resp *http.Response) error { diff --git a/protocols/redis/client.go b/protocols/redis/client.go index 76690ef..5321227 100644 --- a/protocols/redis/client.go +++ b/protocols/redis/client.go @@ -10,6 +10,7 @@ import ( "github.com/baez90/nurse/grammar" ) +//nolint:ireturn // no other choice func clientFromParam(p grammar.Param, srvLookup config.ServerLookup) (redis.UniversalClient, error) { if srvName, err := p.AsString(); err != nil { 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) { opts := &redis.UniversalOptions{ Addrs: srv.Hosts, diff --git a/protocols/redis/validation.go b/protocols/redis/validation.go index edb7e34..e4f2763 100644 --- a/protocols/redis/validation.go +++ b/protocols/redis/validation.go @@ -58,6 +58,8 @@ func (g *GenericCmdValidator) UnmarshalCall(c grammar.Call) error { if g.comparator, err = validation.JSONValueComparatorFor(*c.Params[0].String); err != nil { return err } + case grammar.ParamTypeUnknown: + fallthrough default: return errors.New("param type is unknown") } @@ -70,13 +72,11 @@ func (g *GenericCmdValidator) Validate(cmder redis.Cmder) error { return err } - switch in := cmder.(type) { - case *redis.StringCmd: + if in, ok := cmder.(*redis.StringCmd); ok { res, err := in.Result() if err != nil { return err } - return g.comparator.Equals(res) }