From 11f2f2dbc48771528053773693a237a4438c32db Mon Sep 17 00:00:00 2001 From: Abhinav Nair <11939846+abhinavnair@users.noreply.github.com> Date: Sat, 25 Jun 2022 14:02:21 +0800 Subject: [PATCH 1/2] Replace deprecated ioutil pkg with os & io As of Go 1.16, the same functionality is now provided by package io or package os, and those implementations should be preferred in new code. So replacing all usage of ioutil pkg with io & os. Signed-off-by: Abhinav Nair <11939846+abhinavnair@users.noreply.github.com> --- pkg/compose/build_classic.go | 3 +-- pkg/compose/create.go | 4 ++-- pkg/e2e/compose_test.go | 3 +-- pkg/e2e/framework.go | 5 ++--- pkg/e2e/scan_message_test.go | 3 +-- pkg/utils/scan_suggest.go | 3 +-- 6 files changed, 8 insertions(+), 13 deletions(-) diff --git a/pkg/compose/build_classic.go b/pkg/compose/build_classic.go index 302d55fa2e..3c6487be85 100644 --- a/pkg/compose/build_classic.go +++ b/pkg/compose/build_classic.go @@ -21,7 +21,6 @@ import ( "encoding/json" "fmt" "io" - "io/ioutil" "os" "path/filepath" "runtime" @@ -215,7 +214,7 @@ func (s *composeService) doBuildClassicSimpleImage(ctx context.Context, options if imageID == "" { return "", errors.Errorf("Server did not provide an image ID. Cannot write %s", options.ImageIDFile) } - if err := ioutil.WriteFile(options.ImageIDFile, []byte(imageID), 0666); err != nil { + if err := os.WriteFile(options.ImageIDFile, []byte(imageID), 0666); err != nil { return "", err } } diff --git a/pkg/compose/create.go b/pkg/compose/create.go index 9f33dbc3a0..34e728eb4e 100644 --- a/pkg/compose/create.go +++ b/pkg/compose/create.go @@ -21,7 +21,7 @@ import ( "context" "encoding/json" "fmt" - "io/ioutil" + "os" "path" "path/filepath" "strconv" @@ -414,7 +414,7 @@ func parseSecurityOpts(p *types.Project, securityOpts []string) ([]string, error } } if con[0] == "seccomp" && con[1] != "unconfined" { - f, err := ioutil.ReadFile(p.RelativePath(con[1])) + f, err := os.ReadFile(p.RelativePath(con[1])) if err != nil { return securityOpts, errors.Errorf("opening seccomp profile (%s) failed: %v", con[1], err) } diff --git a/pkg/e2e/compose_test.go b/pkg/e2e/compose_test.go index 536befb562..f2ac2ad183 100644 --- a/pkg/e2e/compose_test.go +++ b/pkg/e2e/compose_test.go @@ -18,7 +18,6 @@ package e2e import ( "fmt" - "io/ioutil" "net/http" "os" "path/filepath" @@ -133,7 +132,7 @@ func TestComposePull(t *testing.T) { func TestDownComposefileInParentFolder(t *testing.T) { c := NewParallelCLI(t) - tmpFolder, err := ioutil.TempDir("fixtures/simple-composefile", "test-tmp") + tmpFolder, err := os.MkdirTemp("fixtures/simple-composefile", "test-tmp") assert.NilError(t, err) defer os.Remove(tmpFolder) // nolint: errcheck projectName := filepath.Base(tmpFolder) diff --git a/pkg/e2e/framework.go b/pkg/e2e/framework.go index ba0d6a1bc4..15eed45549 100644 --- a/pkg/e2e/framework.go +++ b/pkg/e2e/framework.go @@ -19,7 +19,6 @@ package e2e import ( "fmt" "io" - "io/ioutil" "net/http" "os" "path" @@ -115,7 +114,7 @@ func initializePlugins(t testing.TB, configDir string) { t.Cleanup(func() { if t.Failed() { - if conf, err := ioutil.ReadFile(filepath.Join(configDir, "config.json")); err == nil { + if conf, err := os.ReadFile(filepath.Join(configDir, "config.json")); err == nil { t.Logf("Config: %s\n", string(conf)) } t.Log("Contents of config dir:") @@ -389,7 +388,7 @@ func HTTPGetWithRetry( } poll.WaitOn(t, checkUp, poll.WithDelay(retryDelay), poll.WithTimeout(timeout)) if r != nil { - b, err := ioutil.ReadAll(r.Body) + b, err := io.ReadAll(r.Body) assert.NilError(t, err) return string(b) } diff --git a/pkg/e2e/scan_message_test.go b/pkg/e2e/scan_message_test.go index 70dccca43b..1515efd0f4 100644 --- a/pkg/e2e/scan_message_test.go +++ b/pkg/e2e/scan_message_test.go @@ -17,7 +17,6 @@ package e2e import ( - "io/ioutil" "os" "path/filepath" "strings" @@ -74,7 +73,7 @@ func TestDisplayScanMessageAfterBuild(t *testing.T) { t.Run("do not display if scan already invoked", func(t *testing.T) { _ = os.MkdirAll(filepath.Join(c.ConfigDir, "scan"), 0755) scanConfigFile := filepath.Join(c.ConfigDir, "scan", "config.json") - err := ioutil.WriteFile(scanConfigFile, []byte(`{"optin":true}`), 0644) + err := os.WriteFile(scanConfigFile, []byte(`{"optin":true}`), 0644) assert.NilError(t, err) res := c.RunDockerCmd(t, "build", "-t", "test-image-scan-msg", "fixtures/simple-build-test/nginx-build") diff --git a/pkg/utils/scan_suggest.go b/pkg/utils/scan_suggest.go index 93059f80c4..6452a577e9 100644 --- a/pkg/utils/scan_suggest.go +++ b/pkg/utils/scan_suggest.go @@ -19,7 +19,6 @@ package utils import ( "encoding/json" "fmt" - "io/ioutil" "os" "path/filepath" @@ -57,7 +56,7 @@ func scanAlreadyInvoked() bool { type scanOptin struct { Optin bool `json:"optin"` } - data, err := ioutil.ReadFile(filename) + data, err := os.ReadFile(filename) if err != nil { return true } From a783cc45743250441b33a73bf263dc2826b16aef Mon Sep 17 00:00:00 2001 From: Abhinav Nair <11939846+abhinavnair@users.noreply.github.com> Date: Tue, 28 Jun 2022 09:18:40 +0800 Subject: [PATCH 2/2] Blacklist ioutil pkg in golangci-lint This is to prevent anyone from accidentally importing the pkg Signed-off-by: Abhinav Nair <11939846+abhinavnair@users.noreply.github.com> --- .golangci.yml | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/.golangci.yml b/.golangci.yml index 48ca9cf911..d31ae43b2b 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -7,6 +7,7 @@ linters: disable-all: true enable: - deadcode + - depguard - errcheck - gocyclo - gofmt @@ -26,6 +27,13 @@ linters: - unused - varcheck linters-settings: + depguard: + list-type: blacklist + include-go-root: true + packages: + # The io/ioutil package has been deprecated. + # https://go.dev/doc/go1.16#ioutil + - io/ioutil gocyclo: min-complexity: 16 lll: