Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: enable more rules from revive #220

Merged
merged 1 commit into from
Mar 4, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions augerctl/command/global.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ package command

import (
"crypto/tls"
"fmt"
"errors"
"strings"

"github.com/etcd-io/auger/pkg/client"
Expand Down Expand Up @@ -70,7 +70,7 @@ func clientConfigFromCmd(f *flagpole) (clientv3.Config, error) {
if f.Password == "" {
splitted := strings.SplitN(f.User, ":", 2)
if len(splitted) < 2 {
return clientv3.Config{}, fmt.Errorf("password is missing")
return clientv3.Config{}, errors.New("password is missing")
}
cfg.Username = splitted[0]
cfg.Password = splitted[1]
Expand Down
2 changes: 1 addition & 1 deletion cmd/analyze.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import (
var analyzeCmd = &cobra.Command{
Use: "analyze",
Short: "Analyze kubernetes data from the boltdb '.db' files etcd persists to.",
RunE: func(cmd *cobra.Command, args []string) error {
RunE: func(_ *cobra.Command, _ []string) error {
return analyzeValidateAndRun()
},
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/checksum.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import (
var checksumCmd = &cobra.Command{
Use: "checksum",
Short: "Checksum a etcd keyspace.",
RunE: func(cmd *cobra.Command, args []string) error {
RunE: func(_ *cobra.Command, _ []string) error {
return checksum()
},
}
Expand Down
5 changes: 3 additions & 2 deletions cmd/decode.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ limitations under the License.
package cmd

import (
"errors"
"fmt"
"io"
"os"
Expand Down Expand Up @@ -62,7 +63,7 @@ var decodeCmd = &cobra.Command{
Short: "Decode objects from the kubernetes binary key-value store encoding.",
Long: decodeLong,
Example: decodeExample,
RunE: func(cmd *cobra.Command, args []string) error {
RunE: func(_ *cobra.Command, _ []string) error {
return validateAndRun()
},
}
Expand Down Expand Up @@ -97,7 +98,7 @@ func validateAndRun() error {

in, err := readInput(options.inputFilename)
if len(in) == 0 {
return fmt.Errorf("no input data")
return errors.New("no input data")
}
if err != nil {
return err
Expand Down
6 changes: 3 additions & 3 deletions cmd/encode.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ limitations under the License.
package cmd

import (
"fmt"
"errors"
"io"
"os"

Expand All @@ -42,7 +42,7 @@ var encodeCmd = &cobra.Command{
Short: "Encode objects to the kubernetes binary key-value store encoding.",
Long: encodeLong,
Example: encodeExample,
RunE: func(cmd *cobra.Command, args []string) error {
RunE: func(_ *cobra.Command, _ []string) error {
return encodeValidateAndRun()
},
}
Expand Down Expand Up @@ -70,7 +70,7 @@ func encodeValidateAndRun() error {

in, err := readInput(encodeOpts.inputFilename)
if len(in) == 0 {
return fmt.Errorf("no input data")
return errors.New("no input data")
}
if err != nil {
return err
Expand Down
17 changes: 9 additions & 8 deletions cmd/extract.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ limitations under the License.
package cmd

import (
"errors"
"fmt"
"io"
"os"
Expand Down Expand Up @@ -70,7 +71,7 @@ var extractCmd = &cobra.Command{
Short: "Extracts kubernetes data from the boltdb '.db' files etcd persists to.",
Long: extractLong,
Example: extractExample,
RunE: func(cmd *cobra.Command, args []string) error {
RunE: func(_ *cobra.Command, _ []string) error {
return extractValidateAndRun()
},
}
Expand Down Expand Up @@ -160,17 +161,17 @@ func extractValidateAndRun() error {
}
return printLeafItemValue(kv, outMediaType, out)
case hasKey && hasKeyPrefix:
return fmt.Errorf("--keys-by-prefix and --key may not be used together")
return errors.New("--keys-by-prefix and --key may not be used together")
case hasKey && opts.listVersions:
return printVersions(opts.filename, opts.key, out)
case hasKey:
return printValue(opts.filename, opts.key, opts.version, opts.raw, outMediaType, out)
case !hasKey && opts.listVersions:
return fmt.Errorf("--list-versions may only be used with --key")
return errors.New("--list-versions may only be used with --key")
case !hasKey && hasVersion:
return fmt.Errorf("--version may only be used with --key")
return errors.New("--version may only be used with --key")
case hasTemplate && hasFields:
return fmt.Errorf("--template and --fields may not be used together")
return errors.New("--template and --fields may not be used together")
case hasTemplate:
return printTemplateSummaries(opts.filename, opts.keyPrefix, opts.revision, opts.template, opts.filter, out)
default:
Expand Down Expand Up @@ -216,7 +217,7 @@ func printValue(filename string, key string, version string, raw bool, outMediaT
return err
}
if len(in) == 0 {
return fmt.Errorf("0 byte value")
return errors.New("0 byte value")
}
if raw {
fmt.Fprintf(out, "%s\n", string(in))
Expand Down Expand Up @@ -259,7 +260,7 @@ func printLeafItemValue(kv *mvccpb.KeyValue, outMediaType string, out io.Writer)
// printKeySummaries prints all keys in the db file with the given key prefix.
func printKeySummaries(filename string, keyPrefix string, revision int64, fields []string, out io.Writer) error {
if len(fields) == 0 {
return fmt.Errorf("no fields provided, nothing to output")
return errors.New("no fields provided, nothing to output")
}

var hasKey bool
Expand Down Expand Up @@ -297,7 +298,7 @@ func printTemplateSummaries(filename string, keyPrefix string, revision int64, t
}

if len(templatestr) == 0 {
return fmt.Errorf("no template provided, nothing to output")
return errors.New("no template provided, nothing to output")
}

filters := []data.Filter{}
Expand Down
6 changes: 3 additions & 3 deletions pkg/client/client_get.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,19 @@ package client

import (
"context"
"fmt"
"errors"

clientv3 "go.etcd.io/etcd/client/v3"
)

func (c *client) Get(ctx context.Context, prefix string, opOpts ...OpOption) (rev int64, err error) {
if prefix == "" {
return 0, fmt.Errorf("prefix is required")
return 0, errors.New("prefix is required")
}

opt := opOption(opOpts)
if opt.response == nil {
return 0, fmt.Errorf("response is required")
return 0, errors.New("response is required")
}

path, single, err := getPrefix(prefix, opt.gr, opt.name, opt.namespace)
Expand Down
6 changes: 3 additions & 3 deletions pkg/client/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ limitations under the License.
package client

import (
"fmt"
"errors"
"strings"

"k8s.io/apimachinery/pkg/runtime/schema"
Expand All @@ -42,7 +42,7 @@ var specialDefaultMediaTypes = map[string]struct{}{
// prefixFromGR returns the prefix of the given GroupResource.
func prefixFromGR(gr schema.GroupResource) (string, error) {
if gr.Resource == "" {
return "", fmt.Errorf("resource is empty")
return "", errors.New("resource is empty")
}

if prefix, ok := specialDefaultResourcePrefixes[gr]; ok {
Expand Down Expand Up @@ -80,7 +80,7 @@ func getPrefix(prefix string, gr schema.GroupResource, name, namespace string) (

if gr.Empty() {
if namespace != "" || name != "" {
return "", false, fmt.Errorf("namespace and name must be omitted if there is no GroupResource")
return "", false, errors.New("namespace and name must be omitted if there is no GroupResource")
}
} else {
p, err := prefixFromGR(gr)
Expand Down
17 changes: 9 additions & 8 deletions pkg/data/data.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"bytes"
"encoding/binary"
"encoding/json"
"errors"
"fmt"
"hash/crc32"
"os"
Expand Down Expand Up @@ -47,7 +48,7 @@ var (
type KeySummary struct {
Key string
Version int64
Value interface{}
Value any
TypeMeta *runtime.TypeMeta
Stats *KeySummaryStats
}
Expand Down Expand Up @@ -252,7 +253,7 @@ func ListKeySummaries(codecs serializer.CodecFactory, filename string, filters [
valJson = strings.TrimSpace(string(buf))
}
var key string
var value map[string]interface{}
var value map[string]any
if proj.HasKey {
key = string(kv.Key)
}
Expand Down Expand Up @@ -328,7 +329,7 @@ func ListVersions(filename string, key string) ([]int64, error) {

var result []int64

err = walk(db, func(r revKey, kv *mvccpb.KeyValue) (bool, error) {
err = walk(db, func(_ revKey, kv *mvccpb.KeyValue) (bool, error) {
if string(kv.Key) == key {
result = append(result, kv.Version)
}
Expand All @@ -350,7 +351,7 @@ func GetValue(filename string, key string, version int64) ([]byte, error) {
defer db.Close()
var result []byte
found := false
err = walk(db, func(r revKey, kv *mvccpb.KeyValue) (bool, error) {
err = walk(db, func(_ revKey, kv *mvccpb.KeyValue) (bool, error) {
if string(kv.Key) == key && kv.Version == version {
result = kv.Value
found = true
Expand Down Expand Up @@ -378,7 +379,7 @@ func walkRevision(db *bolt.DB, revision int64, f func(r revKey, kv *mvccpb.KeyVa
return err
}
if revision > 0 && revision < compactRev {
return fmt.Errorf("required revision has been compacted")
return errors.New("required revision has been compacted")
}

m := map[string]kvr{}
Expand Down Expand Up @@ -500,16 +501,16 @@ func ParseFilters(filters string) ([]Filter, error) {
return results, nil
}

func rawJsonMarshal(data interface{}) string {
func rawJsonMarshal(data any) string {
b, err := json.Marshal(data)
if err != nil {
return ""
}
return string(b)
}

func rawJsonUnmarshal(valJson string) map[string]interface{} {
val := map[string]interface{}{}
func rawJsonUnmarshal(valJson string) map[string]any {
val := map[string]any{}
if err := json.Unmarshal([]byte(valJson), &val); err != nil {
val = nil
}
Expand Down
7 changes: 4 additions & 3 deletions pkg/encoding/encoding.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package encoding
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"io"

Expand Down Expand Up @@ -80,7 +81,7 @@ func Convert(codecs serializer.CodecFactory, inMediaType, outMediaType string, i
}

if inMediaType == ProtobufMediaType && outMediaType == StorageBinaryMediaType {
return nil, nil, fmt.Errorf("unsupported conversion: protobuf to kubernetes binary storage representation")
return nil, nil, errors.New("unsupported conversion: protobuf to kubernetes binary storage representation")
}

typeMeta, err := DecodeTypeMeta(inMediaType, in)
Expand All @@ -96,7 +97,7 @@ func Convert(codecs serializer.CodecFactory, inMediaType, outMediaType string, i
encoded = append(encoded, '\n')
}
} else if inMediaType == JsonMediaType && outMediaType == YamlMediaType {
val := map[string]interface{}{}
val := map[string]any{}
if err := json.Unmarshal(in, &val); err != nil {
return nil, nil, fmt.Errorf("error decoding from %s: %s", inMediaType, err)
}
Expand Down Expand Up @@ -140,7 +141,7 @@ func DetectAndExtract(in []byte) (string, []byte, error) {
}
return JsonMediaType, js, nil
}
return "", nil, fmt.Errorf("error reading input, does not appear to contain valid JSON or binary data")
return "", nil, errors.New("error reading input, does not appear to contain valid JSON or binary data")
}

// TryFindProto searches for the 'k8s\0' prefix, and, if found, returns the data starting with the prefix.
Expand Down
4 changes: 2 additions & 2 deletions pkg/scheme/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ limitations under the License.
package scheme

import (
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apimachinery/pkg/runtime/serializer"
Expand All @@ -27,6 +27,6 @@ var Scheme = runtime.NewScheme()
var Codecs = serializer.NewCodecFactory(Scheme)

func init() {
v1.AddToGroupVersion(Scheme, schema.GroupVersion{Version: "v1"})
metav1.AddToGroupVersion(Scheme, schema.GroupVersion{Version: "v1"})
AddToScheme(Scheme)
}
12 changes: 12 additions & 0 deletions tools/.golangci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ linters:
# - structcheck
# - varcheck
- goimports
- importas
- ineffassign
- nakedret
- revive
Expand All @@ -28,6 +29,10 @@ linters:
linters-settings: # please keep this alphabetized
goimports:
local-prefixes: go.etcd.io # Put imports beginning with prefix after 3rd-party packages.
importas:
alias:
- alias: metav1
pkg: k8s.io/apimachinery/pkg/apis/meta/v1
nakedret:
# Align with https://github.com/alexkohler/nakedret/blob/v1.0.2/cmd/nakedret/main.go#L10
max-func-lines: 5
Expand All @@ -54,9 +59,16 @@ linters-settings: # please keep this alphabetized
- name: indent-error-flow
arguments:
- "preserveScope"
- name: receiver-naming
- name: redundant-import-alias
- name: superfluous-else
arguments:
- "preserveScope"
- name: unnecessary-stmt
- name: unused-parameter
- name: use-any
- name: use-errors-new
- name: useless-break
- name: var-declaration
# TODO: enable the following rules
- name: var-naming
Expand Down