Skip to content

Commit

Permalink
chore: enable golang-ci errorlint (#923)
Browse files Browse the repository at this point in the history
  • Loading branch information
czeslavo authored May 30, 2023
1 parent b5d2c99 commit 240d962
Show file tree
Hide file tree
Showing 58 changed files with 197 additions and 152 deletions.
3 changes: 2 additions & 1 deletion .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ linters:
- unconvert
- unparam
- wastedassign
- errorlint
issues:
exclude-rules:
- linters:
Expand Down Expand Up @@ -80,4 +81,4 @@ linters-settings:
- sigs.k8s.io/yaml
- gopkg.in/yaml.v3:
recommendations:
- sigs.k8s.io/yaml
- sigs.k8s.io/yaml
2 changes: 1 addition & 1 deletion cmd/common_konnect.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ func GetKongClientForKonnectMode(
}
parsedAddress, err = url.Parse(address)
if err != nil {
return nil, fmt.Errorf("parsing %s address: %v", address, err)
return nil, fmt.Errorf("parsing %s address: %w", address, err)
}
_, err = authenticate(ctx, konnectClient, parsedAddress.Host, *konnectConfig)
if err == nil {
Expand Down
4 changes: 2 additions & 2 deletions cmd/convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ can be converted into a 'kong-gateway-3.x' configuration file.`,

err = convert.Convert(convertCmdInputFile, convertCmdOutputFile, sourceFormat, destinationFormat)
if err != nil {
return fmt.Errorf("converting file: %v", err)
return fmt.Errorf("converting file: %w", err)
}
} else if is2xTo3xConversion() {
path, err := os.Getwd()
Expand All @@ -62,7 +62,7 @@ can be converted into a 'kong-gateway-3.x' configuration file.`,
for _, filename := range files {
err = convert.Convert(filename, filename, sourceFormat, destinationFormat)
if err != nil {
return fmt.Errorf("converting '%s' file: %v", filename, err)
return fmt.Errorf("converting '%s' file: %w", filename, err)
}
}
}
Expand Down
3 changes: 2 additions & 1 deletion cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package cmd

import (
"context"
"errors"
"fmt"
"io/ioutil"
"net/url"
Expand Down Expand Up @@ -250,7 +251,7 @@ func initConfig() {
viper.AutomaticEnv() // read in environment variables that match
// If a config file is found, read it in.
if err := viper.ReadInConfig(); err != nil {
if _, ok := err.(viper.ConfigFileNotFoundError); !ok {
if !errors.As(err, &viper.ConfigFileNotFoundError{}) {
fmt.Println(err)
os.Exit(1)
}
Expand Down
4 changes: 2 additions & 2 deletions diff/diff.go
Original file line number Diff line number Diff line change
Expand Up @@ -339,7 +339,7 @@ func (sc *Syncer) Run(ctx context.Context, parallelism int, d Do) []error {
errs = append(errs, fmt.Errorf("failed to sync all entities: %w", ctx.Err()))
case err, ok := <-sc.errChan:
if ok && err != nil {
if err != errEnqueueFailed {
if !errors.Is(err, errEnqueueFailed) {
errs = append(errs, err)
}
}
Expand All @@ -350,7 +350,7 @@ func (sc *Syncer) Run(ctx context.Context, parallelism int, d Do) []error {

// collect errors
for err := range sc.errChan {
if err != errEnqueueFailed {
if !errors.Is(err, errEnqueueFailed) {
errs = append(errs, err)
}
}
Expand Down
4 changes: 3 additions & 1 deletion dump/dump.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package dump

import (
"context"
"errors"
"fmt"
"net/http"

Expand Down Expand Up @@ -829,7 +830,8 @@ func GetAllMTLSAuths(ctx context.Context,
// but this is currently necessary for compatibility. We need a better approach
// before adding other Enterprise resources that decK handles by default (versus,
// for example, RBAC roles, which require the --rbac-resources-only flag).
if kongErr, ok := err.(*kong.APIError); ok {
var kongErr *kong.APIError
if errors.As(err, &kongErr) {
if kongErr.Code() == http.StatusForbidden {
return mtlsAuths, nil
}
Expand Down
6 changes: 3 additions & 3 deletions dump/dump_konnect.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ func GetFromKonnect(ctx context.Context, konnectClient *konnect.Client,
// the number of parallel requests
err := semaphore.Acquire(ctx, 2)
if err != nil {
return fmt.Errorf("acquire semaphore: %v", err)
return fmt.Errorf("acquire semaphore: %w", err)
}
go func(i int) {
defer semaphore.Release(1)
Expand All @@ -81,7 +81,7 @@ func GetFromKonnect(ctx context.Context, konnectClient *konnect.Client,
for _, version := range servicePackages[i].Versions {
err := semaphore.Acquire(ctx, 1)
if err != nil {
return fmt.Errorf("acquire semaphore: %v", err)
return fmt.Errorf("acquire semaphore: %w", err)
}
go func(version konnect.ServiceVersion) {
defer semaphore.Release(1)
Expand All @@ -96,7 +96,7 @@ func GetFromKonnect(ctx context.Context, konnectClient *konnect.Client,
}
err = semaphore.Acquire(ctx, concurrency)
if err != nil {
return fmt.Errorf("acquire semaphore: %v", err)
return fmt.Errorf("acquire semaphore: %w", err)
}
close(errChan)
semaphore.Release(concurrency)
Expand Down
56 changes: 28 additions & 28 deletions file/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ func (b *stateBuilder) consumerGroups() {
cg := cg
if utils.Empty(cg.ID) {
current, err := b.currentState.ConsumerGroups.Get(*cg.Name)
if err == state.ErrNotFound {
if errors.Is(err, state.ErrNotFound) {
cg.ID = uuid()
} else if err != nil {
b.err = err
Expand All @@ -121,7 +121,7 @@ func (b *stateBuilder) consumerGroups() {
current, err := b.currentState.ConsumerGroupPlugins.Get(
*plugin.Name, *cg.ConsumerGroup.ID,
)
if err == state.ErrNotFound {
if errors.Is(err, state.ErrNotFound) {
plugin.ID = uuid()
} else if err != nil {
b.err = err
Expand All @@ -147,7 +147,7 @@ func (b *stateBuilder) certificates() {
if utils.Empty(c.ID) {
cert, err := b.currentState.Certificates.GetByCertKey(*c.Cert,
*c.Key)
if err == state.ErrNotFound {
if errors.Is(err, state.ErrNotFound) {
c.ID = uuid()
} else if err != nil {
b.err = err
Expand Down Expand Up @@ -187,7 +187,7 @@ func (b *stateBuilder) ingestSNIs(snis []kong.SNI) error {
sni := sni
if utils.Empty(sni.ID) {
currentSNI, err := b.currentState.SNIs.Get(*sni.Name)
if err == state.ErrNotFound {
if errors.Is(err, state.ErrNotFound) {
sni.ID = uuid()
} else if err != nil {
return err
Expand All @@ -210,7 +210,7 @@ func (b *stateBuilder) caCertificates() {
c := c
if utils.Empty(c.ID) {
cert, err := b.currentState.CACertificates.Get(*c.Cert)
if err == state.ErrNotFound {
if errors.Is(err, state.ErrNotFound) {
c.ID = uuid()
} else if err != nil {
b.err = err
Expand All @@ -235,7 +235,7 @@ func (b *stateBuilder) consumers() {
c := c
if utils.Empty(c.ID) {
consumer, err := b.currentState.Consumers.Get(*c.Username)
if err == state.ErrNotFound {
if errors.Is(err, state.ErrNotFound) {
if c.CustomID != nil {
consumer, err = b.currentState.Consumers.Get(*c.CustomID)
if err == nil {
Expand Down Expand Up @@ -384,7 +384,7 @@ func (b *stateBuilder) ingestKeyAuths(creds []kong.KeyAuth) error {
cred := cred
if utils.Empty(cred.ID) {
existingCred, err := b.currentState.KeyAuths.Get(*cred.Key)
if err == state.ErrNotFound {
if errors.Is(err, state.ErrNotFound) {
cred.ID = uuid()
} else if err != nil {
return err
Expand All @@ -405,7 +405,7 @@ func (b *stateBuilder) ingestBasicAuths(creds []kong.BasicAuth) error {
cred := cred
if utils.Empty(cred.ID) {
existingCred, err := b.currentState.BasicAuths.Get(*cred.Username)
if err == state.ErrNotFound {
if errors.Is(err, state.ErrNotFound) {
cred.ID = uuid()
} else if err != nil {
return err
Expand All @@ -426,7 +426,7 @@ func (b *stateBuilder) ingestHMACAuths(creds []kong.HMACAuth) error {
cred := cred
if utils.Empty(cred.ID) {
existingCred, err := b.currentState.HMACAuths.Get(*cred.Username)
if err == state.ErrNotFound {
if errors.Is(err, state.ErrNotFound) {
cred.ID = uuid()
} else if err != nil {
return err
Expand All @@ -447,7 +447,7 @@ func (b *stateBuilder) ingestJWTAuths(creds []kong.JWTAuth) error {
cred := cred
if utils.Empty(cred.ID) {
existingCred, err := b.currentState.JWTAuths.Get(*cred.Key)
if err == state.ErrNotFound {
if errors.Is(err, state.ErrNotFound) {
cred.ID = uuid()
} else if err != nil {
return err
Expand All @@ -468,7 +468,7 @@ func (b *stateBuilder) ingestOauth2Creds(creds []kong.Oauth2Credential) error {
cred := cred
if utils.Empty(cred.ID) {
existingCred, err := b.currentState.Oauth2Creds.Get(*cred.ClientID)
if err == state.ErrNotFound {
if errors.Is(err, state.ErrNotFound) {
cred.ID = uuid()
} else if err != nil {
return err
Expand All @@ -491,7 +491,7 @@ func (b *stateBuilder) ingestACLGroups(creds []kong.ACLGroup) error {
existingCred, err := b.currentState.ACLGroups.Get(
*cred.Consumer.ID,
*cred.Group)
if err == state.ErrNotFound {
if errors.Is(err, state.ErrNotFound) {
cred.ID = uuid()
} else if err != nil {
return err
Expand Down Expand Up @@ -532,7 +532,7 @@ func (b *stateBuilder) konnect() {
targetSP := b.targetContent.ServicePackages[i]
if utils.Empty(targetSP.ID) {
currentSP, err := b.currentState.ServicePackages.Get(*targetSP.Name)
if err == state.ErrNotFound {
if errors.Is(err, state.ErrNotFound) {
targetSP.ID = uuid()
} else if err != nil {
b.err = err
Expand All @@ -558,7 +558,7 @@ func (b *stateBuilder) konnect() {
}
if utils.Empty(targetKonnectDoc.ID) {
currentDoc, err := b.currentState.Documents.GetByParent(&targetKonnectSP, *targetKonnectDoc.Path)
if err == state.ErrNotFound {
if errors.Is(err, state.ErrNotFound) {
targetKonnectDoc.ID = uuid()
} else if err != nil {
b.err = err
Expand All @@ -579,7 +579,7 @@ func (b *stateBuilder) konnect() {
targetRelationID := ""
if utils.Empty(targetKonnectSV.ID) {
currentSV, err := b.currentState.ServiceVersions.Get(*targetKonnectSP.ID, *targetKonnectSV.Version)
if err == state.ErrNotFound {
if errors.Is(err, state.ErrNotFound) {
targetKonnectSV.ID = uuid()
} else if err != nil {
b.err = err
Expand Down Expand Up @@ -615,7 +615,7 @@ func (b *stateBuilder) konnect() {
}
if utils.Empty(targetKonnectDoc.ID) {
currentDoc, err := b.currentState.Documents.GetByParent(&targetKonnectSV, *targetKonnectDoc.Path)
if err == state.ErrNotFound {
if errors.Is(err, state.ErrNotFound) {
targetKonnectDoc.ID = uuid()
} else if err != nil {
b.err = err
Expand Down Expand Up @@ -652,7 +652,7 @@ func (b *stateBuilder) services() {
func (b *stateBuilder) ingestService(s *FService) error {
if utils.Empty(s.ID) {
svc, err := b.currentState.Services.Get(*s.Name)
if err == state.ErrNotFound {
if errors.Is(err, state.ErrNotFound) {
s.ID = uuid()
} else if err != nil {
return err
Expand Down Expand Up @@ -736,7 +736,7 @@ func (b *stateBuilder) vaults() {
v := v
if utils.Empty(v.ID) {
vault, err := b.currentState.Vaults.Get(*v.Prefix)
if err == state.ErrNotFound {
if errors.Is(err, state.ErrNotFound) {
v.ID = uuid()
} else if err != nil {
b.err = err
Expand All @@ -760,7 +760,7 @@ func (b *stateBuilder) rbacRoles() {
r := r
if utils.Empty(r.ID) {
role, err := b.currentState.RBACRoles.Get(*r.Name)
if err == state.ErrNotFound {
if errors.Is(err, state.ErrNotFound) {
r.ID = uuid()
} else if err != nil {
b.err = err
Expand Down Expand Up @@ -788,7 +788,7 @@ func (b *stateBuilder) upstreams() {
u := u
if utils.Empty(u.ID) {
ups, err := b.currentState.Upstreams.Get(*u.Name)
if err == state.ErrNotFound {
if errors.Is(err, state.ErrNotFound) {
u.ID = uuid()
} else if err != nil {
b.err = err
Expand Down Expand Up @@ -820,7 +820,7 @@ func (b *stateBuilder) ingestTargets(targets []kong.Target) error {
t := t
if utils.Empty(t.ID) {
target, err := b.currentState.Targets.Get(*t.Upstream.ID, *t.Target)
if err == state.ErrNotFound {
if errors.Is(err, state.ErrNotFound) {
t.ID = uuid()
} else if err != nil {
return err
Expand All @@ -845,7 +845,7 @@ func (b *stateBuilder) plugins() {
p := p
if p.Consumer != nil && !utils.Empty(p.Consumer.ID) {
c, err := b.intermediate.Consumers.Get(*p.Consumer.ID)
if err == state.ErrNotFound {
if errors.Is(err, state.ErrNotFound) {
b.err = fmt.Errorf("consumer %v for plugin %v: %w",
p.Consumer.FriendlyName(), *p.Name, err)

Expand All @@ -858,7 +858,7 @@ func (b *stateBuilder) plugins() {
}
if p.Service != nil && !utils.Empty(p.Service.ID) {
s, err := b.intermediate.Services.Get(*p.Service.ID)
if err == state.ErrNotFound {
if errors.Is(err, state.ErrNotFound) {
b.err = fmt.Errorf("service %v for plugin %v: %w",
p.Service.FriendlyName(), *p.Name, err)

Expand All @@ -871,7 +871,7 @@ func (b *stateBuilder) plugins() {
}
if p.Route != nil && !utils.Empty(p.Route.ID) {
r, err := b.intermediate.Routes.Get(*p.Route.ID)
if err == state.ErrNotFound {
if errors.Is(err, state.ErrNotFound) {
b.err = fmt.Errorf("route %v for plugin %v: %w",
p.Route.FriendlyName(), *p.Name, err)

Expand Down Expand Up @@ -911,7 +911,7 @@ func getStripPathBasedOnProtocols(route kong.Route) (*bool, error) {
func (b *stateBuilder) ingestRoute(r FRoute) error {
if utils.Empty(r.ID) {
route, err := b.currentState.Routes.Get(*r.Name)
if err == state.ErrNotFound {
if errors.Is(err, state.ErrNotFound) {
r.ID = uuid()
} else if err != nil {
return err
Expand Down Expand Up @@ -988,7 +988,7 @@ func (b *stateBuilder) addPluginDefaults(plugin *FPlugin) error {
if errors.Is(err, ErrWorkspaceNotFound) {
return nil
}
return fmt.Errorf("retrieve schema for %v from Kong: %v", *plugin.Name, err)
return fmt.Errorf("retrieve schema for %v from Kong: %w", *plugin.Name, err)
}
return kong.FillPluginsDefaults(&plugin.Plugin, schema)
}
Expand All @@ -1000,7 +1000,7 @@ func (b *stateBuilder) ingestPlugins(plugins []FPlugin) error {
cID, rID, sID := pluginRelations(&p.Plugin)
plugin, err := b.currentState.Plugins.GetByProp(*p.Name,
sID, rID, cID)
if err == state.ErrNotFound {
if errors.Is(err, state.ErrNotFound) {
p.ID = uuid()
} else if err != nil {
return err
Expand All @@ -1017,7 +1017,7 @@ func (b *stateBuilder) ingestPlugins(plugins []FPlugin) error {
return err
}
if err := b.addPluginDefaults(&p); err != nil {
return fmt.Errorf("add defaults to plugin '%v': %v", *p.Name, err)
return fmt.Errorf("add defaults to plugin '%v': %w", *p.Name, err)
}
utils.MustMergeTags(&p, b.selectTags)
b.rawState.Plugins = append(b.rawState.Plugins, &p.Plugin)
Expand Down
3 changes: 2 additions & 1 deletion file/writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package file

import (
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"os"
Expand Down Expand Up @@ -651,7 +652,7 @@ func populateConsumers(kongState *state.KongState, file *Content,
cg := *cg
_, err := kongState.ConsumerGroupConsumers.Get(*c.ID, *cg.ID)
if err != nil {
if err != state.ErrNotFound {
if !errors.Is(err, state.ErrNotFound) {
return err
}
continue
Expand Down
Loading

0 comments on commit 240d962

Please sign in to comment.