Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable and fix perfsprint linter #1415

Merged
merged 1 commit into from
May 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ linters:
- nakedret
- nilerr
- nosprintfhostport
- perfsprint
- prealloc
- predeclared
- promlinter
Expand Down Expand Up @@ -94,7 +95,6 @@ linters:
# - nolintlint
# - nonamedreturns
# - paralleltest
# - perfsprint
# - revive
# - rowserrcheck
# - sqlclosecheck
Expand Down
5 changes: 3 additions & 2 deletions cmd/crictl/attach.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package main

import (
"context"
"errors"
"fmt"
"net/url"

Expand Down Expand Up @@ -53,7 +54,7 @@ var runtimeAttachCommand = &cli.Command{
Action: func(c *cli.Context) error {
id := c.Args().First()
if id == "" {
return fmt.Errorf("ID cannot be empty")
return errors.New("ID cannot be empty")
}

if c.NArg() != 1 {
Expand Down Expand Up @@ -84,7 +85,7 @@ var runtimeAttachCommand = &cli.Command{
// Attach sends an AttachRequest to server, and parses the returned AttachResponse
func Attach(ctx context.Context, client internalapi.RuntimeService, opts attachOptions) error {
if opts.id == "" {
return fmt.Errorf("ID cannot be empty")
return errors.New("ID cannot be empty")

}
request := &pb.AttachRequest{
Expand Down
3 changes: 2 additions & 1 deletion cmd/crictl/completion.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ limitations under the License.
package main

import (
"errors"
"fmt"
"strings"

Expand Down Expand Up @@ -136,7 +137,7 @@ Examples:
case "zsh":
return zshCompletion(c)
default:
return fmt.Errorf("only bash, zsh or fish are supported")
return errors.New("only bash, zsh or fish are supported")
}
},
}
27 changes: 14 additions & 13 deletions cmd/crictl/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
"path/filepath"
goruntime "runtime"
"sort"
"strconv"
"strings"
"time"

Expand Down Expand Up @@ -201,7 +202,7 @@ var startContainerCommand = &cli.Command{
ArgsUsage: "CONTAINER-ID [CONTAINER-ID...]",
Action: func(c *cli.Context) error {
if c.NArg() == 0 {
return fmt.Errorf("ID cannot be empty")
return errors.New("ID cannot be empty")
}
runtimeClient, err := getRuntimeService(c, 0)
if err != nil {
Expand Down Expand Up @@ -258,7 +259,7 @@ var updateContainerCommand = &cli.Command{
},
Action: func(c *cli.Context) error {
if c.NArg() == 0 {
return fmt.Errorf("ID cannot be empty")
return errors.New("ID cannot be empty")
}
runtimeClient, err := getRuntimeService(c, 0)
if err != nil {
Expand Down Expand Up @@ -300,7 +301,7 @@ var stopContainerCommand = &cli.Command{
},
Action: func(c *cli.Context) error {
if c.NArg() == 0 {
return fmt.Errorf("ID cannot be empty")
return errors.New("ID cannot be empty")
}
runtimeClient, err := getRuntimeService(c, 0)
if err != nil {
Expand Down Expand Up @@ -412,7 +413,7 @@ var removeContainerCommand = &cli.Command{
}

if errored {
return fmt.Errorf("unable to remove container(s)")
return errors.New("unable to remove container(s)")
}
return nil
},
Expand Down Expand Up @@ -440,7 +441,7 @@ var containerStatusCommand = &cli.Command{
},
Action: func(c *cli.Context) error {
if c.NArg() == 0 {
return fmt.Errorf("ID cannot be empty")
return errors.New("ID cannot be empty")
}
runtimeClient, err := getRuntimeService(c, 0)
if err != nil {
Expand Down Expand Up @@ -646,10 +647,10 @@ var checkpointContainerCommand = &cli.Command{
},
Action: func(c *cli.Context) error {
if c.NArg() == 0 {
return fmt.Errorf("ID cannot be empty")
return errors.New("ID cannot be empty")
}
if c.String("export") == "" {
return fmt.Errorf(
return errors.New(
"Cannot checkpoint a container without specifying the checkpoint destination. " +
"Use --export=/path/to/checkpoint.tar",
)
Expand Down Expand Up @@ -769,7 +770,7 @@ func CreateContainer(
// the returned StartContainerResponse.
func StartContainer(client internalapi.RuntimeService, id string) error {
if id == "" {
return fmt.Errorf("ID cannot be empty")
return errors.New("ID cannot be empty")
}
if err := client.StartContainer(context.TODO(), id); err != nil {
return err
Expand Down Expand Up @@ -803,7 +804,7 @@ type updateOptions struct {
// the returned UpdateContainerResourcesResponse.
func UpdateContainerResources(client internalapi.RuntimeService, id string, opts updateOptions) error {
if id == "" {
return fmt.Errorf("ID cannot be empty")
return errors.New("ID cannot be empty")
}
request := &pb.UpdateContainerResourcesRequest{
ContainerId: id,
Expand Down Expand Up @@ -839,7 +840,7 @@ func UpdateContainerResources(client internalapi.RuntimeService, id string, opts
// the returned StopContainerResponse.
func StopContainer(client internalapi.RuntimeService, id string, timeout int64) error {
if id == "" {
return fmt.Errorf("ID cannot be empty")
return errors.New("ID cannot be empty")
}
if err := client.StopContainer(context.TODO(), id, timeout); err != nil {
return err
Expand Down Expand Up @@ -874,7 +875,7 @@ func CheckpointContainer(
// the returned RemoveContainerResponse.
func RemoveContainer(client internalapi.RuntimeService, id string) error {
if id == "" {
return fmt.Errorf("ID cannot be empty")
return errors.New("ID cannot be empty")
}
if err := client.RemoveContainer(context.TODO(), id); err != nil {
return err
Expand Down Expand Up @@ -922,7 +923,7 @@ func ContainerStatus(client internalapi.RuntimeService, id, output string, tmplS
output = "json"
}
if id == "" {
return fmt.Errorf("ID cannot be empty")
return errors.New("ID cannot be empty")
}
request := &pb.ContainerStatusRequest{
ContainerId: id,
Expand Down Expand Up @@ -1088,7 +1089,7 @@ func ListContainers(runtimeClient internalapi.RuntimeService, imageClient intern
}
podName := getPodNameFromLabels(c.Labels)
display.AddRow([]string{id, image, ctm, convertContainerState(c.State), c.Metadata.Name,
fmt.Sprintf("%d", c.Metadata.Attempt), podID, podName})
strconv.FormatUint(uint64(c.Metadata.Attempt), 10), podID, podName})
continue
}

Expand Down
6 changes: 4 additions & 2 deletions cmd/crictl/container_stats.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,10 @@ package main

import (
"context"
"errors"
"fmt"
"sort"
"strconv"
"time"

"github.com/docker/go-units"
Expand Down Expand Up @@ -207,12 +209,12 @@ func (d containerStatsDisplayer) displayStats(ctx context.Context, client intern
// Only generate cpuPerc for running container
duration := s.GetCpu().GetTimestamp() - old.GetCpu().GetTimestamp()
if duration == 0 {
return fmt.Errorf("cpu stat is not updated during sample")
return errors.New("cpu stat is not updated during sample")
}
cpuPerc = float64(cpu-old.GetCpu().GetUsageCoreNanoSeconds().GetValue()) / float64(duration) * 100
}
d.display.AddRow([]string{id, name, fmt.Sprintf("%.2f", cpuPerc), units.HumanSize(float64(mem)),
units.HumanSize(float64(disk)), fmt.Sprintf("%d", inodes)})
units.HumanSize(float64(disk)), strconv.FormatUint(inodes, 10)})

}
d.display.ClearScreen()
Expand Down
4 changes: 2 additions & 2 deletions cmd/crictl/exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -202,15 +202,15 @@ func stream(ctx context.Context, in, tty bool, transport string, url *url.URL) e
streamOptions.Stdin = pr
}
if !in {
return fmt.Errorf("tty=true must be specified with interactive=true")
return errors.New("tty=true must be specified with interactive=true")
}
t := term.TTY{
In: stdin,
Out: stdout,
Raw: true,
}
if !t.IsTerminalIn() {
return fmt.Errorf("input is not a terminal")
return errors.New("input is not a terminal")
}
streamOptions.TerminalSizeQueue = t.MonitorSize(t.GetSize())
return t.Safe(func() error { return executor.StreamWithContext(ctx, streamOptions) })
Expand Down
9 changes: 5 additions & 4 deletions cmd/crictl/image.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"regexp"
"slices"
"sort"
"strconv"
"strings"
"syscall"

Expand Down Expand Up @@ -87,7 +88,7 @@ var pullImageCommand = &cli.Command{
Action: func(c *cli.Context) error {
imageName := c.Args().First()
if imageName == "" {
return fmt.Errorf("Image name cannot be empty")
return errors.New("Image name cannot be empty")
}

if c.NArg() > 1 {
Expand Down Expand Up @@ -237,7 +238,7 @@ var listImageCommand = &cli.Command{
}
row = append(row, id, size)
if showPinned {
row = append(row, fmt.Sprintf("%v", image.Pinned))
row = append(row, strconv.FormatBool(image.Pinned))
}
display.AddRow(row)
}
Expand Down Expand Up @@ -476,7 +477,7 @@ var removeImageCommand = &cli.Command{
}

if errored {
return fmt.Errorf("unable to remove the image(s)")
return errors.New("unable to remove the image(s)")
}

return nil
Expand Down Expand Up @@ -779,7 +780,7 @@ func ImageStatus(client internalapi.ImageManagerService, image string, verbose b
// the returned RemoveImageResponse.
func RemoveImage(client internalapi.ImageManagerService, image string) error {
if image == "" {
return fmt.Errorf("ImageID cannot be empty")
return errors.New("ImageID cannot be empty")
}
request := &pb.RemoveImageRequest{Image: &pb.ImageSpec{Image: image}}
logrus.Debugf("RemoveImageRequest: %v", request)
Expand Down
8 changes: 5 additions & 3 deletions cmd/crictl/logs.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,10 @@ package main

import (
"context"
"errors"
"fmt"
"os"
"strconv"
"strings"
"time"

Expand Down Expand Up @@ -70,7 +72,7 @@ var logsCommand = &cli.Command{
Action: func(c *cli.Context) (retErr error) {
containerID := c.Args().First()
if containerID == "" {
return fmt.Errorf("ID cannot be empty")
return errors.New("ID cannot be empty")
}

if c.NArg() > 1 {
Expand Down Expand Up @@ -103,14 +105,14 @@ var logsCommand = &cli.Command{
}
logPath := status.GetStatus().GetLogPath()
if logPath == "" {
return fmt.Errorf("The container has not set log path")
return errors.New("The container has not set log path")
}
if previous {
containerAttempt := status.GetStatus().GetMetadata().Attempt
if containerAttempt == uint32(0) {
return fmt.Errorf("Previous terminated container %s not found", status.GetStatus().GetMetadata().Name)
}
logPath = fmt.Sprintf("%s%s%s", logPath[:strings.LastIndex(logPath, "/")+1], fmt.Sprint(containerAttempt-1),
logPath = fmt.Sprintf("%s%s%s", logPath[:strings.LastIndex(logPath, "/")+1], strconv.FormatUint(uint64(containerAttempt-1), 10),
logPath[strings.LastIndex(logPath, "."):])
}
// build a WithCancel context based on cli.context
Expand Down
5 changes: 3 additions & 2 deletions cmd/crictl/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package main

import (
"context"
"errors"
"fmt"
"os"
"runtime"
Expand Down Expand Up @@ -64,7 +65,7 @@ var (

func getRuntimeService(_ *cli.Context, timeout time.Duration) (res internalapi.RuntimeService, err error) {
if RuntimeEndpointIsSet && RuntimeEndpoint == "" {
return nil, fmt.Errorf("--runtime-endpoint is not set")
return nil, errors.New("--runtime-endpoint is not set")
}
logrus.Debug("get runtime connection")

Expand Down Expand Up @@ -110,7 +111,7 @@ func getRuntimeService(_ *cli.Context, timeout time.Duration) (res internalapi.R
func getImageService(*cli.Context) (res internalapi.ImageManagerService, err error) {
if ImageEndpoint == "" {
if RuntimeEndpointIsSet && RuntimeEndpoint == "" {
return nil, fmt.Errorf("--image-endpoint is not set")
return nil, errors.New("--image-endpoint is not set")
}
ImageEndpoint = RuntimeEndpoint
ImageEndpointIsSet = RuntimeEndpointIsSet
Expand Down
3 changes: 2 additions & 1 deletion cmd/crictl/portforward.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package main

import (
"context"
"errors"
"fmt"
"net/http"
"net/url"
Expand Down Expand Up @@ -70,7 +71,7 @@ var runtimePortForwardCommand = &cli.Command{
// PortForward sends an PortForwardRequest to server, and parses the returned PortForwardResponse
func PortForward(client internalapi.RuntimeService, opts portforwardOptions) error {
if opts.id == "" {
return fmt.Errorf("ID cannot be empty")
return errors.New("ID cannot be empty")

}
request := &pb.PortForwardRequest{
Expand Down
10 changes: 6 additions & 4 deletions cmd/crictl/sandbox.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,11 @@ package main
import (
"context"
"encoding/json"
"errors"
"fmt"
"log"
"sort"
"strconv"
"strings"
"time"

Expand Down Expand Up @@ -337,7 +339,7 @@ func RunPodSandbox(client internalapi.RuntimeService, config *pb.PodSandboxConfi
// the returned StopPodSandboxResponse.
func StopPodSandbox(client internalapi.RuntimeService, id string) error {
if id == "" {
return fmt.Errorf("ID cannot be empty")
return errors.New("ID cannot be empty")
}
if err := client.StopPodSandbox(context.TODO(), id); err != nil {
return err
Expand All @@ -351,7 +353,7 @@ func StopPodSandbox(client internalapi.RuntimeService, id string) error {
// the returned RemovePodSandboxResponse.
func RemovePodSandbox(client internalapi.RuntimeService, id string) error {
if id == "" {
return fmt.Errorf("ID cannot be empty")
return errors.New("ID cannot be empty")
}
if err := client.RemovePodSandbox(context.TODO(), id); err != nil {
return err
Expand Down Expand Up @@ -384,7 +386,7 @@ func PodSandboxStatus(client internalapi.RuntimeService, id, output string, quie
output = "json"
}
if id == "" {
return fmt.Errorf("ID cannot be empty")
return errors.New("ID cannot be empty")
}

request := &pb.PodSandboxStatusRequest{
Expand Down Expand Up @@ -530,7 +532,7 @@ func ListPodSandboxes(client internalapi.RuntimeService, opts listOptions) error
convertPodState(pod.State),
pod.Metadata.Name,
pod.Metadata.Namespace,
fmt.Sprintf("%d", pod.Metadata.Attempt),
strconv.FormatUint(uint64(pod.Metadata.Attempt), 10),
getSandboxesRuntimeHandler(pod),
})
continue
Expand Down
Loading
Loading