Skip to content

Commit

Permalink
Migrate from the deprecated io/ioutil package (#942)
Browse files Browse the repository at this point in the history
  • Loading branch information
reugn committed Feb 7, 2023
1 parent 718a631 commit fa4662c
Show file tree
Hide file tree
Showing 20 changed files with 43 additions and 49 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/project.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
go-version: [1.15, 1.16, 1.17, 1.18, 1.19]
go-version: [1.16, 1.17, 1.18, 1.19]
steps:
- name: clean docker cache
run: |
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ CGo based library.

> **Note**:
>
> While this library should work with Golang versions as early as 1.15, any bugs specific to versions earlier than 1.18 may not be fixed.
> While this library should work with Golang versions as early as 1.16, any bugs specific to versions earlier than 1.18 may not be fixed.
## Status

Expand Down
5 changes: 2 additions & 3 deletions integration-tests/license_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
package license_test

import (
"io/ioutil"
"os"
"path/filepath"
"regexp"
Expand Down Expand Up @@ -83,7 +82,7 @@ func TestLicense(t *testing.T) {

switch filepath.Ext(path) {
case ".go":
src, err := ioutil.ReadFile(path)
src, err := os.ReadFile(path)
if err != nil {
return nil
}
Expand All @@ -96,7 +95,7 @@ func TestLicense(t *testing.T) {
case ".yaml":
fallthrough
case ".conf":
src, err := ioutil.ReadFile(path)
src, err := os.ReadFile(path)
if err != nil {
return nil
}
Expand Down
4 changes: 2 additions & 2 deletions oauth2/authorization_tokenretriever_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import (
"bytes"
"context"
"encoding/json"
"io/ioutil"
"io"
"net/http"
"strings"
"time"
Expand Down Expand Up @@ -328,7 +328,7 @@ func buildResponse(statusCode int, body interface{}) *http.Response {
resp := &http.Response{
StatusCode: statusCode,
Header: map[string][]string{},
Body: ioutil.NopCloser(bytes.NewReader(b)),
Body: io.NopCloser(bytes.NewReader(b)),
}
if strings.HasPrefix(string(b), "{") {
resp.Header.Add("Content-Type", "application/json")
Expand Down
6 changes: 3 additions & 3 deletions oauth2/client_credentials_provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ package oauth2

import (
"encoding/json"
"io/ioutil"
"os"
"strings"
)

Expand Down Expand Up @@ -54,7 +54,7 @@ func (k *KeyFileProvider) GetClientCredentials() (*KeyFile, error) {
switch {
case strings.HasPrefix(k.KeyFile, FILE):
filename := strings.TrimPrefix(k.KeyFile, FILE)
keyFile, err = ioutil.ReadFile(filename)
keyFile, err = os.ReadFile(filename)
case strings.HasPrefix(k.KeyFile, DATA):
keyFile = []byte(strings.TrimPrefix(k.KeyFile, DATA))
case strings.HasPrefix(k.KeyFile, "data:"):
Expand All @@ -64,7 +64,7 @@ func (k *KeyFileProvider) GetClientCredentials() (*KeyFile, error) {
}
keyFile = url.Data
default:
keyFile, err = ioutil.ReadFile(k.KeyFile)
keyFile, err = os.ReadFile(k.KeyFile)
}
if err != nil {
return nil, err
Expand Down
3 changes: 1 addition & 2 deletions oauth2/client_credentials_provider_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import (
"encoding/base64"
"encoding/json"
"fmt"
"io/ioutil"
"os"
"testing"

Expand All @@ -45,7 +44,7 @@ func TestNewClientCredentialsProviderFromKeyFile(t *testing.T) {

b, err := json.Marshal(keyFile)
require.NoError(t, err)
tmpFile, err := ioutil.TempFile("", "key-file")
tmpFile, err := os.CreateTemp("", "key-file")
require.NoError(t, err)
defer func(name string) {
_ = os.Remove(name)
Expand Down
3 changes: 1 addition & 2 deletions perf/pulsar-perf-go.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ package main
import (
"context"
"fmt"
"io/ioutil"
"net/http"
_ "net/http/pprof"
"os"
Expand Down Expand Up @@ -57,7 +56,7 @@ func NewClient() (pulsar.Client, error) {

if clientArgs.TokenFile != "" {
// read JWT from the file
tokenBytes, err := ioutil.ReadFile(clientArgs.TokenFile)
tokenBytes, err := os.ReadFile(clientArgs.TokenFile)
if err != nil {
log.WithError(err).Errorf("failed to read Pulsar JWT from a file %s", clientArgs.TokenFile)
os.Exit(1)
Expand Down
4 changes: 2 additions & 2 deletions pulsar/auth/athenz.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ import (
"crypto/tls"
"encoding/base64"
"errors"
"io/ioutil"
"net/http"
"os"
"regexp"
"strings"
"time"
Expand Down Expand Up @@ -117,7 +117,7 @@ func (p *athenzAuthProvider) Init() error {
}
keyData = key
} else if uriSt.Scheme == "file" {
key, err := ioutil.ReadFile(uriSt.Path)
key, err := os.ReadFile(uriSt.Path)
if err != nil {
return err
}
Expand Down
4 changes: 2 additions & 2 deletions pulsar/auth/athenz_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ package auth
import (
"bytes"
"errors"
"io/ioutil"
"os"
"testing"
"time"

Expand Down Expand Up @@ -69,7 +69,7 @@ func (m *MockRoleToken) RoleTokenValue() (string, error) {

func MockZmsNewTokenBuilder(domain, name string, privateKeyPEM []byte, keyVersion string) (zms.TokenBuilder, error) {
// assertion
key, err := ioutil.ReadFile(tlsClientKeyPath)
key, err := os.ReadFile(tlsClientKeyPath)
if err != nil {
return nil, err
}
Expand Down
4 changes: 2 additions & 2 deletions pulsar/auth/basic_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ package auth

import (
"errors"
"io/ioutil"
"io"
"net/http"
"net/http/httptest"
"testing"
Expand Down Expand Up @@ -51,7 +51,7 @@ func TestNewAuthenticationBasicWithParams(t *testing.T) {
resp, err := client.Get(s.URL)
require.NoError(t, err)

body, err := ioutil.ReadAll(resp.Body)
body, err := io.ReadAll(resp.Body)
_ = resp.Body.Close()
require.NoError(t, err)
require.Equal(t, []byte("Basic YWRtaW46MTIzNDU2"), body)
Expand Down
3 changes: 1 addition & 2 deletions pulsar/auth/oauth2_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ package auth

import (
"fmt"
"io/ioutil"
"net/http"
"net/http/httptest"
"os"
Expand Down Expand Up @@ -64,7 +63,7 @@ func mockKeyFile(server string) (string, error) {
if err != nil {
return "", err
}
kf, err := ioutil.TempFile(pwd, "test_oauth2")
kf, err := os.CreateTemp(pwd, "test_oauth2")
if err != nil {
return "", err
}
Expand Down
4 changes: 2 additions & 2 deletions pulsar/auth/token.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ package auth
import (
"crypto/tls"
"fmt"
"io/ioutil"
"net/http"
"os"
"strings"

"github.com/pkg/errors"
Expand Down Expand Up @@ -69,7 +69,7 @@ func NewAuthenticationTokenFromSupplier(tokenSupplier func() (string, error)) Pr
func NewAuthenticationTokenFromFile(tokenFilePath string) Provider {
return &tokenAuthProvider{
tokenSupplier: func() (string, error) {
data, err := ioutil.ReadFile(tokenFilePath)
data, err := os.ReadFile(tokenFilePath)
if err != nil {
return "", err
}
Expand Down
11 changes: 5 additions & 6 deletions pulsar/client_impl_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import (
"context"
"crypto/tls"
"fmt"
"io/ioutil"
"net/http"
"net/http/httptest"
"os"
Expand Down Expand Up @@ -189,7 +188,7 @@ func TestTLSAuthWithCertSupplier(t *testing.T) {
}

func TestTokenAuth(t *testing.T) {
token, err := ioutil.ReadFile(tokenFilePath)
token, err := os.ReadFile(tokenFilePath)
assert.NoError(t, err)

client, err := NewClient(ClientOptions{
Expand All @@ -212,7 +211,7 @@ func TestTokenAuthWithSupplier(t *testing.T) {
client, err := NewClient(ClientOptions{
URL: serviceURL,
Authentication: NewAuthenticationTokenFromSupplier(func() (s string, err error) {
token, err := ioutil.ReadFile(tokenFilePath)
token, err := os.ReadFile(tokenFilePath)
if err != nil {
return "", err
}
Expand Down Expand Up @@ -287,7 +286,7 @@ func mockKeyFile(server string) (string, error) {
if err != nil {
return "", err
}
kf, err := ioutil.TempFile(pwd, "test_oauth2")
kf, err := os.CreateTemp(pwd, "test_oauth2")
if err != nil {
return "", err
}
Expand Down Expand Up @@ -838,7 +837,7 @@ func TestHTTPSAuthWithCertSupplier(t *testing.T) {
}

func TestHTTPTokenAuth(t *testing.T) {
token, err := ioutil.ReadFile(tokenFilePath)
token, err := os.ReadFile(tokenFilePath)
assert.NoError(t, err)

client, err := NewClient(ClientOptions{
Expand All @@ -861,7 +860,7 @@ func TestHTTPTokenAuthWithSupplier(t *testing.T) {
client, err := NewClient(ClientOptions{
URL: webServiceURL,
Authentication: NewAuthenticationTokenFromSupplier(func() (s string, err error) {
token, err := ioutil.ReadFile(tokenFilePath)
token, err := os.ReadFile(tokenFilePath)
if err != nil {
return "", err
}
Expand Down
4 changes: 2 additions & 2 deletions pulsar/consumer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ import (
"context"
"errors"
"fmt"
"io/ioutil"
"log"
"net/http"
"os"
"strconv"
"sync"
"sync/atomic"
Expand Down Expand Up @@ -3422,7 +3422,7 @@ func (d *EncKeyReader) PrivateKey(keyName string, keyMeta map[string]string) (*c
}

func readKey(keyName, path string, keyMeta map[string]string) (*crypto.EncryptionKeyInfo, error) {
key, err := ioutil.ReadFile(path)
key, err := os.ReadFile(path)
if err != nil {
return nil, err
}
Expand Down
4 changes: 2 additions & 2 deletions pulsar/crypto/default_crypto_Key_reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

package crypto

import "io/ioutil"
import "os"

// FileKeyReader default implementation of KeyReader
type FileKeyReader struct {
Expand All @@ -43,7 +43,7 @@ func (d *FileKeyReader) PrivateKey(keyName string, keyMeta map[string]string) (*
}

func readKey(keyName, path string, keyMeta map[string]string) (*EncryptionKeyInfo, error) {
key, err := ioutil.ReadFile(path)
key, err := os.ReadFile(path)
if err != nil {
return nil, err
}
Expand Down
3 changes: 1 addition & 2 deletions pulsar/helper_for_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import (
"encoding/json"
"fmt"
"io"
"io/ioutil"
"net/http"
"path"
"strings"
Expand Down Expand Up @@ -110,7 +109,7 @@ func httpDo(method string, requestPath string, in interface{}, out interface{})
}

if out != nil {
outBytes, err := ioutil.ReadAll(resp.Body)
outBytes, err := io.ReadAll(resp.Body)
if err != nil {
return err
}
Expand Down
8 changes: 4 additions & 4 deletions pulsar/internal/compression/compression_bench_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
package compression

import (
"io/ioutil"
"os"
"testing"
)

Expand All @@ -27,7 +27,7 @@ const (
)

func testCompression(b *testing.B, provider Provider) {
data, err := ioutil.ReadFile(dataSampleFile)
data, err := os.ReadFile(dataSampleFile)
if err != nil {
b.Error(err)
}
Expand All @@ -45,7 +45,7 @@ func testCompression(b *testing.B, provider Provider) {

func testDecompression(b *testing.B, provider Provider) {
// Read data sample file
data, err := ioutil.ReadFile(dataSampleFile)
data, err := os.ReadFile(dataSampleFile)
if err != nil {
b.Error(err)
}
Expand Down Expand Up @@ -97,7 +97,7 @@ func BenchmarkDecompression(b *testing.B) {
func BenchmarkCompressionParallel(b *testing.B) {
b.ReportAllocs()

data, err := ioutil.ReadFile(dataSampleFile)
data, err := os.ReadFile(dataSampleFile)
if err != nil {
b.Error(err)
}
Expand Down
4 changes: 2 additions & 2 deletions pulsar/internal/connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ import (
"crypto/x509"
"errors"
"fmt"
"io/ioutil"
"net"
"net/url"
"os"
"sync"
"sync/atomic"
"time"
Expand Down Expand Up @@ -979,7 +979,7 @@ func (c *connection) getTLSConfig() (*tls.Config, error) {
}

if c.tlsOptions.TrustCertsFilePath != "" {
caCerts, err := ioutil.ReadFile(c.tlsOptions.TrustCertsFilePath)
caCerts, err := os.ReadFile(c.tlsOptions.TrustCertsFilePath)
if err != nil {
return nil, err
}
Expand Down
Loading

0 comments on commit fa4662c

Please sign in to comment.