Skip to content

Commit 350c08d

Browse files
Prometheus2677FPiety0521
authored and
FPiety0521
committed
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.
1 parent 9c9a5c2 commit 350c08d

File tree

35 files changed

+109
-139
lines changed

35 files changed

+109
-139
lines changed

database/cassandra/cassandra.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,14 @@ package cassandra
33
import (
44
"errors"
55
"fmt"
6-
"go.uber.org/atomic"
76
"io"
8-
"io/ioutil"
97
nurl "net/url"
108
"strconv"
119
"strings"
1210
"time"
1311

12+
"go.uber.org/atomic"
13+
1414
"github.com/gocql/gocql"
1515
"github.com/golang-migrate/migrate/v4/database"
1616
"github.com/golang-migrate/migrate/v4/database/multistmt"
@@ -231,7 +231,7 @@ func (c *Cassandra) Run(migration io.Reader) error {
231231
return err
232232
}
233233

234-
migr, err := ioutil.ReadAll(migration)
234+
migr, err := io.ReadAll(migration)
235235
if err != nil {
236236
return err
237237
}

database/clickhouse/clickhouse.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import (
44
"database/sql"
55
"fmt"
66
"io"
7-
"io/ioutil"
87
"net/url"
98
"strconv"
109
"strings"
@@ -153,7 +152,7 @@ func (ch *ClickHouse) Run(r io.Reader) error {
153152
return err
154153
}
155154

156-
migration, err := ioutil.ReadAll(r)
155+
migration, err := io.ReadAll(r)
157156
if err != nil {
158157
return err
159158
}

database/cockroachdb/cockroachdb.go

+4-10
Original file line numberDiff line numberDiff line change
@@ -4,23 +4,17 @@ import (
44
"context"
55
"database/sql"
66
"fmt"
7-
"go.uber.org/atomic"
87
"io"
9-
"io/ioutil"
108
nurl "net/url"
119
"regexp"
1210
"strconv"
13-
)
1411

15-
import (
1612
"github.com/cockroachdb/cockroach-go/v2/crdb"
17-
"github.com/hashicorp/go-multierror"
18-
"github.com/lib/pq"
19-
)
20-
21-
import (
2213
"github.com/golang-migrate/migrate/v4"
2314
"github.com/golang-migrate/migrate/v4/database"
15+
"github.com/hashicorp/go-multierror"
16+
"github.com/lib/pq"
17+
"go.uber.org/atomic"
2418
)
2519

2620
func init() {
@@ -217,7 +211,7 @@ func (c *CockroachDb) Unlock() error {
217211
}
218212

219213
func (c *CockroachDb) Run(migration io.Reader) error {
220-
migr, err := ioutil.ReadAll(migration)
214+
migr, err := io.ReadAll(migration)
221215
if err != nil {
222216
return err
223217
}

database/firebird/firebird.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,14 @@ import (
77
"context"
88
"database/sql"
99
"fmt"
10+
"io"
11+
nurl "net/url"
12+
1013
"github.com/golang-migrate/migrate/v4"
1114
"github.com/golang-migrate/migrate/v4/database"
1215
"github.com/hashicorp/go-multierror"
1316
_ "github.com/nakagami/firebirdsql"
1417
"go.uber.org/atomic"
15-
"io"
16-
"io/ioutil"
17-
nurl "net/url"
1818
)
1919

2020
func init() {
@@ -122,7 +122,7 @@ func (f *Firebird) Unlock() error {
122122
}
123123

124124
func (f *Firebird) Run(migration io.Reader) error {
125-
migr, err := ioutil.ReadAll(migration)
125+
migr, err := io.ReadAll(migration)
126126
if err != nil {
127127
return err
128128
}

database/mongodb/mongodb.go

+12-12
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,12 @@ package mongodb
33
import (
44
"context"
55
"fmt"
6+
"io"
7+
"net/url"
8+
"os"
9+
"strconv"
10+
"time"
11+
612
"github.com/cenkalti/backoff/v4"
713
"github.com/golang-migrate/migrate/v4/database"
814
"github.com/hashicorp/go-multierror"
@@ -11,12 +17,6 @@ import (
1117
"go.mongodb.org/mongo-driver/mongo/options"
1218
"go.mongodb.org/mongo-driver/x/mongo/driver/connstring"
1319
"go.uber.org/atomic"
14-
"io"
15-
"io/ioutil"
16-
"net/url"
17-
os "os"
18-
"strconv"
19-
"time"
2020
)
2121

2222
func init() {
@@ -114,7 +114,7 @@ func WithInstance(instance *mongo.Client, config *Config) (database.Driver, erro
114114
}
115115

116116
func (m *Mongo) Open(dsn string) (database.Driver, error) {
117-
//connstring is experimental package, but it used for parse connection string in mongo.Connect function
117+
// connstring is experimental package, but it used for parse connection string in mongo.Connect function
118118
uri, err := connstring.Parse(dsn)
119119
if err != nil {
120120
return nil, err
@@ -182,7 +182,7 @@ func (m *Mongo) Open(dsn string) (database.Driver, error) {
182182
return mc, nil
183183
}
184184

185-
//Parse the url param, convert it to boolean
185+
// Parse the url param, convert it to boolean
186186
// returns error if param invalid. returns defaultValue if param not present
187187
func parseBoolean(urlParam string, defaultValue bool) (bool, error) {
188188

@@ -199,7 +199,7 @@ func parseBoolean(urlParam string, defaultValue bool) (bool, error) {
199199
return defaultValue, nil
200200
}
201201

202-
//Parse the url param, convert it to int
202+
// Parse the url param, convert it to int
203203
// returns error if param invalid. returns defaultValue if param not present
204204
func parseInt(urlParam string, defaultValue int) (int, error) {
205205

@@ -241,7 +241,7 @@ func (m *Mongo) Version() (version int, dirty bool, err error) {
241241
}
242242

243243
func (m *Mongo) Run(migration io.Reader) error {
244-
migr, err := ioutil.ReadAll(migration)
244+
migr, err := io.ReadAll(migration)
245245
if err != nil {
246246
return err
247247
}
@@ -268,8 +268,8 @@ func (m *Mongo) executeCommandsWithTransaction(ctx context.Context, cmds []bson.
268268
return &database.Error{OrigErr: err, Err: "failed to start transaction"}
269269
}
270270
if err := m.executeCommands(sessionContext, cmds); err != nil {
271-
//When command execution is failed, it's aborting transaction
272-
//If you tried to call abortTransaction, it`s return error that transaction already aborted
271+
// When command execution is failed, it's aborting transaction
272+
// If you tried to call abortTransaction, it`s return error that transaction already aborted
273273
return err
274274
}
275275
if err := sessionContext.CommitTransaction(sessionContext); err != nil {

database/mysql/mysql.go

+5-4
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,14 @@ import (
99
"crypto/x509"
1010
"database/sql"
1111
"fmt"
12-
"go.uber.org/atomic"
1312
"io"
14-
"io/ioutil"
1513
nurl "net/url"
14+
"os"
1615
"strconv"
1716
"strings"
1817

18+
"go.uber.org/atomic"
19+
1920
"github.com/go-sql-driver/mysql"
2021
"github.com/golang-migrate/migrate/v4/database"
2122
"github.com/hashicorp/go-multierror"
@@ -154,7 +155,7 @@ func urlToMySQLConfig(url string) (*mysql.Config, error) {
154155
if len(ctls) > 0 {
155156
if _, isBool := readBool(ctls); !isBool && strings.ToLower(ctls) != "skip-verify" {
156157
rootCertPool := x509.NewCertPool()
157-
pem, err := ioutil.ReadFile(parsedParams.Get("x-tls-ca"))
158+
pem, err := os.ReadFile(parsedParams.Get("x-tls-ca"))
158159
if err != nil {
159160
return nil, err
160161
}
@@ -322,7 +323,7 @@ func (m *Mysql) Unlock() error {
322323
}
323324

324325
func (m *Mysql) Run(migration io.Reader) error {
325-
migr, err := ioutil.ReadAll(migration)
326+
migr, err := io.ReadAll(migration)
326327
if err != nil {
327328
return err
328329
}

database/mysql/mysql_test.go

+5-11
Original file line numberDiff line numberDiff line change
@@ -9,27 +9,21 @@ import (
99
"encoding/pem"
1010
"errors"
1111
"fmt"
12-
"io/ioutil"
1312
"log"
1413
"math/big"
1514
"math/rand"
1615
"net/url"
1716
"os"
1817
"strconv"
1918
"testing"
20-
)
2119

22-
import (
2320
"github.com/dhui/dktest"
2421
"github.com/go-sql-driver/mysql"
25-
"github.com/stretchr/testify/assert"
26-
)
27-
28-
import (
2922
"github.com/golang-migrate/migrate/v4"
3023
dt "github.com/golang-migrate/migrate/v4/database/testing"
3124
"github.com/golang-migrate/migrate/v4/dktesting"
3225
_ "github.com/golang-migrate/migrate/v4/source/file"
26+
"github.com/stretchr/testify/assert"
3327
)
3428

3529
const defaultPort = 3306
@@ -88,7 +82,7 @@ func isReady(ctx context.Context, c dktest.ContainerInfo) bool {
8882
}
8983

9084
func Test(t *testing.T) {
91-
// mysql.SetLogger(mysql.Logger(log.New(ioutil.Discard, "", log.Ltime)))
85+
// mysql.SetLogger(mysql.Logger(log.New(io.Discard, "", log.Ltime)))
9286

9387
dktesting.ParallelTest(t, specs, func(t *testing.T, c dktest.ContainerInfo) {
9488
ip, port, err := c.Port(defaultPort)
@@ -121,7 +115,7 @@ func Test(t *testing.T) {
121115
}
122116

123117
func TestMigrate(t *testing.T) {
124-
// mysql.SetLogger(mysql.Logger(log.New(ioutil.Discard, "", log.Ltime)))
118+
// mysql.SetLogger(mysql.Logger(log.New(io.Discard, "", log.Ltime)))
125119

126120
dktesting.ParallelTest(t, specs, func(t *testing.T, c dktest.ContainerInfo) {
127121
ip, port, err := c.Port(defaultPort)
@@ -159,7 +153,7 @@ func TestMigrate(t *testing.T) {
159153
}
160154

161155
func TestMigrateAnsiQuotes(t *testing.T) {
162-
// mysql.SetLogger(mysql.Logger(log.New(ioutil.Discard, "", log.Ltime)))
156+
// mysql.SetLogger(mysql.Logger(log.New(io.Discard, "", log.Ltime)))
163157

164158
dktesting.ParallelTest(t, specsAnsiQuotes, func(t *testing.T, c dktest.ContainerInfo) {
165159
ip, port, err := c.Port(defaultPort)
@@ -342,7 +336,7 @@ func TestExtractCustomQueryParams(t *testing.T) {
342336
}
343337

344338
func createTmpCert(t *testing.T) string {
345-
tmpCertFile, err := ioutil.TempFile("", "migrate_test_cert")
339+
tmpCertFile, err := os.CreateTemp("", "migrate_test_cert")
346340
if err != nil {
347341
t.Fatal("Failed to create temp cert file:", err)
348342
}

database/neo4j/neo4j.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import (
44
"bytes"
55
"fmt"
66
"io"
7-
"io/ioutil"
87
neturl "net/url"
98
"strconv"
109
"sync/atomic"
@@ -173,7 +172,7 @@ func (n *Neo4j) Run(migration io.Reader) (err error) {
173172
return err
174173
}
175174

176-
body, err := ioutil.ReadAll(migration)
175+
body, err := io.ReadAll(migration)
177176
if err != nil {
178177
return err
179178
}

database/pgx/pgx.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -7,19 +7,19 @@ import (
77
"context"
88
"database/sql"
99
"fmt"
10-
"go.uber.org/atomic"
1110
"io"
12-
"io/ioutil"
1311
nurl "net/url"
1412
"regexp"
1513
"strconv"
1614
"strings"
1715
"time"
1816

17+
"go.uber.org/atomic"
18+
1919
"github.com/golang-migrate/migrate/v4"
2020
"github.com/golang-migrate/migrate/v4/database"
2121
"github.com/golang-migrate/migrate/v4/database/multistmt"
22-
multierror "github.com/hashicorp/go-multierror"
22+
"github.com/hashicorp/go-multierror"
2323
"github.com/jackc/pgconn"
2424
"github.com/jackc/pgerrcode"
2525
_ "github.com/jackc/pgx/v4/stdlib"
@@ -265,7 +265,7 @@ func (p *Postgres) Run(migration io.Reader) error {
265265
}
266266
return err
267267
}
268-
migr, err := ioutil.ReadAll(migration)
268+
migr, err := io.ReadAll(migration)
269269
if err != nil {
270270
return err
271271
}

database/postgres/postgres.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import (
88
"database/sql"
99
"fmt"
1010
"io"
11-
"io/ioutil"
1211
nurl "net/url"
1312
"regexp"
1413
"strconv"
@@ -278,7 +277,7 @@ func (p *Postgres) Run(migration io.Reader) error {
278277
}
279278
return err
280279
}
281-
migr, err := ioutil.ReadAll(migration)
280+
migr, err := io.ReadAll(migration)
282281
if err != nil {
283282
return err
284283
}

database/ql/ql.go

+4-5
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,12 @@ package ql
33
import (
44
"database/sql"
55
"fmt"
6-
"github.com/hashicorp/go-multierror"
7-
"go.uber.org/atomic"
86
"io"
9-
"io/ioutil"
7+
nurl "net/url"
108
"strings"
119

12-
nurl "net/url"
10+
"github.com/hashicorp/go-multierror"
11+
"go.uber.org/atomic"
1312

1413
"github.com/golang-migrate/migrate/v4"
1514
"github.com/golang-migrate/migrate/v4/database"
@@ -179,7 +178,7 @@ func (m *Ql) Unlock() error {
179178
return nil
180179
}
181180
func (m *Ql) Run(migration io.Reader) error {
182-
migr, err := ioutil.ReadAll(migration)
181+
migr, err := io.ReadAll(migration)
183182
if err != nil {
184183
return err
185184
}

database/redshift/redshift.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,13 @@ import (
77
"context"
88
"database/sql"
99
"fmt"
10-
"go.uber.org/atomic"
1110
"io"
12-
"io/ioutil"
1311
nurl "net/url"
1412
"strconv"
1513
"strings"
1614

15+
"go.uber.org/atomic"
16+
1717
"github.com/golang-migrate/migrate/v4"
1818
"github.com/golang-migrate/migrate/v4/database"
1919
"github.com/hashicorp/go-multierror"
@@ -142,7 +142,7 @@ func (p *Redshift) Unlock() error {
142142
}
143143

144144
func (p *Redshift) Run(migration io.Reader) error {
145-
migr, err := ioutil.ReadAll(migration)
145+
migr, err := io.ReadAll(migration)
146146
if err != nil {
147147
return err
148148
}

0 commit comments

Comments
 (0)