Skip to content

Commit

Permalink
task: enable more revive checks (#3106)
Browse files Browse the repository at this point in the history
  • Loading branch information
gssbzn authored Jul 15, 2024
1 parent fb4d0df commit 15ce9fa
Show file tree
Hide file tree
Showing 56 changed files with 260 additions and 270 deletions.
4 changes: 4 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -60,12 +60,15 @@ linters-settings:
- name: blank-imports
- name: context-as-argument
- name: context-keys-type
- name: defer
- name: dot-imports
- name: error-return
- name: error-strings
- name: error-naming
- name: early-return
- name: errorf
- name: exported
- name: import-shadowing
- name: indent-error-flow
- name: if-return
- name: increment-decrement
Expand All @@ -89,6 +92,7 @@ linters-settings:
- name: constant-logical-expr
- name: confusing-naming
- name: unnecessary-stmt
- name: use-any
- name: imports-blocklist
arguments:
- "github.com/pkg/errors"
Expand Down
4 changes: 2 additions & 2 deletions internal/cli/auth/login_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,11 +155,11 @@ Successfully logged in as [email protected].

type confirmMock struct{}

func (confirmMock) Prompt(_ *survey.PromptConfig) (interface{}, error) {
func (confirmMock) Prompt(_ *survey.PromptConfig) (any, error) {
return true, nil
}

func (confirmMock) Cleanup(_ *survey.PromptConfig, _ interface{}) error {
func (confirmMock) Cleanup(_ *survey.PromptConfig, _ any) error {
return nil
}

Expand Down
2 changes: 1 addition & 1 deletion internal/cli/config/set.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func (opts *SetOpts) Run() error {
return err
}
}
var value interface{}
var value any
value = opts.val
if slices.Contains(config.BooleanProperties(), opts.prop) {
value = config.IsTrue(opts.val)
Expand Down
2 changes: 1 addition & 1 deletion internal/cli/delete_opts.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ func NewDeleteOpts(successMsg, failMsg string) *DeleteOpts {

// Delete deletes a resource not associated to a project, it expects a callback
// that should perform the deletion from the store.
func (opts *DeleteOpts) Delete(d interface{}, a ...string) error {
func (opts *DeleteOpts) Delete(d any, a ...string) error {
if !opts.Confirm {
fmt.Println(opts.FailMessage())
return nil
Expand Down
9 changes: 7 additions & 2 deletions internal/cli/deployments/logs.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,11 +83,16 @@ func (opts *DownloadOpts) RunAtlas() error {
if err := opts.downloadLogFile(); err != nil {
return err
}
defer opts.Fs.Remove(opts.Out) //nolint:errcheck
defer func() {
_ = opts.Fs.Remove(opts.Out)
}()

return nil
}

// maxBytes 1k each write to avoid compression bomb.
const maxBytes = 1024

func (*DownloadOpts) write(w io.Writer, r io.Reader) error {
gr, errGz := gzip.NewReader(r)
if errGz != nil {
Expand All @@ -96,7 +101,7 @@ func (*DownloadOpts) write(w io.Writer, r io.Reader) error {

written := false
for {
n, err := io.CopyN(w, gr, 1024) //nolint:mnd // 1k each write to avoid compression bomb
n, err := io.CopyN(w, gr, maxBytes)
if n > 0 {
written = true
}
Expand Down
4 changes: 2 additions & 2 deletions internal/cli/deployments/setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,7 @@ func (opts *SetupOpts) promptDeploymentName() error {
Default: opts.DeploymentName,
}

return telemetry.TrackAskOne(p, &opts.DeploymentName, survey.WithValidator(func(ans interface{}) error {
return telemetry.TrackAskOne(p, &opts.DeploymentName, survey.WithValidator(func(ans any) error {
name, _ := ans.(string)
return options.ValidateDeploymentName(name)
}))
Expand Down Expand Up @@ -342,7 +342,7 @@ func (opts *SetupOpts) promptPort() error {
Default: exportPort,
}

err := telemetry.TrackAskOne(p, &exportPort, survey.WithValidator(func(ans interface{}) error {
err := telemetry.TrackAskOne(p, &exportPort, survey.WithValidator(func(ans any) error {
input, _ := ans.(string)
value, err := strconv.Atoi(input)
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion internal/cli/events/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ var listTemplate = `ID TYPE CREATED{{range valueOrEmptySlice .Results}}
`

func (opts *ListOpts) Run() error {
var r interface{}
var r any
var err error

if opts.orgID != "" {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,8 @@ func (opts *UpdateOpts) InitStore(ctx context.Context) func() error {
}
}

func (opts *UpdateOpts) fillReadOnlyValues(config *atlasv2.ConnectedOrgConfig) {
config.OrgId = opts.ConfigOrgID()
func (opts *UpdateOpts) fillReadOnlyValues(c *atlasv2.ConnectedOrgConfig) {
c.OrgId = opts.ConfigOrgID()
}

func (opts *UpdateOpts) Run() error {
Expand Down
5 changes: 4 additions & 1 deletion internal/cli/logs/download.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,9 @@ func (opts *DownloadOpts) initStore(ctx context.Context) func() error {
}
}

// maxBytes 1k each write to avoid compression bomb.
const maxBytes = 1024

func (opts *DownloadOpts) write(w io.Writer, r io.Reader) error {
if !opts.decompress {
_, err := io.Copy(w, r)
Expand All @@ -71,7 +74,7 @@ func (opts *DownloadOpts) write(w io.Writer, r io.Reader) error {

written := false
for {
n, err := io.CopyN(w, gr, 1024) //nolint:mnd // 1k each write to avoid compression bomb
n, err := io.CopyN(w, gr, maxBytes)
if n > 0 {
written = true
}
Expand Down
16 changes: 9 additions & 7 deletions internal/cli/networking/peering/create/aws_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"github.com/mongodb/mongodb-atlas-cli/atlascli/internal/mocks"
"github.com/mongodb/mongodb-atlas-cli/atlascli/internal/pointer"
"github.com/mongodb/mongodb-atlas-cli/atlascli/internal/test"
"github.com/stretchr/testify/assert"
atlasv2 "go.mongodb.org/atlas-sdk/v20240530002/admin"
)

Expand Down Expand Up @@ -84,24 +85,25 @@ func TestAwsOpts_Run(t *testing.T) {
}

func TestNormalizeAtlasRegion(t *testing.T) {
type test struct {
type tc struct {
input string
want string
}

tests := []test{
tests := []tc{
{input: "eu-west-1", want: "EU_WEST_1"},
{input: "eu_west-1", want: "EU_WEST_1"},
{input: "eu-west_1", want: "EU_WEST_1"},
{input: "EU_WEST_1", want: "EU_WEST_1"},
{input: "eu_west_1", want: "EU_WEST_1"},
}

for _, tc := range tests {
got := normalizeAtlasRegion(tc.input)
if tc.want != got {
t.Errorf("expected: %s, got: %s", tc.want, got)
}
for _, c := range tests {
t.Run(c.input, func(t *testing.T) {
t.Parallel()
got := normalizeAtlasRegion(c.input)
assert.Equal(t, c.want, got)
})
}
}

Expand Down
8 changes: 4 additions & 4 deletions internal/cli/output_opts.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ func (opts *OutputOpts) IsCygwinTerminal() bool {
}

// Print will evaluate the defined format and try to parse it accordingly outputting to the set writer.
func (opts *OutputOpts) Print(o interface{}) error {
func (opts *OutputOpts) Print(o any) error {
if opts.ConfigOutput() == jsonFormat {
return jsonwriter.Print(opts.ConfigWriter(), o)
}
Expand All @@ -131,7 +131,7 @@ func (opts *OutputOpts) Print(o interface{}) error {
return err
}

func (opts *OutputOpts) PrintForCompactResultsResponse(o interface{}) error {
func (opts *OutputOpts) PrintForCompactResultsResponse(o any) error {
if opts.ConfigOutput() == jsonFormat {
compactResponse, err := mapReduceResults(o)
if err == nil {
Expand Down Expand Up @@ -159,13 +159,13 @@ func (opts *OutputOpts) PrintForCompactResultsResponse(o interface{}) error {
return err
}

func mapReduceResults(o interface{}) (interface{}, error) {
func mapReduceResults(o any) (any, error) {
jsonString, err := json.Marshal(o)
if err != nil {
return nil, err
}

var val interface{}
var val any
if e := json.Unmarshal(jsonString, &val); e != nil {
return nil, e
}
Expand Down
4 changes: 2 additions & 2 deletions internal/cli/output_opts_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,8 @@ func TestOutputOpts_mapReduceResults(t *testing.T) {
t.Fatalf("mapReduceResults() unexpected error: %v", err)
}

mapArrayResponse := reflect.ValueOf(compactResults).Interface().([]interface{})
mapResponse := mapArrayResponse[0].(map[string]interface{})
mapArrayResponse := reflect.ValueOf(compactResults).Interface().([]any)
mapResponse := mapArrayResponse[0].(map[string]any)
gotID := mapResponse["id"]
gotName := mapResponse["name"]
if gotID != wantID {
Expand Down
2 changes: 1 addition & 1 deletion internal/cli/projects/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ func (opts *ListOpts) initStore(ctx context.Context) func() error {

func (opts *ListOpts) Run() error {
listOptions := opts.NewAtlasListOptions()
var r interface{}
var r any
var err error
if opts.OrgID != "" {
r, err = opts.store.GetOrgProjects(opts.OrgID, listOptions)
Expand Down
6 changes: 3 additions & 3 deletions internal/cli/search/index_opts.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,17 +158,17 @@ func (opts *IndexOpts) NewSearchIndex() (*atlasv2.ClusterSearchIndex, error) {
// indexFieldParts index field should be fieldName:analyzer:fieldType.
const indexFieldParts = 2

func (opts *IndexOpts) indexFields() (map[string]interface{}, error) {
func (opts *IndexOpts) indexFields() (map[string]any, error) {
if len(opts.fields) == 0 {
return nil, nil
}
fields := make(map[string]interface{})
fields := make(map[string]any)
for _, p := range opts.fields {
f := strings.Split(p, ":")
if len(f) != indexFieldParts {
return nil, fmt.Errorf("partition should be fieldName:fieldType, got: %s", p)
}
fields[f[0]] = map[string]interface{}{
fields[f[0]] = map[string]any{
"type": f[1],
}
}
Expand Down
2 changes: 1 addition & 1 deletion internal/cli/setup/dbuser_setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ func (opts *Opts) askDBUserOptions() error {
return telemetry.TrackAsk(qs, opts)
}

func (opts *Opts) validateUniqueUsername(val interface{}) error {
func (opts *Opts) validateUniqueUsername(val any) error {
username, ok := val.(string)
if !ok {
return fmt.Errorf("the username %s is not valid", username)
Expand Down
2 changes: 1 addition & 1 deletion internal/cli/teams/describe.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ func (opts *DescribeOpts) initStore(ctx context.Context) func() error {
}

func (opts *DescribeOpts) Run() error {
var r interface{}
var r any
var err error

if opts.name != "" {
Expand Down
2 changes: 1 addition & 1 deletion internal/cli/users/describe.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ func (opts *DescribeOpts) initStore(ctx context.Context) func() error {
}

func (opts *DescribeOpts) Run() error {
var r interface{}
var r any
var err error

if opts.username != "" {
Expand Down
6 changes: 3 additions & 3 deletions internal/cli/watch_opts.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,10 @@ const (
speed = 100 * time.Millisecond
)

type Watcher func() (interface{}, bool, error)
type Watcher func() (any, bool, error)

// Watch allow to init the OutputOpts in a functional way.
func (opts *WatchOpts) Watch(f Watcher) (interface{}, error) {
func (opts *WatchOpts) Watch(f Watcher) (any, error) {
if f == nil {
return nil, errors.New("no watcher provided")
}
Expand All @@ -63,7 +63,7 @@ func (opts *WatchOpts) Watch(f Watcher) (interface{}, error) {

var backoffCoefficients = []float32{0.5, 1, 2}

func (opts *WatchOpts) exponentialBackoff(f Watcher) (interface{}, bool, error) {
func (opts *WatchOpts) exponentialBackoff(f Watcher) (any, bool, error) {
if opts.IsRetryableErr == nil {
return f()
}
Expand Down
16 changes: 8 additions & 8 deletions internal/config/profile.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,11 +82,11 @@ var (
)

type Setter interface {
Set(string, interface{})
Set(string, any)
}

type GlobalSetter interface {
SetGlobal(string, interface{})
SetGlobal(string, any)
}

type Saver interface {
Expand Down Expand Up @@ -263,20 +263,20 @@ func (p *Profile) SetName(name string) error {
return nil
}

func Set(name string, value interface{}) { Default().Set(name, value) }
func (p *Profile) Set(name string, value interface{}) {
func Set(name string, value any) { Default().Set(name, value) }
func (p *Profile) Set(name string, value any) {
settings := viper.GetStringMap(p.Name())
settings[name] = value
viper.Set(p.name, settings)
}

func SetGlobal(name string, value interface{}) { viper.Set(name, value) }
func (*Profile) SetGlobal(name string, value interface{}) {
func SetGlobal(name string, value any) { viper.Set(name, value) }
func (*Profile) SetGlobal(name string, value any) {
SetGlobal(name, value)
}

func Get(name string) interface{} { return Default().Get(name) }
func (p *Profile) Get(name string) interface{} {
func Get(name string) any { return Default().Get(name) }
func (p *Profile) Get(name string) any {
if viper.IsSet(name) && viper.Get(name) != "" {
return viper.Get(name)
}
Expand Down
4 changes: 2 additions & 2 deletions internal/container/docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -327,7 +327,7 @@ func (e *dockerImpl) ImagePull(ctx context.Context, name string) error {
}

func (e *dockerImpl) ImageHealthCheck(ctx context.Context, name string) (*ImageHealthCheck, error) {
bytes, err := e.run(ctx, "image", "inspect", "--format", "json", name)
b, err := e.run(ctx, "image", "inspect", "--format", "json", name)
if err != nil {
return nil, err
}
Expand All @@ -349,7 +349,7 @@ func (e *dockerImpl) ImageHealthCheck(ctx context.Context, name string) (*ImageH
}

var inspectOutput []PartialImageInspect
if err := json.Unmarshal(bytes, &inspectOutput); err != nil {
if err := json.Unmarshal(b, &inspectOutput); err != nil {
return nil, err
}

Expand Down
4 changes: 2 additions & 2 deletions internal/decryption/log_record.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ func (logLine *AuditLogLine) decodeLogRecord() (*DecodedLogRecord, error) {
}, nil
}

func processLogRecord(decryptConfig *DecryptSection, logLine *AuditLogLine, lineNb int) (bsonData interface{}, keyInvocationCount uint64, err error) {
func processLogRecord(decryptConfig *DecryptSection, logLine *AuditLogLine, lineNb int) (bsonData any, keyInvocationCount uint64, err error) {
encryptedLogRecord, decodeErr := logLine.decodeLogRecord()
if decodeErr != nil {
return nil, 0, fmt.Errorf("at line %v: %w: %w", lineNb, ErrLogCorrupted, decodeErr)
Expand All @@ -90,7 +90,7 @@ func processLogRecord(decryptConfig *DecryptSection, logLine *AuditLogLine, line
return nil, 0, fmt.Errorf("%w at line %v: %w", ErrDecompressionFailure, lineNb, decompressErr)
}

var bsonParsedLogRecord map[string]interface{}
var bsonParsedLogRecord map[string]any
if bsonErr := bson.Unmarshal(decompressedLogRecord, &bsonParsedLogRecord); bsonErr != nil {
return nil, 0, fmt.Errorf("%w at line %v: %w", ErrParse, lineNb, bsonErr)
}
Expand Down
Loading

0 comments on commit 15ce9fa

Please sign in to comment.