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

fix: enable errorlint in etcdctl and etcdutl directories #18813

Merged
merged 1 commit into from
Oct 31, 2024
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
3 changes: 2 additions & 1 deletion etcdctl/ctlv3/command/auth_command.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
package command

import (
"errors"
"fmt"

"github.com/spf13/cobra"
Expand Down Expand Up @@ -82,7 +83,7 @@ func authEnableCommandFunc(cmd *cobra.Command, args []string) {
if _, err = cli.AuthEnable(ctx); err == nil {
break
}
if err == rpctypes.ErrRootRoleNotExist {
if errors.Is(err, rpctypes.ErrRootRoleNotExist) {
if _, err = cli.RoleAdd(ctx, "root"); err != nil {
break
}
Expand Down
2 changes: 1 addition & 1 deletion etcdctl/ctlv3/command/ep_command.go
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,7 @@ func endpointsFromCluster(cmd *cobra.Command) []string {
}()
membs, err := c.MemberList(ctx)
if err != nil {
err = fmt.Errorf("failed to fetch endpoints from etcd cluster member list: %v", err)
err = fmt.Errorf("failed to fetch endpoints from etcd cluster member list: %w", err)
cobrautl.ExitWithError(cobrautl.ExitError, err)
}

Expand Down
8 changes: 4 additions & 4 deletions etcdctl/ctlv3/command/lease_command.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,14 +61,14 @@ func leaseGrantCommandFunc(cmd *cobra.Command, args []string) {

ttl, err := strconv.ParseInt(args[0], 10, 64)
if err != nil {
cobrautl.ExitWithError(cobrautl.ExitBadArgs, fmt.Errorf("bad TTL (%v)", err))
cobrautl.ExitWithError(cobrautl.ExitBadArgs, fmt.Errorf("bad TTL (%w)", err))
}

ctx, cancel := commandCtx(cmd)
resp, err := mustClientFromCmd(cmd).Grant(ctx, ttl)
cancel()
if err != nil {
cobrautl.ExitWithError(cobrautl.ExitError, fmt.Errorf("failed to grant lease (%v)", err))
cobrautl.ExitWithError(cobrautl.ExitError, fmt.Errorf("failed to grant lease (%w)", err))
}
display.Grant(*resp)
}
Expand Down Expand Up @@ -96,7 +96,7 @@ func leaseRevokeCommandFunc(cmd *cobra.Command, args []string) {
resp, err := mustClientFromCmd(cmd).Revoke(ctx, id)
cancel()
if err != nil {
cobrautl.ExitWithError(cobrautl.ExitError, fmt.Errorf("failed to revoke lease (%v)", err))
cobrautl.ExitWithError(cobrautl.ExitError, fmt.Errorf("failed to revoke lease (%w)", err))
}
display.Revoke(id, *resp)
}
Expand Down Expand Up @@ -202,7 +202,7 @@ func leaseKeepAliveCommandFunc(cmd *cobra.Command, args []string) {
func leaseFromArgs(arg string) v3.LeaseID {
id, err := strconv.ParseInt(arg, 16, 64)
if err != nil {
cobrautl.ExitWithError(cobrautl.ExitBadArgs, fmt.Errorf("bad lease ID arg (%v), expecting ID in Hex", err))
cobrautl.ExitWithError(cobrautl.ExitBadArgs, fmt.Errorf("bad lease ID arg (%w), expecting ID in Hex", err))
}
return v3.LeaseID(id)
}
3 changes: 2 additions & 1 deletion etcdctl/ctlv3/command/lock_command.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,8 @@ func getExitCodeFromError(err error) int {
return cobrautl.ExitSuccess
}

if exitErr, ok := err.(*exec.ExitError); ok {
var exitErr *exec.ExitError
if errors.As(err, &exitErr) {
if status, ok := exitErr.Sys().(syscall.WaitStatus); ok {
return status.ExitStatus()
}
Expand Down
6 changes: 3 additions & 3 deletions etcdctl/ctlv3/command/member_command.go
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ func memberRemoveCommandFunc(cmd *cobra.Command, args []string) {

id, err := strconv.ParseUint(args[0], 16, 64)
if err != nil {
cobrautl.ExitWithError(cobrautl.ExitBadArgs, fmt.Errorf("bad member ID arg (%v), expecting ID in Hex", err))
cobrautl.ExitWithError(cobrautl.ExitBadArgs, fmt.Errorf("bad member ID arg (%w), expecting ID in Hex", err))
}

ctx, cancel := commandCtx(cmd)
Expand All @@ -208,7 +208,7 @@ func memberUpdateCommandFunc(cmd *cobra.Command, args []string) {

id, err := strconv.ParseUint(args[0], 16, 64)
if err != nil {
cobrautl.ExitWithError(cobrautl.ExitBadArgs, fmt.Errorf("bad member ID arg (%v), expecting ID in Hex", err))
cobrautl.ExitWithError(cobrautl.ExitBadArgs, fmt.Errorf("bad member ID arg (%w), expecting ID in Hex", err))
}

if len(memberPeerURLs) == 0 {
Expand Down Expand Up @@ -251,7 +251,7 @@ func memberPromoteCommandFunc(cmd *cobra.Command, args []string) {

id, err := strconv.ParseUint(args[0], 16, 64)
if err != nil {
cobrautl.ExitWithError(cobrautl.ExitBadArgs, fmt.Errorf("bad member ID arg (%v), expecting ID in Hex", err))
cobrautl.ExitWithError(cobrautl.ExitBadArgs, fmt.Errorf("bad member ID arg (%w), expecting ID in Hex", err))
}

ctx, cancel := commandCtx(cmd)
Expand Down
2 changes: 1 addition & 1 deletion etcdctl/ctlv3/command/put_command.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ func getPutOp(args []string) (string, string, []clientv3.OpOption) {

id, err := strconv.ParseInt(leaseStr, 16, 64)
if err != nil {
cobrautl.ExitWithError(cobrautl.ExitBadArgs, fmt.Errorf("bad lease ID (%v), expecting ID in Hex", err))
cobrautl.ExitWithError(cobrautl.ExitBadArgs, fmt.Errorf("bad lease ID (%w), expecting ID in Hex", err))
}

var opts []clientv3.OpOption
Expand Down
2 changes: 1 addition & 1 deletion etcdctl/ctlv3/command/txn_command.go
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ func ParseCompare(line string) (*clientv3.Cmp, error) {
return nil, fmt.Errorf("malformed comparison: %s; got %s(%q) %s %q", line, target, key, op, val)
}
if serr != nil {
return nil, fmt.Errorf("malformed comparison: %s (%v)", line, serr)
return nil, fmt.Errorf("malformed comparison: %s (%w)", line, serr)
}

var (
Expand Down
4 changes: 2 additions & 2 deletions etcdctl/ctlv3/command/user_command.go
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,7 @@ func readPasswordInteractive(name string) string {
prompt1 := fmt.Sprintf("Password of %s: ", name)
password1, err1 := speakeasy.Ask(prompt1)
if err1 != nil {
cobrautl.ExitWithError(cobrautl.ExitBadArgs, fmt.Errorf("failed to ask password: %s", err1))
cobrautl.ExitWithError(cobrautl.ExitBadArgs, fmt.Errorf("failed to ask password: %w", err1))
}

if len(password1) == 0 {
Expand All @@ -290,7 +290,7 @@ func readPasswordInteractive(name string) string {
prompt2 := fmt.Sprintf("Type password of %s again for confirmation: ", name)
password2, err2 := speakeasy.Ask(prompt2)
if err2 != nil {
cobrautl.ExitWithError(cobrautl.ExitBadArgs, fmt.Errorf("failed to ask password: %s", err2))
cobrautl.ExitWithError(cobrautl.ExitBadArgs, fmt.Errorf("failed to ask password: %w", err2))
}

if password1 != password2 {
Expand Down
2 changes: 1 addition & 1 deletion etcdctl/ctlv3/command/watch_command.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ func watchInteractiveFunc(cmd *cobra.Command, osArgs []string, envKey, envRange
for {
l, err := reader.ReadString('\n')
if err != nil {
cobrautl.ExitWithError(cobrautl.ExitInvalidInput, fmt.Errorf("error reading watch request line: %v", err))
cobrautl.ExitWithError(cobrautl.ExitInvalidInput, fmt.Errorf("error reading watch request line: %w", err))
}
l = strings.TrimSuffix(l, "\n")

Expand Down
3 changes: 2 additions & 1 deletion etcdctl/ctlv3/command/watch_command_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
package command

import (
"errors"
"reflect"
"testing"
)
Expand Down Expand Up @@ -534,7 +535,7 @@ func Test_parseWatchArgs(t *testing.T) {
}
for i, ts := range tt {
watchArgs, execArgs, err := parseWatchArgs(ts.osArgs, ts.commandArgs, ts.envKey, ts.envRange, ts.interactive)
if err != ts.err {
if !errors.Is(err, ts.err) {
t.Fatalf("#%d: error expected %v, got %v", i, ts.err, err)
}
if !reflect.DeepEqual(watchArgs, ts.watchArgs) {
Expand Down
2 changes: 1 addition & 1 deletion etcdutl/etcdutl/defrag_command.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ func defragCommandFunc(cmd *cobra.Command, args []string) {
err := DefragData(defragDataDir)
if err != nil {
cobrautl.ExitWithError(cobrautl.ExitError,
fmt.Errorf("Failed to defragment etcd data[%s] (%v)", defragDataDir, err))
fmt.Errorf("Failed to defragment etcd data[%s] (%w)", defragDataDir, err))
}
}

Expand Down
6 changes: 3 additions & 3 deletions etcdutl/etcdutl/migrate_command.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ func (o *migrateOptions) Config() (*migrateConfig, error) {
}
c.targetVersion, err = semver.NewVersion(o.targetVersion + ".0")
if err != nil {
return nil, fmt.Errorf("failed to parse target version: %v", err)
return nil, fmt.Errorf("failed to parse target version: %w", err)
}
if c.targetVersion.LessThan(version.V3_5) {
return nil, fmt.Errorf(`target version %q not supported. Minimal "3.5"`, storageVersionToString(c.targetVersion))
Expand All @@ -97,12 +97,12 @@ func (o *migrateOptions) Config() (*migrateConfig, error) {
walPath := datadir.ToWALDir(o.dataDir)
w, err := wal.OpenForRead(c.lg, walPath, walpb.Snapshot{})
if err != nil {
return nil, fmt.Errorf(`failed to open wal: %v`, err)
return nil, fmt.Errorf(`failed to open wal: %w`, err)
}
defer w.Close()
c.walVersion, err = wal.ReadWALVersion(w)
if err != nil {
return nil, fmt.Errorf(`failed to read wal: %v`, err)
return nil, fmt.Errorf(`failed to read wal: %w`, err)
}

return c, nil
Expand Down