Skip to content

Commit bbcce9f

Browse files
authored
refactor: move from io/ioutil to io and os package (#1245)
The io/ioutil package has been deprecated as of Go 1.16, see https://golang.org/doc/go1.16#ioutil. This commit replaces the existing io/ioutil functions with their new definitions in io and os packages. Signed-off-by: Eng Zer Jun <engzerjun@gmail.com>
1 parent 6bcb4af commit bbcce9f

File tree

13 files changed

+42
-52
lines changed

13 files changed

+42
-52
lines changed

integration/client_server_test.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ package integration
66
import (
77
"context"
88
"fmt"
9-
"io/ioutil"
9+
"io"
1010
"os"
1111
"path/filepath"
1212
"strings"
@@ -495,7 +495,7 @@ func setup(t *testing.T, options setupOptions) (*cli.App, string, string) {
495495
go func() {
496496
// Setup CLI App
497497
app := commands.NewApp(version)
498-
app.Writer = ioutil.Discard
498+
app.Writer = io.Discard
499499
osArgs := setupServer(addr, options.token, options.tokenHeader, cacheDir, options.cacheBackend)
500500

501501
// Run Trivy server
@@ -508,7 +508,7 @@ func setup(t *testing.T, options setupOptions) (*cli.App, string, string) {
508508

509509
// Setup CLI App
510510
app := commands.NewApp(version)
511-
app.Writer = ioutil.Discard
511+
app.Writer = io.Discard
512512

513513
return app, addr, cacheDir
514514
}
@@ -549,10 +549,10 @@ func setupClient(t *testing.T, c args, addr string, cacheDir string, golden stri
549549
var err error
550550
var ignoreTmpDir string
551551
if len(c.IgnoreIDs) != 0 {
552-
ignoreTmpDir, err = ioutil.TempDir("", "ignore")
552+
ignoreTmpDir, err = os.MkdirTemp("", "ignore")
553553
require.NoError(t, err, "failed to create a temp dir")
554554
trivyIgnore := filepath.Join(ignoreTmpDir, ".trivyignore")
555-
err = ioutil.WriteFile(trivyIgnore, []byte(strings.Join(c.IgnoreIDs, "\n")), 0444)
555+
err = os.WriteFile(trivyIgnore, []byte(strings.Join(c.IgnoreIDs, "\n")), 0444)
556556
require.NoError(t, err, "failed to write .trivyignore")
557557
osArgs = append(osArgs, []string{"--ignorefile", trivyIgnore}...)
558558
}
@@ -568,7 +568,7 @@ func setupClient(t *testing.T, c args, addr string, cacheDir string, golden stri
568568
if *update {
569569
outputFile = golden
570570
} else {
571-
output, _ := ioutil.TempFile("", "integration")
571+
output, _ := os.CreateTemp("", "integration")
572572
assert.Nil(t, output.Close())
573573
outputFile = output.Name()
574574
}
@@ -615,9 +615,9 @@ func setupRedis(t *testing.T, ctx context.Context) (testcontainers.Container, st
615615
func compare(t *testing.T, wantFile, gotFile string) {
616616
t.Helper()
617617
// Compare want and got
618-
want, err := ioutil.ReadFile(wantFile)
618+
want, err := os.ReadFile(wantFile)
619619
assert.NoError(t, err)
620-
got, err := ioutil.ReadFile(gotFile)
620+
got, err := os.ReadFile(gotFile)
621621
assert.NoError(t, err)
622622

623623
if strings.HasSuffix(wantFile, ".json.golden") {

integration/docker/docker.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import (
66
"encoding/json"
77
"fmt"
88
"io"
9-
"io/ioutil"
109
"net/url"
1110
"os"
1211

@@ -78,7 +77,7 @@ func (d Docker) ReplicateImage(ctx context.Context, imageRef, imagePath string,
7877
if err != nil {
7978
return err
8079
}
81-
if _, err = io.Copy(ioutil.Discard, resp.Body); err != nil {
80+
if _, err = io.Copy(io.Discard, resp.Body); err != nil {
8281
return err
8382
}
8483
defer resp.Body.Close()
@@ -110,7 +109,7 @@ func (d Docker) ReplicateImage(ctx context.Context, imageRef, imagePath string,
110109
}
111110
defer pushOut.Close()
112111

113-
if _, err = io.Copy(ioutil.Discard, pushOut); err != nil {
112+
if _, err = io.Copy(io.Discard, pushOut); err != nil {
114113
return err
115114
}
116115
return nil

integration/docker_engine_test.go

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ package integration
66
import (
77
"context"
88
"io"
9-
"io/ioutil"
109
"os"
1110
"strings"
1211
"testing"
@@ -270,14 +269,14 @@ func TestRun_WithDockerEngine(t *testing.T) {
270269
// load image into docker engine
271270
res, err := cli.ImageLoad(ctx, testfile, true)
272271
require.NoError(t, err, tc.name)
273-
io.Copy(ioutil.Discard, res.Body)
272+
io.Copy(io.Discard, res.Body)
274273

275274
// tag our image to something unique
276275
err = cli.ImageTag(ctx, tc.imageTag, tc.testfile)
277276
require.NoError(t, err, tc.name)
278277
}
279278

280-
of, err := ioutil.TempFile("", "integration-docker-engine-output-file-*")
279+
of, err := os.CreateTemp("", "integration-docker-engine-output-file-*")
281280
require.NoError(t, err, tc.name)
282281
defer os.Remove(of.Name())
283282

@@ -301,7 +300,7 @@ func TestRun_WithDockerEngine(t *testing.T) {
301300
}
302301
if len(tc.ignoreIDs) != 0 {
303302
trivyIgnore := ".trivyignore"
304-
err := ioutil.WriteFile(trivyIgnore, []byte(strings.Join(tc.ignoreIDs, "\n")), 0444)
303+
err := os.WriteFile(trivyIgnore, []byte(strings.Join(tc.ignoreIDs, "\n")), 0444)
305304
assert.NoError(t, err, "failed to write .trivyignore")
306305
defer os.Remove(trivyIgnore)
307306
}
@@ -318,9 +317,9 @@ func TestRun_WithDockerEngine(t *testing.T) {
318317
}
319318

320319
// check for vulnerability output info
321-
got, err := ioutil.ReadAll(of)
320+
got, err := io.ReadAll(of)
322321
assert.NoError(t, err, tc.name)
323-
want, err := ioutil.ReadFile(tc.expectedOutputFile)
322+
want, err := os.ReadFile(tc.expectedOutputFile)
324323
assert.NoError(t, err, tc.name)
325324
assert.JSONEq(t, string(want), string(got), tc.name)
326325

integration/registry_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import (
99
"crypto/x509"
1010
"encoding/json"
1111
"fmt"
12-
"io/ioutil"
12+
"io"
1313
"net/http"
1414
"net/url"
1515
"os"
@@ -236,7 +236,7 @@ func scan(t *testing.T, imageRef name.Reference, baseDir, goldenFile string, opt
236236
if *update && goldenFile != "" {
237237
outputFile = goldenFile
238238
} else {
239-
output, err := ioutil.TempFile("", "integration")
239+
output, err := os.CreateTemp("", "integration")
240240
if err != nil {
241241
return "", cleanup, err
242242
}
@@ -256,7 +256,7 @@ func scan(t *testing.T, imageRef name.Reference, baseDir, goldenFile string, opt
256256

257257
// Setup CLI App
258258
app := commands.NewApp("dev")
259-
app.Writer = ioutil.Discard
259+
app.Writer = io.Discard
260260

261261
osArgs := []string{"trivy", "--cache-dir", cacheDir, "--format", "json", "--skip-update", "--output", outputFile, imageRef.Name()}
262262

@@ -306,7 +306,7 @@ func unsetEnv() error {
306306

307307
func requestRegistryToken(imageRef name.Reference, baseDir string, opt registryOption) (string, error) {
308308
// Create a CA certificate pool and add cert.pem to it
309-
caCert, err := ioutil.ReadFile(filepath.Join(baseDir, "data", "certs", "cert.pem"))
309+
caCert, err := os.ReadFile(filepath.Join(baseDir, "data", "certs", "cert.pem"))
310310
if err != nil {
311311
return "", err
312312
}

integration/standalone_tar_test.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
package integration
55

66
import (
7-
"io/ioutil"
7+
"io"
88
"os"
99
"strings"
1010
"testing"
@@ -377,7 +377,7 @@ func TestRun_WithTar(t *testing.T) {
377377

378378
// Setup CLI App
379379
app := commands.NewApp("dev")
380-
app.Writer = ioutil.Discard
380+
app.Writer = io.Discard
381381

382382
for _, c := range cases {
383383
t.Run(c.name, func(t *testing.T) {
@@ -402,7 +402,7 @@ func TestRun_WithTar(t *testing.T) {
402402
}
403403
if len(c.testArgs.IgnoreIDs) != 0 {
404404
trivyIgnore := ".trivyignore"
405-
err := ioutil.WriteFile(trivyIgnore, []byte(strings.Join(c.testArgs.IgnoreIDs, "\n")), 0444)
405+
err := os.WriteFile(trivyIgnore, []byte(strings.Join(c.testArgs.IgnoreIDs, "\n")), 0444)
406406
assert.NoError(t, err, "failed to write .trivyignore")
407407
defer os.Remove(trivyIgnore)
408408
}
@@ -427,7 +427,7 @@ func TestRun_WithTar(t *testing.T) {
427427
if *update {
428428
outputFile = c.golden
429429
} else {
430-
output, _ := ioutil.TempFile("", "integration")
430+
output, _ := os.CreateTemp("", "integration")
431431
assert.Nil(t, output.Close())
432432
defer os.Remove(output.Name())
433433
outputFile = output.Name()
@@ -439,9 +439,9 @@ func TestRun_WithTar(t *testing.T) {
439439
assert.Nil(t, app.Run(osArgs))
440440

441441
// Compare want and got
442-
want, err := ioutil.ReadFile(c.golden)
442+
want, err := os.ReadFile(c.golden)
443443
assert.NoError(t, err)
444-
got, err := ioutil.ReadFile(outputFile)
444+
got, err := os.ReadFile(outputFile)
445445
assert.NoError(t, err)
446446

447447
assert.JSONEq(t, string(want), string(got))

pkg/commands/app_test.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package commands
33
import (
44
"bytes"
55
"encoding/json"
6-
"io/ioutil"
76
"os"
87
"path/filepath"
98
"testing"
@@ -81,7 +80,7 @@ Vulnerability DB:
8180
case tt.args.cacheDir != "":
8281
cacheDir = tt.args.cacheDir
8382
default:
84-
cacheDir, _ = ioutil.TempDir("", "Test_showVersion-*")
83+
cacheDir, _ = os.MkdirTemp("", "Test_showVersion-*")
8584
defer os.RemoveAll(cacheDir)
8685
}
8786

pkg/db/db_test.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import (
44
"context"
55
"encoding/json"
66
"errors"
7-
"io/ioutil"
87
"os"
98
"testing"
109
"time"
@@ -246,7 +245,7 @@ func TestClient_Download(t *testing.T) {
246245
fs := afero.NewMemMapFs()
247246
metadata := NewMetadata(fs, "/cache")
248247

249-
dir, err := ioutil.TempDir("", "db")
248+
dir, err := os.MkdirTemp("", "db")
250249
require.NoError(t, err, tc.name)
251250
defer os.RemoveAll(dir)
252251

@@ -349,7 +348,7 @@ func TestClient_UpdateMetadata(t *testing.T) {
349348
fs := afero.NewMemMapFs()
350349
metadata := NewMetadata(fs, "/cache")
351350

352-
dir, err := ioutil.TempDir("", "db")
351+
dir, err := os.MkdirTemp("", "db")
353352
require.NoError(t, err, tc.name)
354353
defer os.RemoveAll(dir)
355354

pkg/github/github_test.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import (
44
"context"
55
"fmt"
66
"io"
7-
"io/ioutil"
87
"net/http"
98
"net/http/httptest"
109
"net/url"
@@ -122,7 +121,7 @@ func TestClient_DownloadDB(t *testing.T) {
122121
{
123122
input: 100,
124123
output: downloadAssetOutput{
125-
rc: ioutil.NopCloser(strings.NewReader("foo")),
124+
rc: io.NopCloser(strings.NewReader("foo")),
126125
},
127126
},
128127
},
@@ -217,7 +216,7 @@ func TestClient_DownloadDB(t *testing.T) {
217216
{
218217
input: 300,
219218
output: downloadAssetOutput{
220-
rc: ioutil.NopCloser(strings.NewReader("foo")),
219+
rc: io.NopCloser(strings.NewReader("foo")),
221220
},
222221
},
223222
},
@@ -268,7 +267,7 @@ func TestClient_DownloadDB(t *testing.T) {
268267
{
269268
input: 200,
270269
output: downloadAssetOutput{
271-
rc: ioutil.NopCloser(strings.NewReader("foo")),
270+
rc: io.NopCloser(strings.NewReader("foo")),
272271
},
273272
},
274273
},

pkg/report/template.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import (
66
"fmt"
77
"html"
88
"io"
9-
"io/ioutil"
109
"os"
1110
"regexp"
1211
"strings"
@@ -31,7 +30,7 @@ type TemplateWriter struct {
3130
// NewTemplateWriter is the factory method to return TemplateWriter object
3231
func NewTemplateWriter(output io.Writer, outputTemplate string) (*TemplateWriter, error) {
3332
if strings.HasPrefix(outputTemplate, "@") {
34-
buf, err := ioutil.ReadFile(strings.TrimPrefix(outputTemplate, "@"))
33+
buf, err := os.ReadFile(strings.TrimPrefix(outputTemplate, "@"))
3534
if err != nil {
3635
return nil, xerrors.Errorf("error retrieving template from path: %w", err)
3736
}

pkg/result/result.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import (
44
"bufio"
55
"context"
66
"fmt"
7-
"io/ioutil"
87
"os"
98
"sort"
109
"strings"
@@ -262,7 +261,7 @@ func toSlice(uniqVulns map[string]types.DetectedVulnerability) []types.DetectedV
262261

263262
func applyPolicy(ctx context.Context, vulns []types.DetectedVulnerability, misconfs []types.DetectedMisconfiguration,
264263
policyFile string) ([]types.DetectedVulnerability, []types.DetectedMisconfiguration, error) {
265-
policy, err := ioutil.ReadFile(policyFile)
264+
policy, err := os.ReadFile(policyFile)
266265
if err != nil {
267266
return nil, nil, xerrors.Errorf("unable to read the policy file: %w", err)
268267
}

0 commit comments

Comments
 (0)