Skip to content

Commit

Permalink
chore(lint): fix linter issues
Browse files Browse the repository at this point in the history
- Adapted error comparison according to linter recommendation
- Disabled noctx linting for http request where canceling makes no sense
- Disabled nilerror linting where nil error is returned on purpose
- Disabled makezero linter where slice is explicitly deepcopied
  • Loading branch information
BronzeDeer committed Mar 1, 2023
1 parent 1a9cd4c commit 92ce78c
Show file tree
Hide file tree
Showing 15 changed files with 44 additions and 51 deletions.
12 changes: 6 additions & 6 deletions integration/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -714,18 +714,18 @@ func TestExitCodePropagation(t *testing.T) {
}

type fileDiff struct {
Name string
Size int
Name string `json:"Name"`
Size int `json:"Size"`
}

type fileDiffResult struct {
Adds []fileDiff
Dels []fileDiff
Adds []fileDiff `json:"Adds"`
Dels []fileDiff `json:"Dels"`
}

type metaDiffResult struct {
Adds []string
Dels []string
Adds []string `json:"Adds"`
Dels []string `json:"Dels"`
}

type diffOutput struct {
Expand Down
2 changes: 1 addition & 1 deletion pkg/buildcontext/https.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ func (h *HTTPSTar) UnpackTarFromBuildContext() (directory string, err error) {

// Download tar file from remote https server
// and save it into the target tar file
resp, err := http.Get(h.context)
resp, err := http.Get(h.context) //nolint:noctx
if err != nil {
return
}
Expand Down
30 changes: 10 additions & 20 deletions pkg/cache/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,13 @@ limitations under the License.

package cache

import "errors"

// IsAlreadyCached returns true if the supplied error is of the type AlreadyCachedErr
// otherwise it returns false.
func IsAlreadyCached(err error) bool {
switch err.(type) {
case AlreadyCachedErr:
return true
}

return false
var e AlreadyCachedErr
return errors.As(err, &e)
}

// AlreadyCachedErr is returned when the Docker image requested for caching is already
Expand All @@ -39,13 +37,9 @@ func (a AlreadyCachedErr) Error() string {

// IsNotFound returns true if the supplied error is of the type NotFoundErr
// otherwise it returns false.
func IsNotFound(e error) bool {
switch e.(type) {
case NotFoundErr:
return true
}

return false
func IsNotFound(err error) bool {
var e NotFoundErr
return errors.As(err, &e)
}

// NotFoundErr is returned when the requested Docker image is not present in the cache.
Expand All @@ -59,13 +53,9 @@ func (e NotFoundErr) Error() string {

// IsExpired returns true if the supplied error is of the type ExpiredErr
// otherwise it returns false.
func IsExpired(e error) bool {
switch e.(type) {
case ExpiredErr:
return true
}

return false
func IsExpired(err error) bool {
var e ExpiredErr
return errors.As(err, &e)
}

// ExpiredErr is returned when the requested Docker image is present in the cache, but is
Expand Down
2 changes: 1 addition & 1 deletion pkg/commands/copy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -375,7 +375,7 @@ func Test_resolveIfSymlink(t *testing.T) {
for i, c := range cases {
t.Run(fmt.Sprintf("%d", i), func(t *testing.T) {
res, e := resolveIfSymlink(c.destPath)
if e != c.err {
if !errors.Is(e, c.err) {
t.Errorf("%s: expected %v but got %v", c.destPath, c.err, e)
}

Expand Down
2 changes: 1 addition & 1 deletion pkg/commands/volume.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ func (v *VolumeCommand) ExecuteCommand(config *v1.Config, buildArgs *dockerfile.
if _, err := os.Stat(volume); os.IsNotExist(err) {
logrus.Infof("Creating directory %s", volume)
if err := os.MkdirAll(volume, 0755); err != nil {
return fmt.Errorf("could not create directory for volume %s: %s", volume, err)
return fmt.Errorf("could not create directory for volume %s: %w", volume, err)
}
}
}
Expand Down
3 changes: 2 additions & 1 deletion pkg/dockerfile/buildargs.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,8 @@ func (b *BuildArgs) ReplacementEnvs(envs []string) []string {
resultEnv := make([]string, len(envs))
copy(resultEnv, envs)
filtered := b.FilterAllowed(envs)
return append(resultEnv, filtered...)
// Disable makezero linter, since the previous make is paired with a same sized copy
return append(resultEnv, filtered...) //nolint:makezero
}

// AddMetaArgs adds the supplied args map to b's allowedMetaArgs
Expand Down
2 changes: 1 addition & 1 deletion pkg/dockerfile/dockerfile.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func ParseStages(opts *config.KanikoOptions) ([]instructions.Stage, []instructio
var d []uint8
match, _ := regexp.MatchString("^https?://", opts.DockerfilePath)
if match {
response, e := http.Get(opts.DockerfilePath)
response, e := http.Get(opts.DockerfilePath) //nolint:noctx
if e != nil {
return nil, nil, e
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/executor/push_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ func TestHeaderAdded(t *testing.T) {
os.Setenv("UPSTREAM_CLIENT_TYPE", test.upstream)
defer func() { os.Unsetenv("UPSTREAM_CLIENT_TYPE") }()
}
req, err := http.NewRequest("GET", "dummy", nil)
req, err := http.NewRequest("GET", "dummy", nil) //nolint:noctx
if err != nil {
t.Fatalf("culd not create a req due to %s", err)
}
Expand Down
5 changes: 3 additions & 2 deletions pkg/snapshot/snapshot.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ limitations under the License.
package snapshot

import (
"errors"
"fmt"
"io/ioutil"
"os"
Expand Down Expand Up @@ -262,7 +263,7 @@ func addParentDirectories(t util.Tar, addedPaths map[string]bool, path string) e
// filesWithLinks returns the symlink and the target path if its exists.
func filesWithLinks(path string) ([]string, error) {
link, err := util.GetSymLink(path)
if err == util.ErrNotSymLink {
if errors.Is(err, util.ErrNotSymLink) {
return []string{path}, nil
} else if err != nil {
return nil, err
Expand All @@ -272,7 +273,7 @@ func filesWithLinks(path string) ([]string, error) {
link = filepath.Join(filepath.Dir(path), link)
}
if _, err := os.Stat(link); err != nil {
return []string{path}, nil
return []string{path}, nil //nolint:nilerr
}
return []string{path, link}, nil
}
10 changes: 5 additions & 5 deletions pkg/snapshot/snapshot_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ func TestSnapshotFSFileChange(t *testing.T) {
actualFiles := []string{}
for {
hdr, err := tr.Next()
if err == io.EOF {
if errors.Is(err, io.EOF) {
break
}

Expand Down Expand Up @@ -166,7 +166,7 @@ func TestSnapshotFSChangePermissions(t *testing.T) {
foundFiles := []string{}
for {
hdr, err := tr.Next()
if err == io.EOF {
if errors.Is(err, io.EOF) {
break
}
foundFiles = append(foundFiles, hdr.Name)
Expand Down Expand Up @@ -249,7 +249,7 @@ func TestEmptySnapshotFS(t *testing.T) {
}
tr := tar.NewReader(f)

if _, err := tr.Next(); err != io.EOF {
if _, err := tr.Next(); !errors.Is(err, io.EOF) {
t.Fatal("no files expected in tar, found files.")
}
}
Expand Down Expand Up @@ -560,7 +560,7 @@ func TestSnapshotOmitsUnameGname(t *testing.T) {
tr := tar.NewReader(f)
for {
hdr, err := tr.Next()
if err == io.EOF {
if errors.Is(err, io.EOF) {
break
}
if err != nil {
Expand Down Expand Up @@ -637,7 +637,7 @@ func listFilesInTar(path string) ([]string, error) {
var files []string
for {
hdr, err := tr.Next()
if err == io.EOF {
if errors.Is(err, io.EOF) {
break
}
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion pkg/util/command_util.go
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,7 @@ func IsSrcRemoteFileURL(rawurl string) bool {
if err != nil {
return false
}
_, err = http.Get(rawurl)
_, err = http.Get(rawurl) //nolint:noctx
return err == nil
}

Expand Down
10 changes: 5 additions & 5 deletions pkg/util/fs_util.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ func GetFSFromLayers(root string, layers []v1.Layer, opts ...FSOpt) ([]string, e
tr := tar.NewReader(r)
for {
hdr, err := tr.Next()
if err == io.EOF {
if errors.Is(err, io.EOF) {
break
}

Expand Down Expand Up @@ -221,7 +221,7 @@ func DeleteFilesystem() error {
return filepath.Walk(config.RootDir, func(path string, info os.FileInfo, err error) error {
if err != nil {
// ignore errors when deleting.
return nil
return nil //nolint:nilerr
}

if CheckIgnoreList(path) {
Expand Down Expand Up @@ -270,7 +270,7 @@ func UnTar(r io.Reader, dest string) ([]string, error) {
tr := tar.NewReader(r)
for {
hdr, err := tr.Next()
if err == io.EOF {
if errors.Is(err, io.EOF) {
break
}
if err != nil {
Expand Down Expand Up @@ -450,7 +450,7 @@ func DetectFilesystemIgnoreList(path string) error {
for {
line, err := reader.ReadString('\n')
logrus.Tracef("Read the following line from %s: %s", path, line)
if err != nil && err != io.EOF {
if err != nil && !errors.Is(err, io.EOF) {
return err
}
lineArr := strings.Split(line, " ")
Expand Down Expand Up @@ -604,7 +604,7 @@ func AddVolumePathToIgnoreList(path string) {
// - destination will have permissions of 0600
// - If remote file has HTTP Last-Modified header, we set the mtime of the file to that timestamp
func DownloadFileToDest(rawurl, dest string, uid, gid int64) error {
resp, err := http.Get(rawurl)
resp, err := http.Get(rawurl) //nolint:noctx
if err != nil {
return err
}
Expand Down
6 changes: 3 additions & 3 deletions pkg/util/tar_util.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ func (t *Tar) Close() {
func (t *Tar) AddFileToTar(p string) error {
i, err := os.Lstat(p)
if err != nil {
return fmt.Errorf("Failed to get file info for %s: %s", p, err)
return fmt.Errorf("Failed to get file info for %s: %w", p, err)
}
linkDst := ""
if i.Mode()&os.ModeSymlink != 0 {
Expand Down Expand Up @@ -156,7 +156,7 @@ func writeSecurityXattrToToFile(path string, hdr *tar.Header) error {
}
if capability, ok := hdr.Xattrs[securityCapabilityXattr]; ok {
err := system.Lsetxattr(path, securityCapabilityXattr, []byte(capability), 0)
if err != nil && !errors.Is(err, syscall.EOPNOTSUPP) && err != system.ErrNotSupportedPlatform {
if err != nil && !errors.Is(err, syscall.EOPNOTSUPP) && !errors.Is(err, system.ErrNotSupportedPlatform) {
return errors.Wrapf(err, "failed to write %q attribute to %q", securityCapabilityXattr, path)
}
}
Expand All @@ -170,7 +170,7 @@ func readSecurityXattrToTarHeader(path string, hdr *tar.Header) error {
hdr.Xattrs = make(map[string]string)
}
capability, err := system.Lgetxattr(path, securityCapabilityXattr)
if err != nil && !errors.Is(err, syscall.EOPNOTSUPP) && err != system.ErrNotSupportedPlatform {
if err != nil && !errors.Is(err, syscall.EOPNOTSUPP) && !errors.Is(err, system.ErrNotSupportedPlatform) {
return errors.Wrapf(err, "failed to read %q attribute from %q", securityCapabilityXattr, path)
}
if capability != nil {
Expand Down
5 changes: 3 additions & 2 deletions pkg/util/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import (
"time"

"github.com/minio/highwayhash"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
"golang.org/x/sys/unix"
)
Expand Down Expand Up @@ -195,7 +196,7 @@ func Lgetxattr(path string, attr string) ([]byte, error) {
dest := make([]byte, 128)
sz, errno := unix.Lgetxattr(path, attr, dest)

for errno == unix.ERANGE {
for errors.Is(errno, unix.ERANGE) {
// Buffer too small, use zero-sized buffer to get the actual size
sz, errno = unix.Lgetxattr(path, attr, []byte{})
if errno != nil {
Expand All @@ -206,7 +207,7 @@ func Lgetxattr(path string, attr string) ([]byte, error) {
}

switch {
case errno == unix.ENODATA:
case errors.Is(errno, unix.ENODATA):
return nil, nil
case errno != nil:
return nil, errno
Expand Down
2 changes: 1 addition & 1 deletion testutil/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ func checkErr(shouldErr bool, err error) error {
return fmt.Errorf("Expected error, but returned none")
}
if err != nil && !shouldErr {
return fmt.Errorf("Unexpected error: %s", err)
return fmt.Errorf("Unexpected error: %w", err)
}
return nil
}

0 comments on commit 92ce78c

Please sign in to comment.