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
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

View file

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

View file

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

View file

@ -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 {

View file

@ -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()

View file

@ -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()

View file

@ -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)}
}

View file

@ -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{

View file

@ -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
}

View file

@ -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) {

View file

@ -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 {

View file

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

View file

@ -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 {

View file

@ -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,

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 {
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)
}