Skip to content

Commit dd557be

Browse files
authored
chore: add support for perfsprint (#4070)
1 parent 41aa419 commit dd557be

File tree

74 files changed

+226
-188
lines changed

Some content is hidden

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

74 files changed

+226
-188
lines changed

.golangci.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ linters:
4545
- nakedret # Finds naked returns in functions greater than a specified function length [fast: true, auto-fix: false]
4646
- nolintlint # Reports ill-formed or insufficient nolint directives [fast: true, auto-fix: false]
4747
- nosprintfhostport # Checks for misuse of Sprintf to construct a host with port in a URL. [fast: true, auto-fix: false]
48+
- perfsprint # Checks that fmt.Sprintf can be replaced with a faster alternative. [fast: false, auto-fix: false]
4849
- prealloc # Finds slice declarations that could potentially be pre-allocated [fast: true, auto-fix: false]
4950
- predeclared # find code that shadows one of Go's predeclared identifiers [fast: true, auto-fix: false]
5051
- promlinter # Check Prometheus metrics naming via promlint [fast: true, auto-fix: false]
@@ -102,6 +103,9 @@ issues:
102103
exclude-dirs:
103104
- internal/pkg
104105

106+
max-issues-per-linter: 0
107+
max-same-issues: 0
108+
105109
exclude-rules:
106110
- path: _test\.go
107111
linters:

internal/args/errors.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ type CannotParseDateError struct {
188188
}
189189

190190
func (e *CannotParseDateError) Error() string {
191-
return fmt.Sprintf(`date parsing error: could not parse %s`, e.ArgValue)
191+
return "date parsing error: could not parse " + e.ArgValue
192192
}
193193

194194
type CannotParseBoolError struct {

internal/args/unmarshal.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ package args
55
// into CLI arguments represented as Go data.
66

77
import (
8+
"errors"
89
"fmt"
910
"io"
1011
"net"
@@ -32,7 +33,7 @@ var unmarshalFuncs = map[reflect.Type]UnmarshalFunc{
3233
// Only support G, GB for now (case insensitive).
3334
value = strings.ToLower(value)
3435
if !strings.HasSuffix(value, "g") && !strings.HasSuffix(value, "gb") {
35-
return fmt.Errorf("size must be defined using the G or GB unit")
36+
return errors.New("size must be defined using the G or GB unit")
3637
}
3738

3839
bytes, err := humanize.ParseBytes(value)
@@ -70,7 +71,7 @@ var unmarshalFuncs = map[reflect.Type]UnmarshalFunc{
7071
}
7172

7273
if len(value) == 0 {
73-
return fmt.Errorf("empty time given")
74+
return errors.New("empty time given")
7475
}
7576

7677
// Handle relative time

internal/core/checks_test.go

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

33
import (
44
"context"
5-
"fmt"
5+
"errors"
66
"path/filepath"
77
"reflect"
88
"strings"
@@ -35,7 +35,7 @@ func TestCheckAPIKey(t *testing.T) {
3535
api := iam.NewAPI(ctx.Client)
3636
accessKey, exists := ctx.Client.GetAccessKey()
3737
if !exists {
38-
return fmt.Errorf("missing access-key")
38+
return errors.New("missing access-key")
3939
}
4040

4141
apiKey, err := api.GetAPIKey(&iam.GetAPIKeyRequest{

internal/core/cobra_usage_builder.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ func buildExamples(binaryName string, cmd *Command) string {
126126

127127
for _, cmdExample := range cmd.Examples {
128128
// Build title.
129-
title := fmt.Sprintf(" %s", cmdExample.Short)
129+
title := " " + cmdExample.Short
130130
commandLine := cmdExample.GetCommandLine(binaryName, cmd)
131131

132132
commandLine = interactive.Indent(commandLine, 4)

internal/core/cobra_utils.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -59,15 +59,15 @@ func cobraRun(ctx context.Context, cmd *Command) func(*cobra.Command, []string)
5959
if exist {
6060
otherArgs := rawArgs.Remove(positionalArgSpec.Name)
6161
return &CliError{
62-
Err: fmt.Errorf("a positional argument is required for this command"),
62+
Err: errors.New("a positional argument is required for this command"),
6363
Hint: positionalArgHint(meta.BinaryName, cmd, value, otherArgs, len(positionalArgs) > 0),
6464
}
6565
}
6666

6767
// If no positional arguments were provided, return an error
6868
if len(positionalArgs) == 0 {
6969
return &CliError{
70-
Err: fmt.Errorf("a positional argument is required for this command"),
70+
Err: errors.New("a positional argument is required for this command"),
7171
Hint: positionalArgHint(meta.BinaryName, cmd, "<"+positionalArgSpec.Name+">", rawArgs, false),
7272
}
7373
}
@@ -209,7 +209,7 @@ func handleUnmarshalErrors(cmd *Command, unmarshalErr *args.UnmarshalArgError) e
209209
switch e.Err.(type) {
210210
case *args.CannotParseBoolError:
211211
return &CliError{
212-
Err: fmt.Errorf(""),
212+
Err: errors.New(""),
213213
Message: fmt.Sprintf("invalid value for '%s' argument: invalid boolean value", unmarshalErr.ArgName),
214214
Hint: "Possible values: true, false",
215215
}
@@ -237,7 +237,7 @@ Relative time error: %s
237237

238238
return &CliError{
239239
Err: fmt.Errorf("invalid argument '%s': %s", unmarshalErr.ArgName, e.Error()),
240-
Hint: fmt.Sprintf("Valid arguments are: %s", strings.Join(argNames, ", ")),
240+
Hint: "Valid arguments are: " + strings.Join(argNames, ", "),
241241
}
242242
case *args.UnknownArgError:
243243
argNames := []string(nil)
@@ -248,7 +248,7 @@ Relative time error: %s
248248

249249
return &CliError{
250250
Err: fmt.Errorf("unknown argument '%s'", unmarshalErr.ArgName),
251-
Hint: fmt.Sprintf("Valid arguments are: %s", strings.Join(argNames, ", ")),
251+
Hint: "Valid arguments are: " + strings.Join(argNames, ", "),
252252
}
253253

254254
default:

internal/core/cobra_utils_test.go

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

33
import (
44
"context"
5-
"fmt"
5+
"errors"
66
"reflect"
77
"testing"
88
"time"
@@ -115,7 +115,7 @@ func Test_handleUnmarshalErrors(t *testing.T) {
115115
Check: core.TestCheckCombine(
116116
core.TestCheckExitCode(1),
117117
core.TestCheckError(&core.CliError{
118-
Err: fmt.Errorf("invalid argument 'name_id': arg name must only contain lowercase letters, numbers or dashes"),
118+
Err: errors.New("invalid argument 'name_id': arg name must only contain lowercase letters, numbers or dashes"),
119119
Hint: "Valid arguments are: name-id",
120120
}),
121121
),
@@ -127,7 +127,7 @@ func Test_handleUnmarshalErrors(t *testing.T) {
127127
Check: core.TestCheckCombine(
128128
core.TestCheckExitCode(1),
129129
core.TestCheckError(&core.CliError{
130-
Err: fmt.Errorf("invalid argument 'ubuntu_focal': arg name must only contain lowercase letters, numbers or dashes"),
130+
Err: errors.New("invalid argument 'ubuntu_focal': arg name must only contain lowercase letters, numbers or dashes"),
131131
Hint: "Valid arguments are: name-id",
132132
}),
133133
),
@@ -143,7 +143,7 @@ func Test_handleUnmarshalErrors(t *testing.T) {
143143
Details: `Absolute time error: parsing time "+3R" as "2006-01-02T15:04:05Z07:00": cannot parse "+3R" as "2006"
144144
Relative time error: unknown unit in duration: "R"
145145
`,
146-
Err: fmt.Errorf("date parsing error: +3R"),
146+
Err: errors.New("date parsing error: +3R"),
147147
Hint: "Run `scw help date` to learn more about date parsing",
148148
}),
149149
),
@@ -177,7 +177,7 @@ func Test_PositionalArg(t *testing.T) {
177177
Check: core.TestCheckCombine(
178178
core.TestCheckExitCode(1),
179179
core.TestCheckError(&core.CliError{
180-
Err: fmt.Errorf("a positional argument is required for this command"),
180+
Err: errors.New("a positional argument is required for this command"),
181181
Hint: "Try running: scw test positional <name-id>",
182182
}),
183183
),
@@ -189,7 +189,7 @@ func Test_PositionalArg(t *testing.T) {
189189
Check: core.TestCheckCombine(
190190
core.TestCheckExitCode(1),
191191
core.TestCheckError(&core.CliError{
192-
Err: fmt.Errorf("a positional argument is required for this command"),
192+
Err: errors.New("a positional argument is required for this command"),
193193
Hint: "Try running: scw test positional <name-id> tag=world",
194194
}),
195195
),
@@ -201,7 +201,7 @@ func Test_PositionalArg(t *testing.T) {
201201
Check: core.TestCheckCombine(
202202
core.TestCheckExitCode(1),
203203
core.TestCheckError(&core.CliError{
204-
Err: fmt.Errorf("a positional argument is required for this command"),
204+
Err: errors.New("a positional argument is required for this command"),
205205
Hint: "Try running: scw test positional plop tag=world",
206206
}),
207207
),
@@ -213,7 +213,7 @@ func Test_PositionalArg(t *testing.T) {
213213
Check: core.TestCheckCombine(
214214
core.TestCheckExitCode(1),
215215
core.TestCheckError(&core.CliError{
216-
Err: fmt.Errorf("a positional argument is required for this command"),
216+
Err: errors.New("a positional argument is required for this command"),
217217
Hint: "Try running: scw test positional plop tag=world",
218218
}),
219219
),
@@ -225,7 +225,7 @@ func Test_PositionalArg(t *testing.T) {
225225
Check: core.TestCheckCombine(
226226
core.TestCheckExitCode(1),
227227
core.TestCheckError(&core.CliError{
228-
Err: fmt.Errorf("a positional argument is required for this command"),
228+
Err: errors.New("a positional argument is required for this command"),
229229
Hint: "Try running: scw test positional plop",
230230
}),
231231
),
@@ -311,7 +311,7 @@ func Test_MultiPositionalArg(t *testing.T) {
311311
Check: core.TestCheckCombine(
312312
core.TestCheckExitCode(1),
313313
core.TestCheckError(&core.CliError{
314-
Err: fmt.Errorf("a positional argument is required for this command"),
314+
Err: errors.New("a positional argument is required for this command"),
315315
Hint: "Try running: scw test multi-positional <name-ids> tag=tag1",
316316
}),
317317
),

internal/core/command.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -162,8 +162,8 @@ func (c *Command) seeAlsosAsStr() string {
162162
seeAlsos := make([]string, 0, len(c.SeeAlsos))
163163

164164
for _, cmdSeeAlso := range c.SeeAlsos {
165-
short := fmt.Sprintf(" # %s", cmdSeeAlso.Short)
166-
commandStr := fmt.Sprintf(" %s", cmdSeeAlso.Command)
165+
short := " # " + cmdSeeAlso.Short
166+
commandStr := " " + cmdSeeAlso.Command
167167

168168
seeAlsoLines := []string{
169169
short,

internal/core/errors.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package core
22

33
import (
4+
"errors"
45
"fmt"
56
)
67

@@ -74,6 +75,6 @@ func ArgumentConflictError(arg1 string, arg2 string) *CliError {
7475

7576
func WindowIsNotSupportedError() *CliError {
7677
return &CliError{
77-
Err: fmt.Errorf("windows is not currently supported"),
78+
Err: errors.New("windows is not currently supported"),
7879
}
7980
}

internal/core/printer.go

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

33
import (
44
"encoding/json"
5+
"errors"
56
"fmt"
67
"io"
78
"reflect"
@@ -103,7 +104,7 @@ func setupTemplatePrinter(printer *Printer, opts string) error {
103104
printer.printerType = PrinterTypeTemplate
104105
if opts == "" {
105106
return &CliError{
106-
Err: fmt.Errorf("cannot use a template output with an empty template"),
107+
Err: errors.New("cannot use a template output with an empty template"),
107108
Hint: `Try using golang template string: scw instance server list -o template="{{ .ID }} ☜(˚▽˚)☞ {{ .Name }}"`,
108109
Details: `https://golang.org/pkg/text/template`,
109110
}
@@ -185,7 +186,7 @@ func (p *Printer) printHuman(data interface{}, opt *human.MarshalOpt) error {
185186
}
186187

187188
if len(p.humanFields) > 0 && reflect.TypeOf(data).Kind() != reflect.Slice {
188-
return p.printHuman(fmt.Errorf("list of fields for human output is only supported for commands that return a list"), nil)
189+
return p.printHuman(errors.New("list of fields for human output is only supported for commands that return a list"), nil)
189190
}
190191

191192
if len(p.humanFields) > 0 {
@@ -203,7 +204,7 @@ func (p *Printer) printHuman(data interface{}, opt *human.MarshalOpt) error {
203204
case *human.UnknownFieldError:
204205
return p.printHuman(&CliError{
205206
Err: fmt.Errorf("unknown field '%s' in output options", e.FieldName),
206-
Hint: fmt.Sprintf("Valid fields are: %s", strings.Join(e.ValidFields, ", ")),
207+
Hint: "Valid fields are: " + strings.Join(e.ValidFields, ", "),
207208
}, nil)
208209
case nil:
209210
// Do nothing
@@ -299,7 +300,7 @@ func (p *Printer) printTemplate(data interface{}) error {
299300
return p.printHuman(&CliError{
300301
Err: err,
301302
Message: "templating error",
302-
Hint: fmt.Sprintf("Acceptable values are:\n - %s", strings.Join(gofields.ListFields(elemValue.Type()), "\n - ")),
303+
Hint: "Acceptable values are:\n - " + strings.Join(gofields.ListFields(elemValue.Type()), "\n - "),
303304
}, nil)
304305
}
305306
_, err = writer.Write([]byte{'\n'})

0 commit comments

Comments
 (0)