Skip to content

Commit

Permalink
ci: enable perfsprint linter (#2872)
Browse files Browse the repository at this point in the history
Enable perfsprint linter for golangci-lint

Signed-off-by: Matthieu MOREL <[email protected]>
Co-authored-by: Steven Hartland <[email protected]>
  • Loading branch information
mmorel-35 and stevenh authored Nov 2, 2024
1 parent 1819e70 commit cc5a516
Show file tree
Hide file tree
Showing 47 changed files with 116 additions and 96 deletions.
1 change: 1 addition & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ linters:
- misspell
- nolintlint
- nakedret
- perfsprint
- testifylint
- thelper
- usestdlibvars
Expand Down
2 changes: 1 addition & 1 deletion docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ func (c *DockerContainer) PortEndpoint(ctx context.Context, port nat.Port, proto

protoFull := ""
if proto != "" {
protoFull = fmt.Sprintf("%s://", proto)
protoFull = proto + "://"
}

return fmt.Sprintf("%s%s:%s", protoFull, host, outerPort.Port()), nil
Expand Down
4 changes: 2 additions & 2 deletions docker_auth_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -284,11 +284,11 @@ func prepareLocalRegistryWithAuth(t *testing.T) string {
},
Files: []ContainerFile{
{
HostFilePath: fmt.Sprintf("%s/testdata/auth", wd),
HostFilePath: wd + "/testdata/auth",
ContainerFilePath: "/auth",
},
{
HostFilePath: fmt.Sprintf("%s/testdata/data", wd),
HostFilePath: wd + "/testdata/data",
ContainerFilePath: "/data",
},
},
Expand Down
3 changes: 2 additions & 1 deletion file_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"archive/tar"
"bytes"
"compress/gzip"
"errors"
"fmt"
"io"
"os"
Expand Down Expand Up @@ -37,7 +38,7 @@ func Test_IsDir(t *testing.T) {
{
filepath: "foobar.doc",
expected: false,
err: fmt.Errorf("does not exist"),
err: errors.New("does not exist"),
},
}

Expand Down
3 changes: 2 additions & 1 deletion internal/core/bootstrap.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package core

import (
"crypto/sha256"
"encoding/hex"
"fmt"
"os"

Expand Down Expand Up @@ -89,7 +90,7 @@ func init() {
return
}

sessionID = fmt.Sprintf("%x", hasher.Sum(nil))
sessionID = hex.EncodeToString(hasher.Sum(nil))
}

func ProcessID() string {
Expand Down
4 changes: 2 additions & 2 deletions internal/core/docker_host_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package core

import (
"context"
"fmt"
"errors"
"os"
"path/filepath"
"testing"
Expand Down Expand Up @@ -46,7 +46,7 @@ func testCallbackCheckPassing(_ context.Context, _ string) error {
}

func testCallbackCheckError(_ context.Context, _ string) error {
return fmt.Errorf("could not check the Docker host")
return errors.New("could not check the Docker host")
}

func mockCallbackCheck(t *testing.T, fn func(_ context.Context, _ string) error) {
Expand Down
4 changes: 2 additions & 2 deletions internal/core/docker_rootless.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ package core
import (
"context"
"errors"
"fmt"
"net/url"
"os"
"path/filepath"
"runtime"
"strconv"
)

var (
Expand Down Expand Up @@ -144,7 +144,7 @@ func rootlessSocketPathFromHomeDesktopDir() (string, error) {
// rootlessSocketPathFromRunDir returns the path to the rootless Docker socket from the /run/user/<uid>/docker.sock file.
func rootlessSocketPathFromRunDir() (string, error) {
uid := os.Getuid()
f := filepath.Join(baseRunDir, "user", fmt.Sprintf("%d", uid), "docker.sock")
f := filepath.Join(baseRunDir, "user", strconv.Itoa(uid), "docker.sock")
if fileExists(f) {
return f, nil
}
Expand Down
6 changes: 3 additions & 3 deletions internal/core/docker_rootless_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ package core

import (
"context"
"fmt"
"os"
"path/filepath"
"strconv"
"testing"

"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -161,7 +161,7 @@ func TestRootlessDockerSocketPath(t *testing.T) {
})

uid := os.Getuid()
runDir := filepath.Join(tmpDir, "user", fmt.Sprintf("%d", uid))
runDir := filepath.Join(tmpDir, "user", strconv.Itoa(uid))
err = createTmpDockerSocket(runDir)
require.NoError(t, err)

Expand Down Expand Up @@ -208,7 +208,7 @@ func setupRootlessNotFound(t *testing.T) {

baseRunDir = tmpDir
uid := os.Getuid()
runDir := filepath.Join(tmpDir, "run", "user", fmt.Sprintf("%d", uid))
runDir := filepath.Join(tmpDir, "run", "user", strconv.Itoa(uid))
err = createTmpDir(runDir)
require.NoError(t, err)
}
3 changes: 2 additions & 1 deletion modules/chroma/chroma.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package chroma

import (
"context"
"errors"
"fmt"

"github.com/testcontainers/testcontainers-go"
Expand Down Expand Up @@ -66,7 +67,7 @@ func (c *ChromaContainer) RESTEndpoint(ctx context.Context) (string, error) {

host, err := c.Host(ctx)
if err != nil {
return "", fmt.Errorf("failed to get container host")
return "", errors.New("failed to get container host")
}

return fmt.Sprintf("http://%s:%s", host, containerPort.Port()), nil
Expand Down
8 changes: 4 additions & 4 deletions modules/cockroachdb/certs.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package cockroachdb

import (
"crypto/x509"
"fmt"
"errors"
"net"
"time"

Expand All @@ -28,7 +28,7 @@ func NewTLSConfig() (*TLSConfig, error) {
ValidFor: time.Hour,
})
if caCert == nil {
return nil, fmt.Errorf("failed to generate CA certificate")
return nil, errors.New("failed to generate CA certificate")
}
// }

Expand All @@ -42,7 +42,7 @@ func NewTLSConfig() (*TLSConfig, error) {
Parent: caCert, // using the CA certificate as parent
})
if nodeCert == nil {
return nil, fmt.Errorf("failed to generate node certificate")
return nil, errors.New("failed to generate node certificate")
}
// }

Expand All @@ -54,7 +54,7 @@ func NewTLSConfig() (*TLSConfig, error) {
Parent: caCert, // using the CA certificate as parent
})
if clientCert == nil {
return nil, fmt.Errorf("failed to generate client certificate")
return nil, errors.New("failed to generate client certificate")
}

return &TLSConfig{
Expand Down
5 changes: 3 additions & 2 deletions modules/cockroachdb/cockroachdb.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"crypto/tls"
"crypto/x509"
"encoding/pem"
"errors"
"fmt"
"net"
"net/url"
Expand All @@ -18,7 +19,7 @@ import (
"github.com/testcontainers/testcontainers-go/wait"
)

var ErrTLSNotEnabled = fmt.Errorf("tls not enabled")
var ErrTLSNotEnabled = errors.New("tls not enabled")

const (
certsDir = "/tmp"
Expand Down Expand Up @@ -144,7 +145,7 @@ func addCmd(req *testcontainers.GenericContainerRequest, opts options) error {
return fmt.Errorf("unsupported user %s with TLS, use %s", opts.User, defaultUser)
}
if opts.Password != "" {
return fmt.Errorf("cannot use password authentication with TLS")
return errors.New("cannot use password authentication with TLS")
}
}

Expand Down
6 changes: 3 additions & 3 deletions modules/compose/compose_api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package compose

import (
"context"
"fmt"
"encoding/hex"
"hash/fnv"
"os"
"path/filepath"
Expand Down Expand Up @@ -642,7 +642,7 @@ func TestDockerComposeAPIVolumesDeletedOnDown(t *testing.T) {

volumeListFilters := filters.NewArgs()
// the "mydata" identifier comes from the "testdata/docker-compose-volume.yml" file
volumeListFilters.Add("name", fmt.Sprintf("%s_mydata", identifier))
volumeListFilters.Add("name", identifier+"_mydata")
volumeList, err := compose.dockerClient.VolumeList(ctx, volume.ListOptions{Filters: volumeListFilters})
require.NoError(t, err, "compose.dockerClient.VolumeList()")

Expand Down Expand Up @@ -690,7 +690,7 @@ func TestDockerComposeApiWithWaitForShortLifespanService(t *testing.T) {
}

func testNameHash(name string) StackIdentifier {
return StackIdentifier(fmt.Sprintf("%x", fnv.New32a().Sum([]byte(name))))
return StackIdentifier(hex.EncodeToString(fnv.New32a().Sum([]byte(name))))
}

// cleanup is a helper function that schedules the compose stack to be stopped when the test ends.
Expand Down
3 changes: 2 additions & 1 deletion modules/dolt/dolt.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package dolt
import (
"context"
"database/sql"
"errors"
"fmt"
"path/filepath"
"strings"
Expand Down Expand Up @@ -84,7 +85,7 @@ func Run(ctx context.Context, img string, opts ...testcontainers.ContainerCustom
}

if len(password) == 0 && password == "" && !strings.EqualFold(rootUser, username) {
return nil, fmt.Errorf("empty password can be used only with the root user")
return nil, errors.New("empty password can be used only with the root user")
}

container, err := testcontainers.GenericContainer(ctx, genericContainerReq)
Expand Down
3 changes: 2 additions & 1 deletion modules/elasticsearch/elasticsearch.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"crypto/tls"
"crypto/x509"
"errors"
"fmt"
"io"
"os"
Expand Down Expand Up @@ -212,7 +213,7 @@ func configurePassword(settings *Options, req *testcontainers.GenericContainerRe

if settings.Password != "" {
if isOSS(req.Image) {
return fmt.Errorf("it's not possible to activate security on Elastic OSS Image. Please switch to the default distribution.")
return errors.New("it's not possible to activate security on Elastic OSS Image. Please switch to the default distribution.")
}

if _, ok := req.Env["ELASTIC_PASSWORD"]; !ok {
Expand Down
2 changes: 1 addition & 1 deletion modules/elasticsearch/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ func isAtLeastVersion(image string, major int) bool {
}

if !strings.HasPrefix(version, "v") {
version = fmt.Sprintf("v%s", version)
version = "v" + version
}

if semver.IsValid(version) {
Expand Down
2 changes: 1 addition & 1 deletion modules/gcloud/spanner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ func ExampleRunSpannerContainer() {
// }

instanceOp, err := instanceAdmin.CreateInstance(ctx, &instancepb.CreateInstanceRequest{
Parent: fmt.Sprintf("projects/%s", projectId),
Parent: "projects/" + projectId,
InstanceId: instanceId,
Instance: &instancepb.Instance{
DisplayName: instanceId,
Expand Down
2 changes: 1 addition & 1 deletion modules/inbucket/inbucket.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func (c *InbucketContainer) WebInterface(ctx context.Context) (string, error) {
return "", err
}

return fmt.Sprintf("http://%s", net.JoinHostPort(host, containerPort.Port())), nil
return "http://" + net.JoinHostPort(host, containerPort.Port()), nil
}

// Deprecated: use Run instead
Expand Down
2 changes: 1 addition & 1 deletion modules/k3s/k3s.go
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ func (c *K3sContainer) LoadImages(ctx context.Context, images ...string) error {
return fmt.Errorf("saving images %w", err)
}

containerPath := fmt.Sprintf("/tmp/%s", filepath.Base(imagesTar.Name()))
containerPath := "/tmp/" + filepath.Base(imagesTar.Name())
err = c.Container.CopyFileToContainer(ctx, imagesTar.Name(), containerPath, 0x644)
if err != nil {
return fmt.Errorf("copying image to container %w", err)
Expand Down
2 changes: 1 addition & 1 deletion modules/k6/k6.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ func WithCache() testcontainers.CustomizeRequestOption {
cacheVol := os.Getenv("TC_K6_BUILD_CACHE")
// if no volume is provided, create one and ensure add labels for garbage collection
if cacheVol == "" {
cacheVol = fmt.Sprintf("k6-cache-%s", testcontainers.SessionID())
cacheVol = "k6-cache-" + testcontainers.SessionID()
volOptions = &mount.VolumeOptions{
Labels: testcontainers.GenericLabels(),
}
Expand Down
8 changes: 5 additions & 3 deletions modules/kafka/kafka.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ package kafka

import (
"context"
"errors"
"fmt"
"math"
"strconv"
"strings"

"github.com/docker/go-connections/nat"
Expand Down Expand Up @@ -58,7 +60,7 @@ func Run(ctx context.Context, img string, opts ...testcontainers.ContainerCustom
"KAFKA_OFFSETS_TOPIC_NUM_PARTITIONS": "1",
"KAFKA_TRANSACTION_STATE_LOG_REPLICATION_FACTOR": "1",
"KAFKA_TRANSACTION_STATE_LOG_MIN_ISR": "1",
"KAFKA_LOG_FLUSH_INTERVAL_MESSAGES": fmt.Sprintf("%d", math.MaxInt),
"KAFKA_LOG_FLUSH_INTERVAL_MESSAGES": strconv.Itoa(math.MaxInt),
"KAFKA_GROUP_INITIAL_REBALANCE_DELAY_MS": "0",
"KAFKA_NODE_ID": "1",
"KAFKA_PROCESS_ROLES": "broker,controller",
Expand Down Expand Up @@ -203,7 +205,7 @@ func configureControllerQuorumVoters(req *testcontainers.GenericContainerRequest
// which is available since version 7.0.0.
func validateKRaftVersion(fqName string) error {
if fqName == "" {
return fmt.Errorf("image cannot be empty")
return errors.New("image cannot be empty")
}

image := fqName[:strings.LastIndex(fqName, ":")]
Expand All @@ -218,7 +220,7 @@ func validateKRaftVersion(fqName string) error {

// semver requires the version to start with a "v"
if !strings.HasPrefix(version, "v") {
version = fmt.Sprintf("v%s", version)
version = "v" + version
}

if semver.Compare(version, "v7.4.0") < 0 { // version < v7.4.0
Expand Down
6 changes: 3 additions & 3 deletions modules/localstack/localstack.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func isLegacyMode(image string) bool {
}

if !strings.HasPrefix(version, "v") {
version = fmt.Sprintf("v%s", version)
version = "v" + version
}

if semver.IsValid(version) {
Expand All @@ -48,7 +48,7 @@ func isVersion2(image string) bool {
}

if !strings.HasPrefix(version, "v") {
version = fmt.Sprintf("v%s", version)
version = "v" + version
}

if semver.IsValid(version) {
Expand Down Expand Up @@ -82,7 +82,7 @@ func Run(ctx context.Context, img string, opts ...testcontainers.ContainerCustom
ExposedPorts: []string{fmt.Sprintf("%d/tcp", defaultPort)},
Env: map[string]string{},
HostConfigModifier: func(hostConfig *container.HostConfig) {
hostConfig.Binds = []string{fmt.Sprintf("%s:/var/run/docker.sock", dockerHost)}
hostConfig.Binds = []string{dockerHost + ":/var/run/docker.sock"}
},
}

Expand Down
Loading

0 comments on commit cc5a516

Please sign in to comment.