Skip to content

Commit

Permalink
refactor: drop io/ioutil package usage due to deprecation
Browse files Browse the repository at this point in the history
  • Loading branch information
nettoclaudio committed Aug 8, 2022
1 parent a3e8c3e commit 81b029f
Show file tree
Hide file tree
Showing 22 changed files with 53 additions and 62 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ jobs:
- uses: actions/checkout@v2
- uses: golangci/golangci-lint-action@v2
with:
version: v1.45.2
version: v1.48.0

integration:
runs-on: ubuntu-latest
Expand Down
4 changes: 2 additions & 2 deletions cmd/plugin/rpaasv2/cmd/blocks.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"encoding/json"
"fmt"
"io"
"io/ioutil"
"os"

"github.com/olekukonko/tablewriter"
rpaasclient "github.com/tsuru/rpaas-operator/pkg/rpaas/client"
Expand Down Expand Up @@ -69,7 +69,7 @@ func runUpdateBlock(c *cli.Context) error {
return err
}

content, err := ioutil.ReadFile(c.Path("content"))
content, err := os.ReadFile(c.Path("content"))
if err != nil {
return err
}
Expand Down
3 changes: 1 addition & 2 deletions cmd/plugin/rpaasv2/cmd/blocks_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ package cmd
import (
"bytes"
"fmt"
"io/ioutil"
"os"
"testing"

Expand All @@ -21,7 +20,7 @@ import (
func TestUpdateBlock(t *testing.T) {
nginxConfig := `# My custom NGINX configuration`

blockFile, err := ioutil.TempFile("", "nginx.*.cfg")
blockFile, err := os.CreateTemp("", "nginx.*.cfg")
require.NoError(t, err)
_, err = blockFile.Write([]byte(nginxConfig))
require.NoError(t, err)
Expand Down
6 changes: 3 additions & 3 deletions cmd/plugin/rpaasv2/cmd/certificates.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ package cmd
import (
"fmt"
"io"
"io/ioutil"
"os"
"strings"

"github.com/olekukonko/tablewriter"
Expand Down Expand Up @@ -93,12 +93,12 @@ func runUpdateCertificate(c *cli.Context) error {
return err
}

certificate, err := ioutil.ReadFile(c.Path("certificate"))
certificate, err := os.ReadFile(c.Path("certificate"))
if err != nil {
return err
}

key, err := ioutil.ReadFile(c.Path("key"))
key, err := os.ReadFile(c.Path("key"))
if err != nil {
return err
}
Expand Down
5 changes: 2 additions & 3 deletions cmd/plugin/rpaasv2/cmd/certificates_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ package cmd
import (
"bytes"
"fmt"
"io/ioutil"
"os"
"testing"

Expand Down Expand Up @@ -37,14 +36,14 @@ AwEHoUQDQgAEPR3tU2Fta9ktY+6P9G0cWO+0kETA6SFs38GecTyudlHz6xvCdz8q
EKTcWGekdmdDPsHloRNtsiCa697B2O9IFA==
-----END EC PRIVATE KEY-----`

certFile, err := ioutil.TempFile("", "cert.*.pem")
certFile, err := os.CreateTemp("", "cert.*.pem")
require.NoError(t, err)
_, err = certFile.Write([]byte(certPem))
require.NoError(t, err)
require.NoError(t, certFile.Close())
defer os.Remove(certFile.Name())

keyFile, err := ioutil.TempFile("", "key.*.pem")
keyFile, err := os.CreateTemp("", "key.*.pem")
require.NoError(t, err)
_, err = keyFile.Write([]byte(keyPem))
require.NoError(t, err)
Expand Down
9 changes: 4 additions & 5 deletions cmd/plugin/rpaasv2/cmd/extra_files_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ package cmd
import (
"bytes"
"fmt"
"io/ioutil"
"os"
"testing"

Expand Down Expand Up @@ -81,9 +80,9 @@ func TestDeleteExtraFiles(t *testing.T) {
func TestAddExtraFiles(t *testing.T) {
c1 := "content1"
c2 := "content2"
f1, err := ioutil.TempFile("", "f1")
f1, err := os.CreateTemp("", "f1")
require.NoError(t, err)
f2, err := ioutil.TempFile("", "f2")
f2, err := os.CreateTemp("", "f2")
require.NoError(t, err)
_, err = f1.Write([]byte(c1))
require.NoError(t, err)
Expand Down Expand Up @@ -169,9 +168,9 @@ func TestAddExtraFiles(t *testing.T) {
func TestUpdateExtraFiles(t *testing.T) {
c1 := "content1"
c2 := "content2"
f1, err := ioutil.TempFile("", "f1")
f1, err := os.CreateTemp("", "f1")
require.NoError(t, err)
f2, err := ioutil.TempFile("", "f2")
f2, err := os.CreateTemp("", "f2")
require.NoError(t, err)
_, err = f1.Write([]byte(c1))
require.NoError(t, err)
Expand Down
5 changes: 2 additions & 3 deletions cmd/plugin/rpaasv2/cmd/routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"encoding/json"
"fmt"
"io"
"io/ioutil"
"os"
"strings"

Expand Down Expand Up @@ -235,10 +234,10 @@ func fetchContentFile(c *cli.Context) ([]byte, error) {
if contentFile == "" {
return nil, nil
}
content, err := ioutil.ReadFile(contentFile)
content, err := os.ReadFile(contentFile)
if os.IsNotExist(err) &&
strings.HasPrefix(contentFile, "@") {
return ioutil.ReadFile(strings.TrimPrefix(contentFile, "@"))
return os.ReadFile(strings.TrimPrefix(contentFile, "@"))
}

return content, err
Expand Down
3 changes: 1 addition & 2 deletions cmd/plugin/rpaasv2/cmd/routes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ package cmd
import (
"bytes"
"fmt"
"io/ioutil"
"os"
"testing"

Expand Down Expand Up @@ -192,7 +191,7 @@ func TestListRoutes(t *testing.T) {

func TestUpdateRoute(t *testing.T) {
nginxConfig := `# My custom NGINX configuration`
configFile, err := ioutil.TempFile("", "nginx.*.cfg")
configFile, err := os.CreateTemp("", "nginx.*.cfg")
require.NoError(t, err)
_, err = configFile.Write([]byte(nginxConfig))
require.NoError(t, err)
Expand Down
5 changes: 2 additions & 3 deletions internal/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
package config

import (
"io/ioutil"
"os"
"path/filepath"
"regexp"
Expand Down Expand Up @@ -201,10 +200,10 @@ config-deny-patterns:
os.Setenv(k, v)
defer os.Unsetenv(k)
}
dir, err := ioutil.TempDir("", "")
dir, err := os.MkdirTemp("", "")
require.NoError(t, err)
name := filepath.Join(dir, "config.yaml")
err = ioutil.WriteFile(name, []byte(tt.config), 0644)
err = os.WriteFile(name, []byte(tt.config), 0644)
require.NoError(t, err)
defer os.RemoveAll(dir)
os.Args = []string{"test", "--config", name}
Expand Down
4 changes: 2 additions & 2 deletions internal/purge/cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package purge

import (
"fmt"
"io/ioutil"
"io"
"net/http"
"net/http/httptest"
"reflect"
Expand All @@ -20,7 +20,7 @@ import (
)

func bodyContent(rsp *httptest.ResponseRecorder) string {
data, _ := ioutil.ReadAll(rsp.Body)
data, _ := io.ReadAll(rsp.Body)
return strings.TrimSuffix(string(data), "\n")
}

Expand Down
6 changes: 3 additions & 3 deletions pkg/rpaas/client/extra_files_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"context"
"encoding/json"
"fmt"
"io/ioutil"
"io"
"net/http"
"testing"

Expand Down Expand Up @@ -138,7 +138,7 @@ func TestClientThroughTsuru_AddExtraFiles(t *testing.T) {
assert.Equal(t, "Bearer f4k3t0k3n", r.Header.Get("Authorization"))
assert.Contains(t, r.Header.Get("Content-Type"), "multipart/form-data")

body, err := ioutil.ReadAll(r.Body)
body, err := io.ReadAll(r.Body)
require.NoError(t, err)
defer r.Body.Close()
bodyString := string(body)
Expand Down Expand Up @@ -223,7 +223,7 @@ func TestClientThroughTsuru_UpdateExtraFiles(t *testing.T) {
assert.Equal(t, "Bearer f4k3t0k3n", r.Header.Get("Authorization"))
assert.Contains(t, r.Header.Get("Content-Type"), "multipart/form-data")

body, err := ioutil.ReadAll(r.Body)
body, err := io.ReadAll(r.Body)
require.NoError(t, err)
defer r.Body.Close()
bodyString := string(body)
Expand Down
5 changes: 2 additions & 3 deletions pkg/rpaas/client/internal_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (
"encoding/json"
"fmt"
"io"
"io/ioutil"
"net"
"net/http"
"net/url"
Expand Down Expand Up @@ -54,7 +53,7 @@ func (e *ErrUnexpectedStatusCode) Error() string {
}

func newErrUnexpectedStatusCodeFromResponse(r *http.Response) error {
body, err := ioutil.ReadAll(r.Body)
body, err := io.ReadAll(r.Body)
if err != nil {
return err
}
Expand Down Expand Up @@ -251,7 +250,7 @@ func (c *client) formatURLWithQueryString(pathName, instance string, qs url.Valu
}

func unmarshalBody(resp *http.Response, dst interface{}) error {
body, err := ioutil.ReadAll(resp.Body)
body, err := io.ReadAll(resp.Body)
if err != nil {
return err
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/rpaas/client/internal_client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
package client

import (
"io/ioutil"
"io"
"net/http"
"net/http/httptest"
"os"
Expand Down Expand Up @@ -135,7 +135,7 @@ func newClientThroughTsuru(t *testing.T, h http.Handler) (Client, *httptest.Serv
}

func getBody(t *testing.T, r *http.Request) string {
body, err := ioutil.ReadAll(r.Body)
body, err := io.ReadAll(r.Body)
require.NoError(t, err)
defer r.Body.Close()
return string(body)
Expand Down
4 changes: 2 additions & 2 deletions pkg/web/certificate.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"crypto/tls"
"errors"
"fmt"
"io/ioutil"
"io"
"mime"
"net/http"
"net/url"
Expand Down Expand Up @@ -173,7 +173,7 @@ func getValueFromFormOrMultipart(r *http.Request, key string) ([]byte, error) {
}
defer f.Close()

return ioutil.ReadAll(f)
return io.ReadAll(f)
}

return nil, nil
Expand Down
4 changes: 2 additions & 2 deletions pkg/web/extra_files.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ package web

import (
"fmt"
"io/ioutil"
"io"
"mime/multipart"
"net/http"
"net/url"
Expand Down Expand Up @@ -177,7 +177,7 @@ func newRpaasFile(fh *multipart.FileHeader) (file rpaas.File, err error) {
return
}
defer uploaded.Close()
rawContent, err := ioutil.ReadAll(uploaded)
rawContent, err := io.ReadAll(uploaded)
if err != nil {
return
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/web/routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ package web

import (
"fmt"
"io/ioutil"
"io"
"net/http"
"net/url"

Expand Down Expand Up @@ -83,7 +83,7 @@ func formValue(req *http.Request, key string) (string, error) {
return "", fmt.Errorf("content-type is not application form")
}

rawBody, err := ioutil.ReadAll(req.Body)
rawBody, err := io.ReadAll(req.Body)
if err != nil {
return "", err
}
Expand Down
3 changes: 1 addition & 2 deletions pkg/web/service_handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"bytes"
"fmt"
"io"
"io/ioutil"
"net/http"
"strings"

Expand Down Expand Up @@ -228,7 +227,7 @@ func decodeFormParameters(r *http.Request) map[string]interface{} {
Parameters map[string]interface{} `form:"parameters"`
}
newFormDecoder(reader).Decode(&obj)
r.Body = ioutil.NopCloser(&buffer)
r.Body = io.NopCloser(&buffer)
return obj.Parameters
}

Expand Down
4 changes: 2 additions & 2 deletions pkg/web/target/multi-cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import (
"context"
"encoding/base64"
"errors"
"io/ioutil"
"net/http"
"os"
"sync"

"github.com/opentracing/opentracing-go"
Expand Down Expand Up @@ -165,7 +165,7 @@ func (m *multiClusterFactory) readTokenFile(tokenFile string) (string, error) {
return tokenInterface.(string), nil
}

token, err := ioutil.ReadFile(tokenFile)
token, err := os.ReadFile(tokenFile)
if err != nil {
return "", err
}
Expand Down
5 changes: 2 additions & 3 deletions pkg/web/target/multi-cluster_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package target

import (
"context"
"io/ioutil"
"net/http"
"os"
"testing"
Expand All @@ -15,7 +14,7 @@ import (
var ctx = context.Background()

func TestMultiClusterTokenFile(t *testing.T) {
tmpfile, err := ioutil.TempFile("", "example")
tmpfile, err := os.CreateTemp("", "example")
require.NoError(t, err)
target := NewMultiClustersFactory([]config.ClusterConfig{
{
Expand Down Expand Up @@ -43,7 +42,7 @@ func TestMultiClusterTokenFile(t *testing.T) {
}

func TestMultiClusterNoToken(t *testing.T) {
tmpfile, err := ioutil.TempFile("", "example")
tmpfile, err := os.CreateTemp("", "example")
require.NoError(t, err)
target := NewMultiClustersFactory([]config.ClusterConfig{
{
Expand Down
Loading

0 comments on commit 81b029f

Please sign in to comment.