Skip to content

Commit 06c76c0

Browse files
authored
chore: add support for usetesting, nlreturn and exptostd (#4499)
1 parent bfebf9a commit 06c76c0

File tree

196 files changed

+767
-9
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

196 files changed

+767
-9
lines changed

.golangci.yml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ linters:
1919
- errcheck # Errcheck is a program for checking for unchecked errors in go programs. These unchecked errors can be critical bugs in some cases [fast: false, auto-fix: false]
2020
- errchkjson # Checks types passed to the json encoding functions. Reports unsupported types and optionally reports occasions, where the check for the returned error can be omitted. [fast: false, auto-fix: false]
2121
- errname # Checks that sentinel errors are prefixed with the `Err` and error types are suffixed with the `Error`. [fast: false, auto-fix: false]
22+
- exptostd # Detects functions from golang.org/x/exp/ that can be replaced by std functions. [auto-fix]
2223
- gci # Gci controls golang package import order and makes it always deterministic. [fast: true, auto-fix: false]
2324
- gocheckcompilerdirectives # Checks that go compiler directive comments (//go:) are valid. [fast: true, auto-fix: false]
2425
- gochecksumtype # Run exhaustiveness checks on Go "sum types" [fast: false, auto-fix: false]
@@ -46,6 +47,7 @@ linters:
4647
- musttag # enforce field tags in (un)marshaled structs [fast: false, auto-fix: false]
4748
- nakedret # Finds naked returns in functions greater than a specified function length [fast: true, auto-fix: false]
4849
- nilerr # Finds the code that returns nil even if it checks that the error is not nil. [fast: false, auto-fix: false]
50+
- nlreturn # nlreturn checks for a new line before return and branch statements to increase code clarity [fast: true, auto-fix: false]
4951
- nolintlint # Reports ill-formed or insufficient nolint directives [fast: true, auto-fix: false]
5052
- nosprintfhostport # Checks for misuse of Sprintf to construct a host with port in a URL. [fast: true, auto-fix: false]
5153
- perfsprint # Checks that fmt.Sprintf can be replaced with a faster alternative. [fast: false, auto-fix: false]
@@ -70,6 +72,7 @@ linters:
7072
- unparam # Reports unused function parameters [fast: false, auto-fix: false]
7173
- unused #(megacheck): Checks Go code for unused constants, variables, functions and types [fast: false, auto-fix: false]
7274
- usestdlibvars # A linter that detect the possibility to use variables/constants from the Go standard library. [fast: true, auto-fix: false]
75+
- usetesting # Reports uses of functions with replacement inside the testing package. [auto-fix]
7376
- wastedassign # wastedassign finds wasted assignment statements. [fast: false, auto-fix: false]
7477
- whitespace # Tool for detection of leading and trailing whitespace [fast: true, auto-fix: true]
7578
- zerologlint # Detects the wrong usage of `zerolog` that a user forgets to dispatch with `Send` or `Msg` [fast: false, auto-fix: false]
@@ -92,7 +95,6 @@ linters:
9295
- maintidx # maintidx measures the maintainability index of each function. [fast: true, auto-fix: false]
9396
- nestif # Reports deeply nested if statements [fast: true, auto-fix: false]
9497
- nilnil # Checks that there is no simultaneous return of `nil` error and an invalid value. [fast: false, auto-fix: false]
95-
- nlreturn # nlreturn checks for a new line before return and branch statements to increase code clarity [fast: true, auto-fix: false]
9698
- varnamelen # checks that the length of a variable's name matches its scope [fast: false, auto-fix: false]
9799
- wsl # Whitespace Linter - Forces you to use empty lines! [fast: true, auto-fix: false]
98100

cmd/scw-sweeper/main.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ func getConfigProfile() *scw.Profile {
4747
if err != nil {
4848
return &scw.Profile{}
4949
}
50+
5051
return profile
5152
}
5253

@@ -189,12 +190,14 @@ func mainNoExit() int {
189190
err = vpcgwSweeper.SweepAllLocalities(client)
190191
if err != nil {
191192
log.Fatalf("Error sweeping vpcgw: %s", err)
193+
192194
return -1
193195
}
194196

195197
err = webhostingSweeper.SweepAllLocalities(client)
196198
if err != nil {
197199
log.Fatalf("Error sweeping webhosting: %s", err)
200+
198201
return -1
199202
}
200203

cmd/scw/main.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,10 @@ func buildVersion() string {
5656
if ok && buildInfos.Main.Version != "(devel)" && buildInfos.Main.Version != "" {
5757
return buildInfos.Main.Version
5858
}
59+
5960
return "v2+dev"
6061
}
62+
6163
return Version
6264
}
6365

core/alias.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,6 @@ func aliasDisabled(cmd string) bool {
66
case "alias", "autocomplete", "init":
77
return true
88
}
9+
910
return false
1011
}

