refactor: move from io/ioutil to io and os packages (#5266)

The io/ioutil package has been deprecated as of Go 1.16 [1]. This commit
replaces the existing io/ioutil functions with their new definitions in
io and os packages.

[1]: https://golang.org/doc/go1.16#ioutil
Signed-off-by: Eng Zer Jun <engzerjun@gmail.com>
This commit is contained in:
Eng Zer Jun
2022-10-18 15:47:38 +08:00
committed by GitHub
parent 08b8069647
commit d1fec72a32
45 changed files with 113 additions and 127 deletions

View File

@@ -18,7 +18,7 @@ package fake
import (
"fmt"
"io/ioutil"
"io"
"net/http"
"net/url"
"strings"
@@ -113,7 +113,7 @@ func (d *Devops) DeleteDevOpsProject(projectId string) error {
Header: http.Header{
"Foo": []string{"Bar"},
},
Body: ioutil.NopCloser(strings.NewReader("foo")), // shouldn't be used
Body: io.NopCloser(strings.NewReader("foo")), // shouldn't be used
},
Message: "",
}
@@ -136,7 +136,7 @@ func (d *Devops) GetDevOpsProject(projectId string) (string, error) {
Header: http.Header{
"Foo": []string{"Bar"},
},
Body: ioutil.NopCloser(strings.NewReader("foo")), // shouldn't be used
Body: io.NopCloser(strings.NewReader("foo")), // shouldn't be used
Request: &http.Request{
Method: "",
URL: &url.URL{
@@ -211,7 +211,7 @@ func (d *Devops) SubmitInputStep(projectName, pipelineName, runId, nodeId, stepI
return nil, nil
}
//BranchPipelinne operator interface
// BranchPipelinne operator interface
func (d *Devops) GetBranchPipeline(projectName, pipelineName, branchName string, httpParameters *devops.HttpParameters) (*devops.BranchPipeline, error) {
return nil, nil
}
@@ -283,7 +283,7 @@ func (d *Devops) Validate(scmId string, httpParameters *devops.HttpParameters) (
return nil, nil
}
//Webhook operator interface
// Webhook operator interface
func (d *Devops) GetNotifyCommit(httpParameters *devops.HttpParameters) ([]byte, error) {
return nil, nil
}
@@ -327,7 +327,7 @@ func (d *Devops) UpdateCredentialInProject(projectId string, credential *v1.Secr
Header: http.Header{
"Foo": []string{"Bar"},
},
Body: ioutil.NopCloser(strings.NewReader("foo")), // shouldn't be used
Body: io.NopCloser(strings.NewReader("foo")), // shouldn't be used
Request: &http.Request{
Method: "",
URL: &url.URL{
@@ -365,7 +365,7 @@ func (d *Devops) GetCredentialInProject(projectId, id string) (*devops.Credentia
Header: http.Header{
"Foo": []string{"Bar"},
},
Body: ioutil.NopCloser(strings.NewReader("foo")), // shouldn't be used
Body: io.NopCloser(strings.NewReader("foo")), // shouldn't be used
Request: &http.Request{
Method: "",
URL: &url.URL{
@@ -404,7 +404,7 @@ func (d *Devops) DeleteCredentialInProject(projectId, id string) (string, error)
Header: http.Header{
"Foo": []string{"Bar"},
},
Body: ioutil.NopCloser(strings.NewReader("foo")), // shouldn't be used
Body: io.NopCloser(strings.NewReader("foo")), // shouldn't be used
Request: &http.Request{
Method: "",
URL: &url.URL{
@@ -460,7 +460,7 @@ func (d *Devops) DeleteProjectPipeline(projectId string, pipelineId string) (str
Header: http.Header{
"Foo": []string{"Bar"},
},
Body: ioutil.NopCloser(strings.NewReader("foo")), // shouldn't be used
Body: io.NopCloser(strings.NewReader("foo")), // shouldn't be used
Request: &http.Request{
Method: "",
URL: &url.URL{
@@ -498,7 +498,7 @@ func (d *Devops) UpdateProjectPipeline(projectId string, pipeline *devopsv1alpha
Header: http.Header{
"Foo": []string{"Bar"},
},
Body: ioutil.NopCloser(strings.NewReader("foo")), // shouldn't be used
Body: io.NopCloser(strings.NewReader("foo")), // shouldn't be used
Request: &http.Request{
Method: "",
URL: &url.URL{
@@ -536,7 +536,7 @@ func (d *Devops) GetProjectPipelineConfig(projectId, pipelineId string) (*devops
Header: http.Header{
"Foo": []string{"Bar"},
},
Body: ioutil.NopCloser(strings.NewReader("foo")), // shouldn't be used
Body: io.NopCloser(strings.NewReader("foo")), // shouldn't be used
Request: &http.Request{
Method: "",
URL: &url.URL{

View File

@@ -20,7 +20,6 @@ import (
"errors"
"fmt"
"io"
"io/ioutil"
"log"
"net/http"
"net/url"
@@ -849,7 +848,7 @@ func (j *Jenkins) CheckCron(projectName string, httpParameters *devops.HttpParam
reader = httpParameters.Body
//nolint:ineffassign,staticcheck
cronData, err := ioutil.ReadAll(reader)
cronData, err := io.ReadAll(reader)
err = json.Unmarshal(cronData, cron)
if err != nil {
klog.Error(err)

View File

@@ -21,7 +21,6 @@ import (
"errors"
"fmt"
"io"
"io/ioutil"
"mime/multipart"
"net/http"
"net/url"
@@ -185,7 +184,8 @@ func (r *Requester) SetClient(client *http.Client) *Requester {
return r
}
//Add auth on redirect if required.
// Add auth on redirect if required.
//
//nolint:unused
func (r *Requester) redirectPolicyFunc(req *http.Request, via []*http.Request) error {
if r.BasicAuth != nil {
@@ -448,7 +448,7 @@ func (r *Requester) DoPostForm(ar *APIRequest, responseStruct interface{}, form
func (r *Requester) ReadRawResponse(response *http.Response, responseStruct interface{}) (*http.Response, error) {
defer response.Body.Close()
content, err := ioutil.ReadAll(response.Body)
content, err := io.ReadAll(response.Body)
if err != nil {
return nil, err
}
@@ -478,7 +478,7 @@ func CheckResponse(r *http.Response) error {
}
defer r.Body.Close()
errorResponse := &devops.ErrorResponse{Response: r}
data, err := ioutil.ReadAll(r.Body)
data, err := io.ReadAll(r.Body)
if err == nil && data != nil {
errorResponse.Body = data
errorResponse.Message = string(data)

View File

@@ -18,7 +18,6 @@ import (
"compress/gzip"
"encoding/json"
"io"
"io/ioutil"
"net/http"
"net/url"
"strings"
@@ -64,7 +63,7 @@ func getRespBody(resp *http.Response) ([]byte, error) {
} else {
reader = resp.Body
}
resBody, err := ioutil.ReadAll(reader)
resBody, err := io.ReadAll(reader)
if err != nil {
klog.Error(err)
return nil, err

View File

@@ -18,9 +18,9 @@ package es
import (
"fmt"
"io/ioutil"
"net/http"
"net/http/httptest"
"os"
"testing"
"time"
@@ -158,7 +158,7 @@ func TestOpensearchClient_Search(t *testing.T) {
func mockElasticsearchService(pattern, fakeResp string, fakeCode int) *httptest.Server {
mux := http.NewServeMux()
mux.HandleFunc(pattern, func(res http.ResponseWriter, req *http.Request) {
b, _ := ioutil.ReadFile(fmt.Sprintf("./testdata/%s", fakeResp))
b, _ := os.ReadFile(fmt.Sprintf("./testdata/%s", fakeResp))
res.WriteHeader(fakeCode)
res.Write(b)
})
@@ -166,7 +166,7 @@ func mockElasticsearchService(pattern, fakeResp string, fakeCode int) *httptest.
}
func JsonFromFile(expectedFile string, expectedJsonPtr interface{}) error {
json, err := ioutil.ReadFile(fmt.Sprintf("./testdata/%s", expectedFile))
json, err := os.ReadFile(fmt.Sprintf("./testdata/%s", expectedFile))
if err != nil {
return err
}

View File

@@ -22,7 +22,7 @@ import (
"crypto/tls"
"encoding/json"
"fmt"
"io/ioutil"
"io"
"net/http"
"time"
@@ -81,7 +81,7 @@ func (o *OpenSearch) Search(indices string, body []byte, scroll bool) ([]byte, e
return nil, parseError(response)
}
return ioutil.ReadAll(response.Body)
return io.ReadAll(response.Body)
}
func (o *OpenSearch) Scroll(id string) ([]byte, error) {
@@ -98,7 +98,7 @@ func (o *OpenSearch) Scroll(id string) ([]byte, error) {
return nil, parseError(response)
}
return ioutil.ReadAll(response.Body)
return io.ReadAll(response.Body)
}
func (o *OpenSearch) ClearScroll(scrollId string) {

View File

@@ -22,7 +22,7 @@ import (
"crypto/tls"
"encoding/json"
"fmt"
"io/ioutil"
"io"
"net/http"
"time"
@@ -81,7 +81,7 @@ func (o *OpenSearch) Search(indices string, body []byte, scroll bool) ([]byte, e
return nil, parseError(response)
}
return ioutil.ReadAll(response.Body)
return io.ReadAll(response.Body)
}
func (o *OpenSearch) Scroll(id string) ([]byte, error) {
@@ -98,7 +98,7 @@ func (o *OpenSearch) Scroll(id string) ([]byte, error) {
return nil, parseError(response)
}
return ioutil.ReadAll(response.Body)
return io.ReadAll(response.Body)
}
func (o *OpenSearch) ClearScroll(scrollId string) {

View File

@@ -22,7 +22,7 @@ import (
"crypto/tls"
"encoding/json"
"fmt"
"io/ioutil"
"io"
"net/http"
"time"
@@ -81,7 +81,7 @@ func (e *Elastic) Search(indices string, body []byte, scroll bool) ([]byte, erro
return nil, parseError(response)
}
return ioutil.ReadAll(response.Body)
return io.ReadAll(response.Body)
}
func (e *Elastic) Scroll(id string) ([]byte, error) {
@@ -98,7 +98,7 @@ func (e *Elastic) Scroll(id string) ([]byte, error) {
return nil, parseError(response)
}
return ioutil.ReadAll(response.Body)
return io.ReadAll(response.Body)
}
func (e *Elastic) ClearScroll(scrollId string) {

View File

@@ -22,7 +22,7 @@ import (
"crypto/tls"
"encoding/json"
"fmt"
"io/ioutil"
"io"
"net/http"
"time"
@@ -81,7 +81,7 @@ func (e *Elastic) Search(indices string, body []byte, scroll bool) ([]byte, erro
return nil, parseError(response)
}
return ioutil.ReadAll(response.Body)
return io.ReadAll(response.Body)
}
func (e *Elastic) Scroll(id string) ([]byte, error) {
@@ -98,7 +98,7 @@ func (e *Elastic) Scroll(id string) ([]byte, error) {
return nil, parseError(response)
}
return ioutil.ReadAll(response.Body)
return io.ReadAll(response.Body)
}
func (e *Elastic) ClearScroll(scrollId string) {

View File

@@ -22,7 +22,7 @@ import (
"crypto/tls"
"encoding/json"
"fmt"
"io/ioutil"
"io"
"net/http"
"time"
@@ -82,7 +82,7 @@ func (e *Elastic) Search(indices string, body []byte, scroll bool) ([]byte, erro
return nil, parseError(response)
}
return ioutil.ReadAll(response.Body)
return io.ReadAll(response.Body)
}
func (e *Elastic) Scroll(id string) ([]byte, error) {
@@ -99,7 +99,7 @@ func (e *Elastic) Scroll(id string) ([]byte, error) {
return nil, parseError(response)
}
b, err := ioutil.ReadAll(response.Body)
b, err := io.ReadAll(response.Body)
return b, err
}

View File

@@ -19,7 +19,7 @@ package kiali
import (
"encoding/json"
"fmt"
"io/ioutil"
"io"
"net/http"
"net/url"
"time"
@@ -109,7 +109,7 @@ func (c *Client) authenticate() (*TokenResponse, error) {
if err != nil {
return nil, err
}
body, err := ioutil.ReadAll(resp.Body)
body, err := io.ReadAll(resp.Body)
if err != nil {
return nil, err
}

View File

@@ -19,7 +19,7 @@ package kiali
import (
"bytes"
"encoding/json"
"io/ioutil"
"io"
"net/http"
"reflect"
"testing"
@@ -70,7 +70,7 @@ func TestClient_Get(t *testing.T) {
args: args{url: "http://kiali.istio-system.svc"},
wantResp: &http.Response{
StatusCode: 200,
Body: ioutil.NopCloser(bytes.NewReader([]byte("fake"))),
Body: io.NopCloser(bytes.NewReader([]byte("fake"))),
},
wantErr: false,
},
@@ -89,7 +89,7 @@ func TestClient_Get(t *testing.T) {
args: args{url: "http://kiali.istio-system.svc"},
wantResp: &http.Response{
StatusCode: 200,
Body: ioutil.NopCloser(bytes.NewReader([]byte("fake"))),
Body: io.NopCloser(bytes.NewReader([]byte("fake"))),
},
wantErr: false,
},
@@ -108,7 +108,7 @@ func TestClient_Get(t *testing.T) {
args: args{url: "http://kiali.istio-system.svc"},
wantResp: &http.Response{
StatusCode: 200,
Body: ioutil.NopCloser(bytes.NewReader([]byte("fake"))),
Body: io.NopCloser(bytes.NewReader([]byte("fake"))),
},
wantErr: false,
},

View File

@@ -2,7 +2,7 @@ package kiali
import (
"bytes"
"io/ioutil"
"io"
"net/http"
"net/url"
)
@@ -15,13 +15,13 @@ type MockClient struct {
func (c *MockClient) Do(req *http.Request) (*http.Response, error) {
return &http.Response{
StatusCode: 200,
Body: ioutil.NopCloser(bytes.NewReader([]byte(c.RequestResult))),
Body: io.NopCloser(bytes.NewReader([]byte(c.RequestResult))),
}, nil
}
func (c *MockClient) PostForm(url string, data url.Values) (resp *http.Response, err error) {
return &http.Response{
StatusCode: 200,
Body: ioutil.NopCloser(bytes.NewReader(c.TokenResult)),
Body: io.NopCloser(bytes.NewReader(c.TokenResult)),
}, nil
}

View File

@@ -18,9 +18,9 @@ package elasticsearch
import (
"fmt"
"io/ioutil"
"net/http"
"net/http/httptest"
"os"
"testing"
"time"
@@ -274,7 +274,7 @@ func TestParseToQueryPart(t *testing.T) {
for i, test := range tests {
t.Run(fmt.Sprintf("%d", i), func(t *testing.T) {
expected, err := ioutil.ReadFile(fmt.Sprintf("./testdata/%s", test.expected))
expected, err := os.ReadFile(fmt.Sprintf("./testdata/%s", test.expected))
if err != nil {
t.Fatalf("read expected error, %s", err.Error())
}
@@ -290,7 +290,7 @@ func TestParseToQueryPart(t *testing.T) {
func mockElasticsearchService(pattern, fakeResp string, fakeCode int) *httptest.Server {
mux := http.NewServeMux()
mux.HandleFunc(pattern, func(res http.ResponseWriter, req *http.Request) {
b, _ := ioutil.ReadFile(fmt.Sprintf("./testdata/%s", fakeResp))
b, _ := os.ReadFile(fmt.Sprintf("./testdata/%s", fakeResp))
res.WriteHeader(fakeCode)
_, _ = res.Write(b)
})
@@ -298,7 +298,7 @@ func mockElasticsearchService(pattern, fakeResp string, fakeCode int) *httptest.
}
func JsonFromFile(expectedFile string, expectedJsonPtr interface{}) error {
json, err := ioutil.ReadFile(fmt.Sprintf("./testdata/%s", expectedFile))
json, err := os.ReadFile(fmt.Sprintf("./testdata/%s", expectedFile))
if err != nil {
return err
}

View File

@@ -2,11 +2,10 @@ package metricsserver
import (
"fmt"
"os"
"testing"
"time"
"io/ioutil"
"github.com/google/go-cmp/cmp"
jsoniter "github.com/json-iterator/go"
@@ -514,7 +513,7 @@ func TestGetNamedMetricsOverTime(t *testing.T) {
}
func jsonFromFile(expectedFile string, expectedJsonPtr interface{}) error {
json, err := ioutil.ReadFile(fmt.Sprintf("./testdata/%s", expectedFile))
json, err := os.ReadFile(fmt.Sprintf("./testdata/%s", expectedFile))
if err != nil {
return err
}

View File

@@ -18,9 +18,9 @@ package prometheus
import (
"fmt"
"io/ioutil"
"net/http"
"net/http/httptest"
"os"
"testing"
"time"
@@ -176,14 +176,14 @@ func TestGetMetricLabelSet(t *testing.T) {
func mockPrometheusService(pattern, fakeResp string) *httptest.Server {
mux := http.NewServeMux()
mux.HandleFunc(pattern, func(res http.ResponseWriter, req *http.Request) {
b, _ := ioutil.ReadFile(fmt.Sprintf("./testdata/%s", fakeResp))
b, _ := os.ReadFile(fmt.Sprintf("./testdata/%s", fakeResp))
res.Write(b)
})
return httptest.NewServer(mux)
}
func jsonFromFile(expectedFile string, expectedJsonPtr interface{}) error {
json, err := ioutil.ReadFile(fmt.Sprintf("./testdata/%s", expectedFile))
json, err := os.ReadFile(fmt.Sprintf("./testdata/%s", expectedFile))
if err != nil {
return err
}

View File

@@ -24,7 +24,6 @@ import (
"encoding/json"
"fmt"
"io"
"io/ioutil"
"strings"
"time"
@@ -275,7 +274,7 @@ func ByteArrayToSavedIndex(data []byte) (*SavedIndex, error) {
return nil, err
}
r.Close()
b, err := ioutil.ReadAll(r)
b, err := io.ReadAll(r)
if err != nil && err != io.EOF {
return nil, err

View File

@@ -17,7 +17,6 @@ limitations under the License.
package helmwrapper
import (
"io/ioutil"
"os"
"testing"
@@ -42,7 +41,7 @@ func TestHelmInstall(t *testing.T) {
func TempDir(t *testing.T) string {
t.Helper()
d, err := ioutil.TempDir("", "kubesphere")
d, err := os.MkdirTemp("", "kubesphere")
if err != nil {
t.Fatal(err)
}
@@ -66,7 +65,7 @@ func GenerateChartData(t *testing.T, name string) string {
if err != nil {
t.Fatalf("Error creating chart for test: %v", err)
}
charData, err := ioutil.ReadFile(filename)
charData, err := os.ReadFile(filename)
if err != nil {
t.Fatalf("Error loading chart data %v", err)
}

View File

@@ -19,7 +19,6 @@ package fake
import (
"fmt"
"io"
"io/ioutil"
"github.com/aws/aws-sdk-go/aws/awserr"
"github.com/aws/aws-sdk-go/service/s3"
@@ -66,7 +65,7 @@ func (s *FakeS3) Delete(key string) error {
func (s *FakeS3) Read(key string) ([]byte, error) {
if o, ok := s.Storage[key]; ok && o.Body != nil {
data, err := ioutil.ReadAll(o.Body)
data, err := io.ReadAll(o.Body)
if err != nil {
return nil, err
}