Skip to content

Commit c5f2b23

Browse files
authored
Merge pull request #220 from mmorel-35/other-revive-rules
chore: enable more rules from revive
2 parents f95b828 + 9e0f40d commit c5f2b23

File tree

12 files changed

+52
-36
lines changed

12 files changed

+52
-36
lines changed

augerctl/command/global.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ package command
1818

1919
import (
2020
"crypto/tls"
21-
"fmt"
21+
"errors"
2222
"strings"
2323

2424
"github.com/etcd-io/auger/pkg/client"
@@ -70,7 +70,7 @@ func clientConfigFromCmd(f *flagpole) (clientv3.Config, error) {
7070
if f.Password == "" {
7171
splitted := strings.SplitN(f.User, ":", 2)
7272
if len(splitted) < 2 {
73-
return clientv3.Config{}, fmt.Errorf("password is missing")
73+
return clientv3.Config{}, errors.New("password is missing")
7474
}
7575
cfg.Username = splitted[0]
7676
cfg.Password = splitted[1]

cmd/analyze.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ import (
2828
var analyzeCmd = &cobra.Command{
2929
Use: "analyze",
3030
Short: "Analyze kubernetes data from the boltdb '.db' files etcd persists to.",
31-
RunE: func(cmd *cobra.Command, args []string) error {
31+
RunE: func(_ *cobra.Command, _ []string) error {
3232
return analyzeValidateAndRun()
3333
},
3434
}

cmd/checksum.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ import (
2626
var checksumCmd = &cobra.Command{
2727
Use: "checksum",
2828
Short: "Checksum a etcd keyspace.",
29-
RunE: func(cmd *cobra.Command, args []string) error {
29+
RunE: func(_ *cobra.Command, _ []string) error {
3030
return checksum()
3131
},
3232
}

cmd/decode.go

+3-2
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ limitations under the License.
1717
package cmd
1818

1919
import (
20+
"errors"
2021
"fmt"
2122
"io"
2223
"os"
@@ -62,7 +63,7 @@ var decodeCmd = &cobra.Command{
6263
Short: "Decode objects from the kubernetes binary key-value store encoding.",
6364
Long: decodeLong,
6465
Example: decodeExample,
65-
RunE: func(cmd *cobra.Command, args []string) error {
66+
RunE: func(_ *cobra.Command, _ []string) error {
6667
return validateAndRun()
6768
},
6869
}
@@ -97,7 +98,7 @@ func validateAndRun() error {
9798

9899
in, err := readInput(options.inputFilename)
99100
if len(in) == 0 {
100-
return fmt.Errorf("no input data")
101+
return errors.New("no input data")
101102
}
102103
if err != nil {
103104
return err

cmd/encode.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ limitations under the License.
1717
package cmd
1818

1919
import (
20-
"fmt"
20+
"errors"
2121
"io"
2222
"os"
2323

@@ -42,7 +42,7 @@ var encodeCmd = &cobra.Command{
4242
Short: "Encode objects to the kubernetes binary key-value store encoding.",
4343
Long: encodeLong,
4444
Example: encodeExample,
45-
RunE: func(cmd *cobra.Command, args []string) error {
45+
RunE: func(_ *cobra.Command, _ []string) error {
4646
return encodeValidateAndRun()
4747
},
4848
}
@@ -70,7 +70,7 @@ func encodeValidateAndRun() error {
7070

7171
in, err := readInput(encodeOpts.inputFilename)
7272
if len(in) == 0 {
73-
return fmt.Errorf("no input data")
73+
return errors.New("no input data")
7474
}
7575
if err != nil {
7676
return err

cmd/extract.go

+9-8
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ limitations under the License.
1717
package cmd
1818

1919
import (
20+
"errors"
2021
"fmt"
2122
"io"
2223
"os"
@@ -70,7 +71,7 @@ var extractCmd = &cobra.Command{
7071
Short: "Extracts kubernetes data from the boltdb '.db' files etcd persists to.",
7172
Long: extractLong,
7273
Example: extractExample,
73-
RunE: func(cmd *cobra.Command, args []string) error {
74+
RunE: func(_ *cobra.Command, _ []string) error {
7475
return extractValidateAndRun()
7576
},
7677
}
@@ -160,17 +161,17 @@ func extractValidateAndRun() error {
160161
}
161162
return printLeafItemValue(kv, outMediaType, out)
162163
case hasKey && hasKeyPrefix:
163-
return fmt.Errorf("--keys-by-prefix and --key may not be used together")
164+
return errors.New("--keys-by-prefix and --key may not be used together")
164165
case hasKey && opts.listVersions:
165166
return printVersions(opts.filename, opts.key, out)
166167
case hasKey:
167168
return printValue(opts.filename, opts.key, opts.version, opts.raw, outMediaType, out)
168169
case !hasKey && opts.listVersions:
169-
return fmt.Errorf("--list-versions may only be used with --key")
170+
return errors.New("--list-versions may only be used with --key")
170171
case !hasKey && hasVersion:
171-
return fmt.Errorf("--version may only be used with --key")
172+
return errors.New("--version may only be used with --key")
172173
case hasTemplate && hasFields:
173-
return fmt.Errorf("--template and --fields may not be used together")
174+
return errors.New("--template and --fields may not be used together")
174175
case hasTemplate:
175176
return printTemplateSummaries(opts.filename, opts.keyPrefix, opts.revision, opts.template, opts.filter, out)
176177
default:
@@ -216,7 +217,7 @@ func printValue(filename string, key string, version string, raw bool, outMediaT
216217
return err
217218
}
218219
if len(in) == 0 {
219-
return fmt.Errorf("0 byte value")
220+
return errors.New("0 byte value")
220221
}
221222
if raw {
222223
fmt.Fprintf(out, "%s\n", string(in))
@@ -259,7 +260,7 @@ func printLeafItemValue(kv *mvccpb.KeyValue, outMediaType string, out io.Writer)
259260
// printKeySummaries prints all keys in the db file with the given key prefix.
260261
func printKeySummaries(filename string, keyPrefix string, revision int64, fields []string, out io.Writer) error {
261262
if len(fields) == 0 {
262-
return fmt.Errorf("no fields provided, nothing to output")
263+
return errors.New("no fields provided, nothing to output")
263264
}
264265

265266
var hasKey bool
@@ -297,7 +298,7 @@ func printTemplateSummaries(filename string, keyPrefix string, revision int64, t
297298
}
298299

299300
if len(templatestr) == 0 {
300-
return fmt.Errorf("no template provided, nothing to output")
301+
return errors.New("no template provided, nothing to output")
301302
}
302303

303304
filters := []data.Filter{}

pkg/client/client_get.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -18,19 +18,19 @@ package client
1818

1919
import (
2020
"context"
21-
"fmt"
21+
"errors"
2222

2323
clientv3 "go.etcd.io/etcd/client/v3"
2424
)
2525

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

3131
opt := opOption(opOpts)
3232
if opt.response == nil {
33-
return 0, fmt.Errorf("response is required")
33+
return 0, errors.New("response is required")
3434
}
3535

3636
path, single, err := getPrefix(prefix, opt.gr, opt.name, opt.namespace)

pkg/client/util.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ limitations under the License.
1717
package client
1818

1919
import (
20-
"fmt"
20+
"errors"
2121
"strings"
2222

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

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

8181
if gr.Empty() {
8282
if namespace != "" || name != "" {
83-
return "", false, fmt.Errorf("namespace and name must be omitted if there is no GroupResource")
83+
return "", false, errors.New("namespace and name must be omitted if there is no GroupResource")
8484
}
8585
} else {
8686
p, err := prefixFromGR(gr)

pkg/data/data.go

+9-8
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
"bytes"
2121
"encoding/binary"
2222
"encoding/json"
23+
"errors"
2324
"fmt"
2425
"hash/crc32"
2526
"os"
@@ -47,7 +48,7 @@ var (
4748
type KeySummary struct {
4849
Key string
4950
Version int64
50-
Value interface{}
51+
Value any
5152
TypeMeta *runtime.TypeMeta
5253
Stats *KeySummaryStats
5354
}
@@ -252,7 +253,7 @@ func ListKeySummaries(codecs serializer.CodecFactory, filename string, filters [
252253
valJson = strings.TrimSpace(string(buf))
253254
}
254255
var key string
255-
var value map[string]interface{}
256+
var value map[string]any
256257
if proj.HasKey {
257258
key = string(kv.Key)
258259
}
@@ -328,7 +329,7 @@ func ListVersions(filename string, key string) ([]int64, error) {
328329

329330
var result []int64
330331

331-
err = walk(db, func(r revKey, kv *mvccpb.KeyValue) (bool, error) {
332+
err = walk(db, func(_ revKey, kv *mvccpb.KeyValue) (bool, error) {
332333
if string(kv.Key) == key {
333334
result = append(result, kv.Version)
334335
}
@@ -350,7 +351,7 @@ func GetValue(filename string, key string, version int64) ([]byte, error) {
350351
defer db.Close()
351352
var result []byte
352353
found := false
353-
err = walk(db, func(r revKey, kv *mvccpb.KeyValue) (bool, error) {
354+
err = walk(db, func(_ revKey, kv *mvccpb.KeyValue) (bool, error) {
354355
if string(kv.Key) == key && kv.Version == version {
355356
result = kv.Value
356357
found = true
@@ -378,7 +379,7 @@ func walkRevision(db *bolt.DB, revision int64, f func(r revKey, kv *mvccpb.KeyVa
378379
return err
379380
}
380381
if revision > 0 && revision < compactRev {
381-
return fmt.Errorf("required revision has been compacted")
382+
return errors.New("required revision has been compacted")
382383
}
383384

384385
m := map[string]kvr{}
@@ -500,16 +501,16 @@ func ParseFilters(filters string) ([]Filter, error) {
500501
return results, nil
501502
}
502503

503-
func rawJsonMarshal(data interface{}) string {
504+
func rawJsonMarshal(data any) string {
504505
b, err := json.Marshal(data)
505506
if err != nil {
506507
return ""
507508
}
508509
return string(b)
509510
}
510511

511-
func rawJsonUnmarshal(valJson string) map[string]interface{} {
512-
val := map[string]interface{}{}
512+
func rawJsonUnmarshal(valJson string) map[string]any {
513+
val := map[string]any{}
513514
if err := json.Unmarshal([]byte(valJson), &val); err != nil {
514515
val = nil
515516
}

pkg/encoding/encoding.go

+4-3
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ package encoding
1919
import (
2020
"bytes"
2121
"encoding/json"
22+
"errors"
2223
"fmt"
2324
"io"
2425

@@ -80,7 +81,7 @@ func Convert(codecs serializer.CodecFactory, inMediaType, outMediaType string, i
8081
}
8182

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

8687
typeMeta, err := DecodeTypeMeta(inMediaType, in)
@@ -96,7 +97,7 @@ func Convert(codecs serializer.CodecFactory, inMediaType, outMediaType string, i
9697
encoded = append(encoded, '\n')
9798
}
9899
} else if inMediaType == JsonMediaType && outMediaType == YamlMediaType {
99-
val := map[string]interface{}{}
100+
val := map[string]any{}
100101
if err := json.Unmarshal(in, &val); err != nil {
101102
return nil, nil, fmt.Errorf("error decoding from %s: %s", inMediaType, err)
102103
}
@@ -140,7 +141,7 @@ func DetectAndExtract(in []byte) (string, []byte, error) {
140141
}
141142
return JsonMediaType, js, nil
142143
}
143-
return "", nil, fmt.Errorf("error reading input, does not appear to contain valid JSON or binary data")
144+
return "", nil, errors.New("error reading input, does not appear to contain valid JSON or binary data")
144145
}
145146

146147
// TryFindProto searches for the 'k8s\0' prefix, and, if found, returns the data starting with the prefix.

pkg/scheme/init.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ limitations under the License.
1717
package scheme
1818

1919
import (
20-
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
20+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2121
"k8s.io/apimachinery/pkg/runtime"
2222
"k8s.io/apimachinery/pkg/runtime/schema"
2323
"k8s.io/apimachinery/pkg/runtime/serializer"
@@ -27,6 +27,6 @@ var Scheme = runtime.NewScheme()
2727
var Codecs = serializer.NewCodecFactory(Scheme)
2828

2929
func init() {
30-
v1.AddToGroupVersion(Scheme, schema.GroupVersion{Version: "v1"})
30+
metav1.AddToGroupVersion(Scheme, schema.GroupVersion{Version: "v1"})
3131
AddToScheme(Scheme)
3232
}

tools/.golangci.yaml

+12
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ linters:
1717
# - structcheck
1818
# - varcheck
1919
- goimports
20+
- importas
2021
- ineffassign
2122
- nakedret
2223
- revive
@@ -28,6 +29,10 @@ linters:
2829
linters-settings: # please keep this alphabetized
2930
goimports:
3031
local-prefixes: go.etcd.io # Put imports beginning with prefix after 3rd-party packages.
32+
importas:
33+
alias:
34+
- alias: metav1
35+
pkg: k8s.io/apimachinery/pkg/apis/meta/v1
3136
nakedret:
3237
# Align with https://github.com/alexkohler/nakedret/blob/v1.0.2/cmd/nakedret/main.go#L10
3338
max-func-lines: 5
@@ -54,9 +59,16 @@ linters-settings: # please keep this alphabetized
5459
- name: indent-error-flow
5560
arguments:
5661
- "preserveScope"
62+
- name: receiver-naming
63+
- name: redundant-import-alias
5764
- name: superfluous-else
5865
arguments:
5966
- "preserveScope"
67+
- name: unnecessary-stmt
68+
- name: unused-parameter
69+
- name: use-any
70+
- name: use-errors-new
71+
- name: useless-break
6072
- name: var-declaration
6173
# TODO: enable the following rules
6274
- name: var-naming

0 commit comments

Comments
 (0)