core/arg_specs.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ func (s ArgSpecs) GetPositionalArg() *ArgSpec {
2424
positionalArg = argSpec
2525
}
2626
}
27+
2728
return positionalArg
2829
}
2930

@@ -35,6 +36,7 @@ func (s ArgSpecs) GetDeprecated(deprecated bool) ArgSpecs {
3536
result = append(result, argSpec)
3637
}
3738
}
39+
3840
return result
3941
}
4042

@@ -44,13 +46,15 @@ func (s ArgSpecs) GetByName(name string) *ArgSpec {
4446
return spec
4547
}
4648
}
49+
4750
return nil
4851
}
4952

5053
func (s *ArgSpecs) DeleteByName(name string) {
5154
for i, spec := range *s {
5255
if spec.Name == name {
5356
*s = append((*s)[:i], (*s)[i+1:]...)
57+
5458
return
5559
}
5660
}
@@ -65,6 +69,7 @@ func (s *ArgSpecs) AddBefore(name string, argSpec *ArgSpec) {
6569
newSpecs = append(newSpecs, argSpec)
6670
newSpecs = append(newSpecs, (*s)[i:]...)
6771
*s = newSpecs
72+
6873
return
6974
}
7075
}
@@ -127,6 +132,7 @@ func ZoneArgSpec(zones ...scw.Zone) *ArgSpec {
127132
for _, zone := range zones {
128133
enumValues = append(enumValues, zone.String())
129134
}
135+
130136
return &ArgSpec{
131137
Name: "zone",
132138
Short: "Zone to target. If none is passed will use default zone from the config",
@@ -140,6 +146,7 @@ func ZoneArgSpec(zones ...scw.Zone) *ArgSpec {
140146
if validation.IsZone(value.(scw.Zone).String()) {
141147
return nil
142148
}
149+
143150
return &CliError{
144151
Err: fmt.Errorf("invalid zone %s", value),
145152
Hint: "Zone format should look like XX-XXX-X (e.g. fr-par-1)",
@@ -148,6 +155,7 @@ func ZoneArgSpec(zones ...scw.Zone) *ArgSpec {
148155
Default: func(ctx context.Context) (value string, doc string) {
149156
client := ExtractClient(ctx)
150157
zone, _ := client.GetDefaultZone()
158+
151159
return zone.String(), zone.String()
152160
},
153161
}
@@ -158,6 +166,7 @@ func RegionArgSpec(regions ...scw.Region) *ArgSpec {
158166
for _, region := range regions {
159167
enumValues = append(enumValues, region.String())
160168
}
169+
161170
return &ArgSpec{
162171
Name: "region",
163172
Short: "Region to target. If none is passed will use default region from the config",
@@ -171,6 +180,7 @@ func RegionArgSpec(regions ...scw.Region) *ArgSpec {
171180
if validation.IsRegion(value.(scw.Region).String()) {
172181
return nil
173182
}
183+
174184
return &CliError{
175185
Err: fmt.Errorf("invalid region %s", value),
176186
Hint: "Region format should look like XX-XXX (e.g. fr-par)",
@@ -179,6 +189,7 @@ func RegionArgSpec(regions ...scw.Region) *ArgSpec {
179189
Default: func(ctx context.Context) (value string, doc string) {
180190
client := ExtractClient(ctx)
181191
region, _ := client.GetDefaultRegion()
192+
182193
return region.String(), region.String()
183194
},
184195
}

core/autocomplete.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ func (node *AutoCompleteNode) addFlags(flags []FlagSpec) {
6767
// newAutoCompleteResponse builds a new AutocompleteResponse
6868
func newAutoCompleteResponse(suggestions []string) *AutocompleteResponse {
6969
sort.Strings(suggestions)
70+
7071
return &AutocompleteResponse{
7172
Suggestions: suggestions,
7273
}
@@ -80,6 +81,7 @@ func NewAutoCompleteCommandNode(flags []FlagSpec) *AutoCompleteNode {
8081
Type: AutoCompleteNodeTypeCommand,
8182
}
8283
node.addFlags(flags)
84+
8385
return node
8486
}
8587

@@ -128,6 +130,7 @@ func NewAutoCompleteFlagNode(parent *AutoCompleteNode, flagSpec *FlagSpec) *Auto
128130
if len(node.Children) == 0 {
129131
node.Children = parent.Children
130132
}
133+
131134
return node
132135
}
133136

@@ -142,6 +145,7 @@ func (node *AutoCompleteNode) GetChildOrCreate(name string, aliases []string, fl
142145
node.Children[alias] = childNode
143146
}
144147
}
148+
145149
return node.Children[name]
146150
}
147151

@@ -164,6 +168,7 @@ func (node *AutoCompleteNode) GetChildMatch(name string) (*AutoCompleteNode, boo
164168
return child, true
165169
}
166170
}
171+
167172
return nil, false
168173
}
169174

@@ -178,6 +183,7 @@ func (node *AutoCompleteNode) isLeafCommand() bool {
178183
return false
179184
}
180185
}
186+
181187
return true
182188
}
183189

@@ -205,6 +211,7 @@ func BuildAutoCompleteTree(ctx context.Context, commands *Commands) *AutoComplet
205211
for _, argSpec := range nonDeprecatedArgs {
206212
if argSpec.Positional {
207213
node.Children[positionalValueNodeID] = NewAutoCompleteArgNode(cmd, argSpec)
214+
208215
continue
209216
}
210217
node.Children[argSpec.Name+"="] = NewAutoCompleteArgNode(cmd, argSpec)
@@ -322,6 +329,7 @@ func AutoComplete(ctx context.Context, leftWords []string, wordToComplete string
322329
suggestions = append(suggestions, positionalSuggestion)
323330
}
324331
}
332+
325333
continue
326334
}
327335

@@ -346,6 +354,7 @@ func AutoComplete(ctx context.Context, leftWords []string, wordToComplete string
346354
continue
347355
}
348356
suggestions = append(suggestions, key)
357+
349358
continue
350359
}
351360

