refactor: fix golangci-lint findings
This commit is contained in:
parent
f9e3c2f4f1
commit
1b005c3444
15 changed files with 31 additions and 19 deletions
|
@ -56,11 +56,6 @@ 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
|
||||||
|
@ -98,7 +93,6 @@ linters:
|
||||||
- nestif
|
- nestif
|
||||||
- nilnil
|
- nilnil
|
||||||
- noctx
|
- noctx
|
||||||
- nolintlint
|
|
||||||
- nosprintfhostport
|
- nosprintfhostport
|
||||||
- paralleltest
|
- paralleltest
|
||||||
- prealloc
|
- prealloc
|
||||||
|
|
|
@ -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
|
||||||
}
|
}
|
||||||
|
|
|
@ -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
|
||||||
|
|
||||||
|
|
|
@ -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 {
|
||||||
|
|
|
@ -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()
|
||||||
|
|
|
@ -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()
|
||||||
|
|
|
@ -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)}
|
||||||
}
|
}
|
||||||
|
|
|
@ -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{
|
||||||
|
|
|
@ -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
|
||||||
}
|
}
|
||||||
|
|
|
@ -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) {
|
||||||
|
|
|
@ -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 {
|
||||||
|
|
1
main.go
1
main.go
|
@ -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))
|
||||||
}
|
}
|
||||||
|
|
|
@ -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 {
|
||||||
|
|
|
@ -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,
|
||||||
|
|
|
@ -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)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue