Skip to content

Commit a9232ac

Browse files
remyleoneMia-Cross
andauthored
chore: add support for modernize (#5272)
Co-authored-by: Mia-Cross <lmarabese@scaleway.com>
1 parent 1ae0709 commit a9232ac

File tree

11 files changed

+27
-46
lines changed

11 files changed

+27
-46
lines changed

.golangci.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ linters:
5353
- makezero # Finds slice declarations with non-zero initial length [fast: false, auto-fix: false]
5454
- mirror # reports wrong mirror patterns of bytes/strings usage [fast: false, auto-fix: false]
5555
- misspell # Finds commonly misspelled English words in comments [fast: true, auto-fix: true]
56+
- modernize # A suite of analyzers that suggest simplifications to Go code, using modern language and library features.
5657
- musttag # enforce field tags in (un)marshaled structs [fast: false, auto-fix: false]
5758
- nakedret # Finds naked returns in functions greater than a specified function length [fast: true, auto-fix: false]
5859
- nilerr # Finds the code that returns nil even if it checks that the error is not nil. [fast: false, auto-fix: false]
@@ -92,6 +93,9 @@ linters:
9293
rules:
9394
- name: exported
9495
disabled: true
96+
modernize:
97+
disable:
98+
- reflecttypefor
9599

96100
exclusions:
97101
paths:

core/arg_specs.go

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package core
33
import (
44
"context"
55
"fmt"
6+
"slices"
67
"strings"
78
"time"
89

@@ -150,10 +151,8 @@ func ZoneArgSpec(zones ...scw.Zone) *ArgSpec {
150151
Short: "Zone to target. If none is passed will use default zone from the config",
151152
EnumValues: enumValues,
152153
ValidateFunc: func(_ *ArgSpec, value any) error {
153-
for _, zone := range zones {
154-
if value.(scw.Zone) == zone {
155-
return nil
156-
}
154+
if slices.Contains(zones, value.(scw.Zone)) {
155+
return nil
157156
}
158157
if validation.IsZone(value.(scw.Zone).String()) {
159158
return nil
@@ -184,10 +183,8 @@ func RegionArgSpec(regions ...scw.Region) *ArgSpec {
184183
Short: "Region to target. If none is passed will use default region from the config",
185184
EnumValues: enumValues,
186185
ValidateFunc: func(_ *ArgSpec, value any) error {
187-
for _, region := range regions {
188-
if value.(scw.Region) == region {
189-
return nil
190-
}
186+
if slices.Contains(regions, value.(scw.Region)) {
187+
return nil
191188
}
192189
if validation.IsRegion(value.(scw.Region).String()) {
193190
return nil

core/testing.go

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import (
1414
"path"
1515
"path/filepath"
1616
"regexp"
17+
"slices"
1718
"strconv"
1819
"strings"
1920
"testing"
@@ -320,13 +321,8 @@ func folderUsesVCRv4(fullFolderPath string) bool {
320321
fullPathSplit := strings.Split(fullFolderPath, string(os.PathSeparator))
321322

322323
folder := fullPathSplit[len(fullPathSplit)-2]
323-
for _, migratedFolder := range foldersUsingVCRv4 {
324-
if migratedFolder == folder {
325-
return true
326-
}
327-
}
328324

329-
return false
325+
return slices.Contains(foldersUsingVCRv4, folder)
330326
}
331327

332328
// Run a CLI integration test. See TestConfig for configuration option

core/validate.go

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"context"
55
"fmt"
66
"reflect"
7+
"slices"
78
"strconv"
89
"strings"
910

@@ -225,13 +226,7 @@ func DefaultArgSpecValidateFunc() ArgSpecValidateFunc {
225226
}
226227

227228
func stringExists(strs []string, s string) bool {
228-
for _, s2 := range strs {
229-
if s == s2 {
230-
return true
231-
}
232-
}
233-
234-
return false
229+
return slices.Contains(strs, s)
235230
}
236231

237232
// ValidateSecretKey validates a secret key ID.

internal/alias/alias.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,9 @@ func (a *Alias) Args() []string {
2323
func (a *Alias) computeArgs() {
2424
a.args = []string{}
2525
for _, cmd := range a.Command {
26-
argSplitterIndex := strings.Index(cmd, "=")
27-
if argSplitterIndex != -1 {
28-
a.args = append(a.args, cmd[:argSplitterIndex])
26+
before, _, ok := strings.Cut(cmd, "=")
27+
if ok {
28+
a.args = append(a.args, before)
2929
}
3030
}
3131
}

internal/args/args.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"fmt"
55
"reflect"
66
"regexp"
7+
"slices"
78
"strings"
89

910
"github.com/scaleway/scaleway-sdk-go/strcase"
@@ -317,10 +318,8 @@ func listArgTypeFields(base string, argType reflect.Type) []string {
317318

318319
return fields
319320
default:
320-
for _, skippedArg := range listArgTypeFieldsSkippedArguments {
321-
if base == skippedArg {
322-
return []string{}
323-
}
321+
if slices.Contains(listArgTypeFieldsSkippedArguments, base) {
322+
return []string{}
324323
}
325324

326325
return []string{base}

internal/editor/marshal.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ func addTemplate(content []byte, template string, mode MarshalMode) []byte {
7777
}
7878
newContent := []byte(nil)
7979

80-
for _, line := range strings.Split(template, "\n") {
80+
for line := range strings.SplitSeq(template, "\n") {
8181
newContent = append(newContent, []byte("#"+line+"\n")...)
8282
}
8383

internal/editor/reflect.go

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package editor
22

33
import (
44
"reflect"
5+
"slices"
56
)
67

78
func areSameType(v1 reflect.Value, v2 reflect.Value) bool {
@@ -34,13 +35,7 @@ func areSameType(v1 reflect.Value, v2 reflect.Value) bool {
3435
}
3536

3637
func hasTag(tags []string, actualTag string) bool {
37-
for _, tag := range tags {
38-
if tag == actualTag {
39-
return true
40-
}
41-
}
42-
43-
return false
38+
return slices.Contains(tags, actualTag)
4439
}
4540

4641
func valueMapperWithoutOpt(

internal/interactive/print.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,8 @@ func Line(char string) string {
4747

4848
func Center(str string) string {
4949
longestLine := 0
50-
lines := strings.Split(str, "\n")
51-
for _, line := range lines {
50+
lines := strings.SplitSeq(str, "\n")
51+
for line := range lines {
5252
longestLine = int(math.Max(float64(longestLine), float64(len(line))))
5353
}
5454

internal/namespaces/instance/v1/custom_ssh_config.go

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"errors"
66
"fmt"
77
"reflect"
8+
"slices"
89
"strings"
910

1011
"github.com/scaleway/scaleway-cli/v2/core"
@@ -28,13 +29,7 @@ type sshConfigServer struct {
2829
}
2930

3031
func (s sshConfigServer) InPrivateNetwork(id string) bool {
31-
for _, pnID := range s.PrivateNetworksID {
32-
if pnID == id {
33-
return true
34-
}
35-
}
36-
37-
return false
32+
return slices.Contains(s.PrivateNetworksID, id)
3833
}
3934

4035
type sshConfigInstallRequest struct {

0 commit comments

Comments
 (0)