@@ -391,16 +400,19 @@ func AutoCompleteArgValue(ctx context.Context, cmd *Command, argSpec *ArgSpec, a
391400
suggestions = append(suggestions, value)
392401
}
393402
}
403+
394404
return suggestions
395405
}
396406

397407
func isCompletingArgValue(wordToComplete string) bool {
398408
wordParts := strings.SplitN(wordToComplete, "=", 2)
409+
399410
return len(wordParts) == 2
400411
}
401412

402413
func splitArgWord(wordToComplete string) (string, string) {
403414
wordParts := strings.SplitN(wordToComplete, "=", 2)
415+
404416
return wordParts[0] + "=", wordParts[1]
405417
}
406418

@@ -413,6 +425,7 @@ func wordValue(word string) string {
413425
if len(words) >= 2 {
414426
return words[1]
415427
}
428+
416429
return ""
417430
}
418431

@@ -447,6 +460,7 @@ func hasPrefix(key, wordToComplete string) bool {
447460
if leftKey == leftWord || leftKey == sliceSchema || leftKey == mapSchema {
448461
return hasPrefix(rightKey, rightWord)
449462
}
463+
450464
return false
451465
}
452466

@@ -473,6 +487,7 @@ func keySuggestion(key string, completedArg map[string]string, wordToComplete st
473487
return []string{}
474488
}
475489
}
490+
476491
return []string{newKey}
477492
}
478493

@@ -518,6 +533,7 @@ func keySuggestion(key string, completedArg map[string]string, wordToComplete st
518533
for k := 1; k <= j; k++ {
519534
baseIndex[len(baseIndex)-k] = "0"
520535
}
536+
521537
break
522538
}
523539
if strings.HasSuffix(newKey, ".") {
@@ -535,5 +551,6 @@ func keySuggestion(key string, completedArg map[string]string, wordToComplete st
535551
baseIndex[len(baseIndex)-j-1] = strconv.Itoa(newIndex + 1)
536552
}
537553
}
554+
538555
return finalKeys
539556
}

