Skip to content

Commit

Permalink
refactor: move from io/ioutil to io and os packages (#906)
Browse files Browse the repository at this point in the history
* chore: run `go fmt ./...`

This commit synchronizes `//go:build` lines with `// +build` lines.

Reference: https://go.googlesource.com/proposal/+/master/design/draft-gobuild.md
Signed-off-by: Eng Zer Jun <engzerjun@gmail.com>

* refactor: move from io/ioutil to io and os packages

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>
  • Loading branch information
Juneezee committed Dec 10, 2021
1 parent be403a9 commit f9c2a82
Show file tree
Hide file tree
Showing 46 changed files with 135 additions and 178 deletions.
3 changes: 1 addition & 2 deletions cdn/storedriver/local/local_driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ package local
import (
"fmt"
"io"
"io/ioutil"
"os"
"path/filepath"

Expand Down Expand Up @@ -156,7 +155,7 @@ func (ds *driver) GetBytes(raw *storedriver.Raw) (data []byte, err error) {
return nil, err
}
if raw.Length == 0 {
data, err = ioutil.ReadAll(f)
data, err = io.ReadAll(f)
} else {
data = make([]byte, raw.Length)
_, err = f.Read(data)
Expand Down
9 changes: 4 additions & 5 deletions cdn/storedriver/local/local_driver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ package local

import (
"io"
"io/ioutil"
"os"
"path/filepath"
"strings"
Expand Down Expand Up @@ -46,7 +45,7 @@ type LocalDriverTestSuite struct {
}

func (s *LocalDriverTestSuite) SetupSuite() {
s.workHome, _ = ioutil.TempDir("/tmp", "cdn-local-driver-repo")
s.workHome, _ = os.MkdirTemp("/tmp", "cdn-local-driver-repo")
pluginProps := map[plugins.PluginType][]*plugins.PluginProperties{
plugins.StorageDriverPlugin: {
&plugins.PluginProperties{
Expand Down Expand Up @@ -296,7 +295,7 @@ func (s *LocalDriverTestSuite) TestGetPut() {
r, err := s.Get(v.getRaw)
s.True(v.getErrCheck(err))
if err == nil {
result, err := ioutil.ReadAll(r)
result, err := io.ReadAll(r)
s.Nil(err)
s.Equal(v.expected, string(result))
}
Expand Down Expand Up @@ -386,7 +385,7 @@ func (s *LocalDriverTestSuite) TestAppendBytes() {
r, err := s.Get(v.getRaw)
s.True(v.getErrCheck(err))
if err == nil {
result, err := ioutil.ReadAll(r)
result, err := io.ReadAll(r)
s.Nil(err)
s.Equal(v.expected, string(result))
}
Expand Down Expand Up @@ -466,7 +465,7 @@ func (s *LocalDriverTestSuite) TestPutTrunc() {
s.Nil(err)

if err == nil {
result, err := ioutil.ReadAll(r)
result, err := io.ReadAll(r)
s.Nil(err)
s.Equal(string(result[:]), v.expectedData)
}
Expand Down
3 changes: 1 addition & 2 deletions cdn/supervisor/cdn/cache_detector.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import (
"fmt"
"hash"
"io"
"io/ioutil"
"sort"

"github.com/pkg/errors"
Expand Down Expand Up @@ -270,7 +269,7 @@ func checkPieceContent(reader io.Reader, pieceRecord *storage.PieceMetaRecord, f
// TODO Analyze the original data for the slice format to calculate fileMd5
pieceMd5 := md5.New()
tee := io.TeeReader(io.TeeReader(io.LimitReader(reader, int64(pieceRecord.PieceLen)), pieceMd5), fileDigest)
if n, err := io.Copy(ioutil.Discard, tee); n != int64(pieceRecord.PieceLen) || err != nil {
if n, err := io.Copy(io.Discard, tee); n != int64(pieceRecord.PieceLen) || err != nil {
return errors.Wrap(err, "read piece content")
}
realPieceMd5 := digestutils.ToHashString(pieceMd5)
Expand Down
15 changes: 7 additions & 8 deletions cdn/supervisor/cdn/cache_detector_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import (
"crypto/md5"
"hash"
"io"
"io/ioutil"
"os"
"sort"
"strings"
Expand Down Expand Up @@ -66,21 +65,21 @@ func (suite *CacheDetectorTestSuite) SetupSuite() {
storageManager.EXPECT().ReadFileMetadata(noCache.taskID).Return(noCache.fileMeta, os.ErrNotExist).AnyTimes()
storageManager.EXPECT().ReadDownloadFile(fullNoExpiredCache.taskID).DoAndReturn(
func(taskID string) (io.ReadCloser, error) {
content, err := ioutil.ReadFile("../../testdata/cdn/go.html")
content, err := os.ReadFile("../../testdata/cdn/go.html")
suite.Nil(err)
return ioutil.NopCloser(strings.NewReader(string(content))), nil
return io.NopCloser(strings.NewReader(string(content))), nil
}).AnyTimes()
storageManager.EXPECT().ReadDownloadFile(partialNotSupportRangeCache.taskID).DoAndReturn(
func(taskID string) (io.ReadCloser, error) {
content, err := ioutil.ReadFile("../../testdata/cdn/go.html")
content, err := os.ReadFile("../../testdata/cdn/go.html")
suite.Nil(err)
return ioutil.NopCloser(strings.NewReader(string(content))), nil
return io.NopCloser(strings.NewReader(string(content))), nil
}).AnyTimes()
storageManager.EXPECT().ReadDownloadFile(partialSupportRangeCache.taskID).DoAndReturn(
func(taskID string) (io.ReadCloser, error) {
content, err := ioutil.ReadFile("../../testdata/cdn/go.html")
content, err := os.ReadFile("../../testdata/cdn/go.html")
suite.Nil(err)
return ioutil.NopCloser(strings.NewReader(string(content))), nil
return io.NopCloser(strings.NewReader(string(content))), nil
}).AnyTimes()
storageManager.EXPECT().ReadDownloadFile(noCache.taskID).Return(nil, os.ErrNotExist).AnyTimes()
storageManager.EXPECT().ReadPieceMetaRecords(fullNoExpiredCache.taskID).Return(fullNoExpiredCache.pieces, nil).AnyTimes()
Expand Down Expand Up @@ -421,7 +420,7 @@ func (suite *CacheDetectorTestSuite) TestParseByReadMetaFile() {
}

func (suite *CacheDetectorTestSuite) TestCheckPieceContent() {
content, err := ioutil.ReadFile("../../testdata/cdn/go.html")
content, err := os.ReadFile("../../testdata/cdn/go.html")
suite.Nil(err)
type args struct {
reader io.Reader
Expand Down
5 changes: 2 additions & 3 deletions cdn/supervisor/cdn/cache_writer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import (
"context"
"fmt"
"io"
"io/ioutil"
"os"
"strings"
"testing"
Expand Down Expand Up @@ -83,7 +82,7 @@ func NewPlugins(workHome string) map[plugins.PluginType][]*plugins.PluginPropert
}

func (suite *CacheWriterTestSuite) SetupSuite() {
suite.workHome, _ = ioutil.TempDir("/tmp", "cdn-CacheWriterDetectorTestSuite-")
suite.workHome, _ = os.MkdirTemp("/tmp", "cdn-CacheWriterDetectorTestSuite-")
suite.T().Log("workHome:", suite.workHome)
suite.Nil(plugins.Initialize(NewPlugins(suite.workHome)))
storeMgr, ok := storage.Get(config.DefaultStorageMode)
Expand All @@ -105,7 +104,7 @@ func (suite *CacheWriterTestSuite) TearDownSuite() {
}

func (suite *CacheWriterTestSuite) TestStartWriter() {
content, err := ioutil.ReadFile("../../testdata/cdn/go.html")
content, err := os.ReadFile("../../testdata/cdn/go.html")
suite.Nil(err)
contentLen := int64(len(content))
type args struct {
Expand Down
15 changes: 7 additions & 8 deletions cdn/supervisor/cdn/manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import (
"context"
"fmt"
"io"
"io/ioutil"
"os"
"strings"
"testing"
Expand Down Expand Up @@ -54,7 +53,7 @@ type CDNManagerTestSuite struct {
}

func (suite *CDNManagerTestSuite) SetupSuite() {
suite.workHome, _ = ioutil.TempDir("/tmp", "cdn-ManagerTestSuite-")
suite.workHome, _ = os.MkdirTemp("/tmp", "cdn-ManagerTestSuite-")
fmt.Printf("workHome: %s", suite.workHome)
suite.Nil(plugins.Initialize(NewPlugins(suite.workHome)))
storeMgr, ok := storage.Get(config.DefaultStorageMode)
Expand Down Expand Up @@ -93,26 +92,26 @@ func (suite *CDNManagerTestSuite) TestTriggerCDN() {
sourceClient.EXPECT().IsExpired(gomock.Any(), gomock.Any()).Return(false, nil).AnyTimes()
sourceClient.EXPECT().Download(gomock.Any()).DoAndReturn(
func(request *source.Request) (io.ReadCloser, error) {
content, _ := ioutil.ReadFile("../../testdata/cdn/go.html")
content, _ := os.ReadFile("../../testdata/cdn/go.html")
if request.Header.Get(source.Range) != "" {
parsed, _ := rangeutils.GetRange(request.Header.Get(source.Range))
return ioutil.NopCloser(io.NewSectionReader(strings.NewReader(string(content)), int64(parsed.StartIndex), int64(parsed.EndIndex))), nil
return io.NopCloser(io.NewSectionReader(strings.NewReader(string(content)), int64(parsed.StartIndex), int64(parsed.EndIndex))), nil
}
return ioutil.NopCloser(strings.NewReader(string(content))), nil
return io.NopCloser(strings.NewReader(string(content))), nil
},
).AnyTimes()
sourceClient.EXPECT().DownloadWithExpireInfo(gomock.Any()).DoAndReturn(
func(request *source.Request) (io.ReadCloser, *source.ExpireInfo, error) {
content, _ := ioutil.ReadFile("../../testdata/cdn/go.html")
content, _ := os.ReadFile("../../testdata/cdn/go.html")
if request.Header.Get(source.Range) != "" {
parsed, _ := rangeutils.GetRange(request.Header.Get(source.Range))
return ioutil.NopCloser(io.NewSectionReader(strings.NewReader(string(content)), int64(parsed.StartIndex), int64(parsed.EndIndex))),
return io.NopCloser(io.NewSectionReader(strings.NewReader(string(content)), int64(parsed.StartIndex), int64(parsed.EndIndex))),
&source.ExpireInfo{
LastModified: "Sun, 06 Jun 2021 12:52:30 GMT",
ETag: "etag",
}, nil
}
return ioutil.NopCloser(strings.NewReader(string(content))),
return io.NopCloser(strings.NewReader(string(content))),
&source.ExpireInfo{
LastModified: "Sun, 06 Jun 2021 12:52:30 GMT",
ETag: "etag",
Expand Down
1 change: 1 addition & 0 deletions client/config/dfget_darwin.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
//go:build darwin
// +build darwin

/*
Expand Down
1 change: 1 addition & 0 deletions client/config/dfget_linux.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
//go:build linux
// +build linux

/*
Expand Down
16 changes: 3 additions & 13 deletions client/config/dynconfig_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
package config

import (
"io/ioutil"
"os"
"path/filepath"
"testing"
Expand All @@ -32,10 +31,7 @@ import (
)

func TestDynconfigNewDynconfig(t *testing.T) {
mockCacheDir, err := ioutil.TempDir("", "dragonfly-test")
if err != nil {
t.Fatal(err)
}
mockCacheDir := t.TempDir()

mockCachePath := filepath.Join(mockCacheDir, cacheFileName)
tests := []struct {
Expand Down Expand Up @@ -125,10 +121,7 @@ func TestDynconfigNewDynconfig(t *testing.T) {
}

func TestDynconfigGet(t *testing.T) {
mockCacheDir, err := ioutil.TempDir("", "dragonfly-test")
if err != nil {
t.Fatal(err)
}
mockCacheDir := t.TempDir()

mockCachePath := filepath.Join(mockCacheDir, cacheFileName)
tests := []struct {
Expand Down Expand Up @@ -310,10 +303,7 @@ func TestDynconfigGet(t *testing.T) {
}

func TestDynconfigGetSchedulers(t *testing.T) {
mockCacheDir, err := ioutil.TempDir("", "dragonfly-test")
if err != nil {
t.Fatal(err)
}
mockCacheDir := t.TempDir()

mockCachePath := filepath.Join(mockCacheDir, cacheFileName)
tests := []struct {
Expand Down
14 changes: 7 additions & 7 deletions client/config/peerhost.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ import (
"crypto/x509"
"encoding/json"
"fmt"
"io/ioutil"
"net"
"net/url"
"os"
"path/filepath"
"regexp"
"strings"
Expand Down Expand Up @@ -70,7 +70,7 @@ func NewDaemonConfig() *DaemonOption {
}

func (p *DaemonOption) Load(path string) error {
data, err := ioutil.ReadFile(path)
data, err := os.ReadFile(path)
if err != nil {
return fmt.Errorf("unable to load peer host configuration from %q [%v]", path, err)
}
Expand Down Expand Up @@ -206,7 +206,7 @@ func (p *ProxyOption) UnmarshalJSON(b []byte) error {

switch value := v.(type) {
case string:
file, err := ioutil.ReadFile(value)
file, err := os.ReadFile(value)
if err != nil {
return err
}
Expand All @@ -232,7 +232,7 @@ func (p *ProxyOption) UnmarshalYAML(node *yaml.Node) error {
return err
}

file, err := ioutil.ReadFile(path)
file, err := os.ReadFile(path)
if err != nil {
return err
}
Expand Down Expand Up @@ -445,7 +445,7 @@ func (f *FileString) UnmarshalJSON(b []byte) error {
return err
}

file, err := ioutil.ReadFile(s)
file, err := os.ReadFile(s)
if err != nil {
return err
}
Expand All @@ -465,7 +465,7 @@ func (f *FileString) UnmarshalYAML(node *yaml.Node) error {
return errors.New("invalid filestring")
}

file, err := ioutil.ReadFile(s)
file, err := os.ReadFile(s)
if err != nil {
return err
}
Expand Down Expand Up @@ -628,7 +628,7 @@ func certPoolFromFiles(files ...string) (*x509.CertPool, error) {

roots := x509.NewCertPool()
for _, f := range files {
cert, err := ioutil.ReadFile(f)
cert, err := os.ReadFile(f)
if err != nil {
return nil, errors.Wrapf(err, "read cert file %s", f)
}
Expand Down
5 changes: 2 additions & 3 deletions client/daemon/daemon.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import (
"crypto/tls"
"crypto/x509"
"fmt"
"io/ioutil"
"net"
"net/http"
"os"
Expand Down Expand Up @@ -226,7 +225,7 @@ func New(opt *config.DaemonOption, d dfpath.Dfpath) (Daemon, error) {

func loadGPRCTLSCredentials(opt config.SecurityOption) (credentials.TransportCredentials, error) {
// Load certificate of the CA who signed client's certificate
pemClientCA, err := ioutil.ReadFile(opt.CACert)
pemClientCA, err := os.ReadFile(opt.CACert)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -301,7 +300,7 @@ func (*clientDaemon) prepareTCPListener(opt config.ListenOption, withTLS bool) (
}
tlsConfig := opt.Security.TLSConfig
if opt.Security.CACert != "" {
caCert, err := ioutil.ReadFile(opt.Security.CACert)
caCert, err := os.ReadFile(opt.Security.CACert)
if err != nil {
return nil, -1, err
}
Expand Down
1 change: 1 addition & 0 deletions client/daemon/daemon_linux.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
//go:build linux
// +build linux

/*
Expand Down
1 change: 1 addition & 0 deletions client/daemon/daemon_stub.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
//go:build !linux
// +build !linux

/*
Expand Down

0 comments on commit f9c2a82

Please sign in to comment.