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: align golangci-lint config with etcd #221

Merged
merged 1 commit into from
Mar 5, 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
5 changes: 1 addition & 4 deletions augerctl/command/get_command.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,7 @@ type getFlagpole struct {
Prefix string
}

var (
getExample = `
var getExample = `
# List a single service with namespace "default" and name "kubernetes"
augerctl get services -n default kubernetes
# Nearly equivalent
Expand All @@ -60,7 +59,6 @@ var (
# Nearly equivalent
kubectl get $(kubectl api-resources --verbs=list --output=name | paste -s -d, - ) -A -o yaml
`
)

func newCtlGetCommand(f *flagpole) *cobra.Command {
flags := &getFlagpole{}
Expand All @@ -76,7 +74,6 @@ func newCtlGetCommand(f *flagpole) *cobra.Command {
return err
}
err = getCommand(cmd.Context(), etcdclient, flags, args)

if err != nil {
return fmt.Errorf("%v: %w", args, err)
}
Expand Down
20 changes: 9 additions & 11 deletions cmd/decode.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,14 @@ limitations under the License.
package cmd

import (
"bufio"
"bytes"
"encoding/hex"
"errors"
"fmt"
"io"
"os"

"bufio"
"bytes"
"encoding/hex"

"github.com/etcd-io/auger/pkg/encoding"
"github.com/etcd-io/auger/pkg/scheme"
"github.com/spf13/cobra"
Expand Down Expand Up @@ -122,17 +121,17 @@ func runInBatchMode(metaOnly bool, outMediaType string, out io.Writer) (err erro
lineNum := 0
for {
input, err := inputReader.ReadBytes(byte('\n'))
if err == io.EOF {
if errors.Is(err, io.EOF) {
return nil
}
if err != nil {
return fmt.Errorf("error reading --batch-process input: %v", err)
return fmt.Errorf("error reading --batch-process input: %w", err)
}

input = stripNewline(input)
decodedinput, err := hex.DecodeString(string(input))
if err != nil {
return fmt.Errorf("error decoding input on line %d of --batch-process input: %v", lineNum, err)
return fmt.Errorf("error decoding input on line %d of --batch-process input: %w", lineNum, err)
}
inMediaType, decodedinput, err := encoding.DetectAndExtract(decodedinput)
if err != nil {
Expand All @@ -144,7 +143,6 @@ func runInBatchMode(metaOnly bool, outMediaType string, out io.Writer) (err erro
buf := bytes.NewBufferString("")
err = encoding.DecodeSummary(inMediaType, decodedinput, buf)
if err != nil {

fmt.Fprintf(out, "ERROR:%v|\n", err)
} else {
fmt.Fprintf(out, "OK|%s\n", buf.String())
Expand Down Expand Up @@ -188,23 +186,23 @@ func readInput(inputFilename string) ([]byte, error) {
if inputFilename != "" {
data, err := os.ReadFile(inputFilename)
if err != nil {
return nil, fmt.Errorf("error reading input file %s: %v", inputFilename, err)
return nil, fmt.Errorf("error reading input file %s: %w", inputFilename, err)
}
data = stripNewline(data)
return data, nil
}

stat, err := os.Stdin.Stat()
if err != nil {
return nil, fmt.Errorf("stdin error: %s", err)
return nil, fmt.Errorf("stdin error: %w", err)
}
if (stat.Mode() & os.ModeCharDevice) != 0 {
fmt.Fprintln(os.Stderr, "warn: waiting on stdin from tty")
}

stdin, err := io.ReadAll(os.Stdin)
if err != nil {
return nil, fmt.Errorf("unable to read data from stdin: %v", err)
return nil, fmt.Errorf("unable to read data from stdin: %w", err)
}
// Etcd --print-value-only includes an extranous newline even for binary values.
// We can safely strip it off since none of the valid inputs this code processes
Expand Down
8 changes: 4 additions & 4 deletions cmd/extract.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ func templateFields() string {
func extractValidateAndRun() error {
outMediaType, err := encoding.ToMediaType(opts.out)
if err != nil {
return fmt.Errorf("invalid --output %s: %s", opts.out, err)
return fmt.Errorf("invalid --output %s: %w", opts.out, err)
}
out := os.Stdout
hasKey := opts.key != ""
Expand All @@ -148,11 +148,11 @@ func extractValidateAndRun() error {
case opts.leafItem:
raw, err := readInput(opts.filename)
if err != nil {
return fmt.Errorf("unable to read input: %s", err)
return fmt.Errorf("unable to read input: %w", err)
}
kv, err := extractKvFromLeafItem(raw)
if err != nil {
return fmt.Errorf("failed to extract etcd key-value record from boltdb leaf item: %s", err)
return fmt.Errorf("failed to extract etcd key-value record from boltdb leaf item: %w", err)
}
if opts.metaSummary {
return printLeafItemSummary(kv, out)
Expand Down Expand Up @@ -209,7 +209,7 @@ func printValue(filename string, key string, version string, raw bool, outMediaT
} else {
v, err = strconv.ParseInt(version, 10, 64)
if err != nil {
return fmt.Errorf("version must be an int64, but got %s: %s", version, err)
return fmt.Errorf("version must be an int64, but got %s: %w", version, err)
}
}
in, err := data.GetValue(filename, key, v)
Expand Down
5 changes: 2 additions & 3 deletions pkg/data/data.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ func (ff *FieldFilter) Accept(ks *KeySummary) (bool, error) {
buf := new(bytes.Buffer)
err := ff.lhsTemplate.Execute(buf, ks)
if err != nil {
return false, fmt.Errorf("failed to look up field in filter %s: %v", ff.lhs, err)
return false, fmt.Errorf("failed to look up field in filter %s: %w", ff.lhs, err)
}
val := buf.String()
switch ff.op {
Expand All @@ -162,7 +162,7 @@ func boltOpen(path string) (*bolt.DB, error) {
}

// Open the file in read-only mode
return bolt.Open(path, 0400, &bolt.Options{
return bolt.Open(path, 0o400, &bolt.Options{
ReadOnly: true,
})
}
Expand Down Expand Up @@ -294,7 +294,6 @@ func ListKeySummaries(codecs serializer.CodecFactory, filename string, filters [
ks.Stats.AllVersionsKeySize += len(kv.Key)
ks.Stats.AllVersionsValueSize += len(kv.Value)
}

}
return false, nil
})
Expand Down
10 changes: 5 additions & 5 deletions pkg/encoding/encoding.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,11 +99,11 @@ func Convert(codecs serializer.CodecFactory, inMediaType, outMediaType string, i
} else if inMediaType == JsonMediaType && outMediaType == YamlMediaType {
val := map[string]any{}
if err := json.Unmarshal(in, &val); err != nil {
return nil, nil, fmt.Errorf("error decoding from %s: %s", inMediaType, err)
return nil, nil, fmt.Errorf("error decoding from %s: %w", inMediaType, err)
}
encoded, err = yaml.Marshal(val)
if err != nil {
return nil, nil, fmt.Errorf("error encoding from %s: %s", outMediaType, err)
return nil, nil, fmt.Errorf("error encoding from %s: %w", outMediaType, err)
}
} else {
inCodec, err := newCodec(codecs, typeMeta, inMediaType)
Expand All @@ -117,12 +117,12 @@ func Convert(codecs serializer.CodecFactory, inMediaType, outMediaType string, i

obj, err := runtime.Decode(inCodec, in)
if err != nil {
return nil, nil, fmt.Errorf("error decoding from %s: %s", inMediaType, err)
return nil, nil, fmt.Errorf("error decoding from %s: %w", inMediaType, err)
}

encoded, err = runtime.Encode(outCodec, obj)
if err != nil {
return nil, nil, fmt.Errorf("error encoding to %s: %s", outMediaType, err)
return nil, nil, fmt.Errorf("error encoding to %s: %w", outMediaType, err)
}
}

Expand Down Expand Up @@ -222,7 +222,7 @@ func newCodec(codecs serializer.CodecFactory, typeMeta *runtime.TypeMeta, mediaT

gv, err := schema.ParseGroupVersion(typeMeta.APIVersion)
if err != nil {
return nil, fmt.Errorf("unable to parse meta APIVersion '%s': %s", typeMeta.APIVersion, err)
return nil, fmt.Errorf("unable to parse meta APIVersion '%s': %w", typeMeta.APIVersion, err)
}
encoder := codecs.EncoderForVersion(info.Serializer, gv)
decoder := codecs.DecoderToVersion(info.Serializer, gv)
Expand Down
6 changes: 4 additions & 2 deletions pkg/scheme/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,10 @@ import (
"k8s.io/apimachinery/pkg/runtime/serializer"
)

var Scheme = runtime.NewScheme()
var Codecs = serializer.NewCodecFactory(Scheme)
var (
Scheme = runtime.NewScheme()
Codecs = serializer.NewCodecFactory(Scheme)
)

func init() {
metav1.AddToGroupVersion(Scheme, schema.GroupVersion{Version: "v1"})
Expand Down
9 changes: 8 additions & 1 deletion tools/.golangci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,10 @@ linters:
# - deadcode
# - structcheck
# - varcheck
- errorlint
- gofumpt
- goimports
- gosimple
- importas
- ineffassign
- nakedret
Expand All @@ -26,6 +29,9 @@ linters:
- unconvert # Remove unnecessary type conversions
- unparam
- unused
- usestdlibvars
- usetesting
- whitespace
linters-settings: # please keep this alphabetized
goimports:
local-prefixes: go.etcd.io # Put imports beginning with prefix after 3rd-party packages.
Expand Down Expand Up @@ -85,6 +91,7 @@ linters-settings: # please keep this alphabetized
stylecheck:
checks:
- ST1019 # Importing the same package multiple times.

usetesting:
os-mkdir-temp: false
run:
timeout: 30m