core/autocomplete_utils.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ func AutocompleteProfileName() AutoCompleteArgFunc {
7676
if strings.HasPrefix(scw.DefaultProfileName, prefix) { //nolint:gocritic
7777
res = append(res, scw.DefaultProfileName)
7878
}
79+
7980
return res
8081
}
8182
}
@@ -202,6 +203,7 @@ func listRawArgsLocalities(completedArgs map[string]string, cmd *Command) []stri
202203
listRawArgs = append(listRawArgs, arg+value)
203204
}
204205
}
206+
205207
return listRawArgs
206208
}
207209

core/bootstrap.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,7 @@ func Bootstrap(config *BootstrapConfig) (exitCode int, result interface{}, err e
132132
})
133133
if err != nil {
134134
_, _ = fmt.Fprintln(config.Stderr, err)
135+
135136
return 1, nil, err
136137
}
137138
interactive.SetOutputWriter(config.Stderr) // set printer for interactive function (always stderr).
@@ -154,6 +155,7 @@ func Bootstrap(config *BootstrapConfig) (exitCode int, result interface{}, err e
154155
if printErr != nil {
155156
_, _ = fmt.Fprintln(config.Stderr, printErr)
156157
}
158+
157159
return 1, nil, err
158160
}
159161
}
@@ -205,6 +207,7 @@ func Bootstrap(config *BootstrapConfig) (exitCode int, result interface{}, err e
205207
if printErr != nil {
206208
_, _ = fmt.Fprintln(config.Stderr, printErr)
207209
}
210+
208211
return 1, nil, err
209212
}
210213
meta.CliConfig = cliCfg
@@ -217,6 +220,7 @@ func Bootstrap(config *BootstrapConfig) (exitCode int, result interface{}, err e
217220
})
218221
if err != nil {
219222
_, _ = fmt.Fprintln(config.Stderr, err)
223+
220224
return 1, nil, err
221225
}
222226
}
@@ -243,6 +247,7 @@ func Bootstrap(config *BootstrapConfig) (exitCode int, result interface{}, err e
243247
// ShellMode
244248
if len(config.Args) >= 2 && config.Args[1] == "shell" {
245249
RunShell(ctx, printer, meta, rootCmd, config.Args)
250+
246251
return 0, meta.result, nil
247252
}
248253

@@ -273,6 +278,7 @@ func Bootstrap(config *BootstrapConfig) (exitCode int, result interface{}, err e
273278
if printErr != nil {
274279
_, _ = fmt.Fprintln(os.Stderr, err)
275280
}
281+
276282
return errorCode, nil, err
277283
}
278284

core/build_info.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ type BuildInfo struct {
2525

2626
func (b *BuildInfo) MarshalJSON() ([]byte, error) {
2727
type Tmp BuildInfo
28+
2829
return json.Marshal(
2930
struct {
3031
Tmp
@@ -50,6 +51,7 @@ func (b *BuildInfo) GetUserAgent() string {
5051
if b.Version != nil {
5152
return b.UserAgentPrefix + "/" + b.Version.String()
5253
}
54+
5355
return b.UserAgentPrefix
5456
}
5557

@@ -65,13 +67,15 @@ func (b *BuildInfo) Tags() map[string]string {
6567
func (b *BuildInfo) checkVersion(ctx context.Context) {
6668
if !b.IsRelease() || ExtractEnv(ctx, scwDisableCheckVersionEnv) == "true" {
6769
ExtractLogger(ctx).Debug("skipping check version")
70+
6871
return
6972
}
7073

7174
// pull latest version
7275
latestVersion, err := getLatestVersion(ExtractHTTPClient(ctx))
7376
if err != nil {
7477
ExtractLogger(ctx).Debugf("failed to retrieve latest version: %s\n", err)
78+
7579
return
7680
}
7781

core/build_info_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ func Test_CheckVersion(t *testing.T) {
8484
err := core.CreateAndCloseFile(filePath)
8585
require.NoError(t, err)
8686
twoDaysAgo := time.Now().Local().Add(-2 * time.Hour * 24)
87+
8788
return os.Chtimes(filePath, twoDaysAgo, twoDaysAgo)
8889
},
8990
Cmd: "scw plop",

0 commit comments

Comments
 (0)