Skip to content

Commit

Permalink
refactor: remove io/ioutil dependency
Browse files Browse the repository at this point in the history
io/ioutil has been deprecated since Go 1.16
https://pkg.go.dev/io/ioutil

Signed-off-by: Gavin Inglis <giinglis@amazon.com>
  • Loading branch information
ginglis13 committed Oct 13, 2023
1 parent c53ff19 commit 054d7ac
Show file tree
Hide file tree
Showing 17 changed files with 74 additions and 80 deletions.
23 changes: 11 additions & 12 deletions commands/profile_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ package commands
import (
"errors"
"fmt"
"io/ioutil"
"opensearch-cli/controller/profile/mocks"
"opensearch-cli/entity"
"os"
Expand Down Expand Up @@ -95,7 +94,7 @@ func TestCreateProfile(t *testing.T) {
assert.EqualErrorf(t, err, "required flag(s) \"auth-type\", \"endpoint\", \"name\" not set", "unexpected error")
})
t.Run("create security disabled profile", func(t *testing.T) {
f, err := ioutil.TempFile("", "profile")
f, err := os.CreateTemp("", "profile")
assert.NoError(t, err)
defer func() {
err := os.Remove(f.Name())
Expand All @@ -116,7 +115,7 @@ func TestCreateProfile(t *testing.T) {
})
_, err = root.ExecuteC()
assert.NoError(t, err)
contents, _ := ioutil.ReadFile(f.Name())
contents, _ := os.ReadFile(f.Name())
var actual entity.Config
assert.NoError(t, yaml.Unmarshal(contents, &actual))
retryVal := 2
Expand All @@ -135,7 +134,7 @@ func TestCreateProfile(t *testing.T) {

func TestDeleteProfileCommand(t *testing.T) {
t.Run("test delete profile command", func(t *testing.T) {
f, err := ioutil.TempFile("", "profile-delete")
f, err := os.CreateTemp("", "profile-delete")
assert.NoError(t, err)
defer func() {
err := os.Remove(f.Name())
Expand All @@ -144,7 +143,7 @@ func TestDeleteProfileCommand(t *testing.T) {
config := entity.Config{Profiles: []entity.Profile{fakeInputProfile()}}
bytes, err := yaml.Marshal(config)
assert.NoError(t, err)
assert.NoError(t, ioutil.WriteFile(f.Name(), bytes, 0644))
assert.NoError(t, os.WriteFile(f.Name(), bytes, 0644))
assert.NoError(t, f.Sync())
root := GetRoot()
assert.NotNil(t, root)
Expand All @@ -155,7 +154,7 @@ func TestDeleteProfileCommand(t *testing.T) {
assert.NoError(t, err)
assert.EqualValues(t, expected, f.Name())
var expectedConfig entity.Config
contents, err := ioutil.ReadFile(f.Name())
contents, err := os.ReadFile(f.Name())
assert.NoError(t, err)
err = yaml.Unmarshal(contents, &expectedConfig)
assert.NoError(t, err)
Expand All @@ -165,7 +164,7 @@ func TestDeleteProfileCommand(t *testing.T) {

func TestListsProfileCommand(t *testing.T) {
t.Run("list profiles", func(t *testing.T) {
f, err := ioutil.TempFile("", "profile-list")
f, err := os.CreateTemp("", "profile-list")
assert.NoError(t, err)
defer func() {
err := os.Remove(f.Name())
Expand All @@ -174,7 +173,7 @@ func TestListsProfileCommand(t *testing.T) {
config := entity.Config{Profiles: []entity.Profile{fakeInputProfile()}}
bytes, err := yaml.Marshal(config)
assert.NoError(t, err)
assert.NoError(t, ioutil.WriteFile(f.Name(), bytes, 0644))
assert.NoError(t, os.WriteFile(f.Name(), bytes, 0644))
assert.NoError(t, f.Sync())
root := GetRoot()
assert.NotNil(t, root)
Expand All @@ -186,7 +185,7 @@ func TestListsProfileCommand(t *testing.T) {
assert.EqualValues(t, expected, f.Name())
})
t.Run("list profiles with verbose", func(t *testing.T) {
f, err := ioutil.TempFile("", "profile-list")
f, err := os.CreateTemp("", "profile-list")
assert.NoError(t, err)
defer func() {
err := os.Remove(f.Name())
Expand All @@ -195,7 +194,7 @@ func TestListsProfileCommand(t *testing.T) {
config := entity.Config{Profiles: []entity.Profile{fakeInputProfile()}}
bytes, err := yaml.Marshal(config)
assert.NoError(t, err)
assert.NoError(t, ioutil.WriteFile(f.Name(), bytes, 0644))
assert.NoError(t, os.WriteFile(f.Name(), bytes, 0644))
assert.NoError(t, f.Sync())
root := GetRoot()
assert.NotNil(t, root)
Expand All @@ -207,7 +206,7 @@ func TestListsProfileCommand(t *testing.T) {
assert.EqualValues(t, expected, f.Name())
})
t.Run("no profiles found", func(t *testing.T) {
f, err := ioutil.TempFile("", "profile")
f, err := os.CreateTemp("", "profile")
assert.NoError(t, err)
defer func() {
err := os.Remove(f.Name())
Expand All @@ -216,7 +215,7 @@ func TestListsProfileCommand(t *testing.T) {
config := entity.Config{Profiles: []entity.Profile{}}
bytes, err := yaml.Marshal(config)
assert.NoError(t, err)
assert.NoError(t, ioutil.WriteFile(f.Name(), bytes, 0644))
assert.NoError(t, os.WriteFile(f.Name(), bytes, 0644))
assert.NoError(t, f.Sync())
root := GetRoot()
assert.NotNil(t, root)
Expand Down
5 changes: 2 additions & 3 deletions commands/root_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
package commands

import (
"io/ioutil"
"opensearch-cli/entity"
"os"
"path/filepath"
Expand Down Expand Up @@ -65,11 +64,11 @@ func TestVersionString(t *testing.T) {
}

func createTempConfigFile(testFilePath string) (*os.File, error) {
content, err := ioutil.ReadFile(testFilePath)
content, err := os.ReadFile(testFilePath)
if err != nil {
return nil, err
}
tmpfile, err := ioutil.TempFile(os.TempDir(), "test-file")
tmpfile, err := os.CreateTemp(os.TempDir(), "test-file")
if err != nil {
return nil, err
}
Expand Down
3 changes: 1 addition & 2 deletions controller/ad/ad_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ import (
"encoding/json"
"errors"
"fmt"
"io/ioutil"
mockController "opensearch-cli/controller/platform/mocks"
entity "opensearch-cli/entity/ad"
gateway "opensearch-cli/gateway/ad/mocks"
Expand All @@ -35,7 +34,7 @@ const mockDetectorName = "detector"

func helperLoadBytes(t *testing.T, name string) []byte {
path := filepath.Join("testdata", name) // relative path
contents, err := ioutil.ReadFile(path)
contents, err := os.ReadFile(path)
if err != nil {
t.Fatal(err)
}
Expand Down
9 changes: 4 additions & 5 deletions controller/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
package config

import (
"io/ioutil"
"opensearch-cli/entity"
"os"

Expand All @@ -29,17 +28,17 @@ type controller struct {
path string
}

//Read deserialize config file into entity.Config
// Read deserialize config file into entity.Config
func (c controller) Read() (result entity.Config, err error) {
contents, err := ioutil.ReadFile(c.path)
contents, err := os.ReadFile(c.path)
if err != nil {
return
}
err = yaml.Unmarshal(contents, &result)
return
}

//Write serialize entity.Config into file path
// Write serialize entity.Config into file path
func (c controller) Write(config entity.Config) (err error) {
file, err := os.Create(c.path) //overwrite if file exists
if err != nil {
Expand All @@ -59,7 +58,7 @@ func (c controller) Write(config entity.Config) (err error) {
return file.Sync()
}

//New returns config controller instance
// New returns config controller instance
func New(path string) Controller {
return controller{
path: path,
Expand Down
5 changes: 2 additions & 3 deletions controller/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ package config

import (
"fmt"
"io/ioutil"
"opensearch-cli/entity"
"os"
"path/filepath"
Expand Down Expand Up @@ -60,7 +59,7 @@ func TestControllerRead(t *testing.T) {
}
func TestControllerWrite(t *testing.T) {
t.Run("success", func(t *testing.T) {
f, err := ioutil.TempFile("", "config")
f, err := os.CreateTemp("", "config")
assert.NoError(t, err)
defer func() {
err = os.Remove(f.Name())
Expand All @@ -69,7 +68,7 @@ func TestControllerWrite(t *testing.T) {
ctrl := New(f.Name())
err = ctrl.Write(getSampleConfig())
assert.NoError(t, err)
contents, err := ioutil.ReadFile(f.Name())
contents, err := os.ReadFile(f.Name())
assert.NoError(t, err)
var config entity.Config
err = yaml.Unmarshal(contents, &config)
Expand Down
4 changes: 2 additions & 2 deletions controller/platform/platform_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ package platform
import (
"context"
"errors"
"io/ioutil"
"net/http"
"opensearch-cli/entity/platform"
"opensearch-cli/gateway/platform/mocks"
"os"
"path/filepath"
"testing"

Expand All @@ -27,7 +27,7 @@ import (

func helperLoadBytes(t *testing.T, name string) []byte {
path := filepath.Join("testdata", name) // relative path
bytes, err := ioutil.ReadFile(path)
bytes, err := os.ReadFile(path)
if err != nil {
t.Fatal(err)
}
Expand Down
2 changes: 1 addition & 1 deletion docs/dev/tutorial.md
Original file line number Diff line number Diff line change
Expand Up @@ -309,7 +309,7 @@ func (h *Handler) CreateAnomalyDetector(fileName string) error {
fmt.Println("failed to close json:", err)
}
}()
byteValue, _ := ioutil.ReadAll(jsonFile)
byteValue, _ := io.ReadAll(jsonFile)
var request entity.CreateDetectorRequest
err = json.Unmarshal(byteValue, &request)
if err != nil {
Expand Down
17 changes: 8 additions & 9 deletions entity/platform/request_error.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,17 @@ import (
"encoding/json"
"fmt"
"io"
"io/ioutil"
)

//RequestError contains more information that can be used by client to provide
//better error message
// RequestError contains more information that can be used by client to provide
// better error message
type RequestError struct {
statusCode int
err error
response []byte
}

//NewRequestError builds RequestError
// NewRequestError builds RequestError
func NewRequestError(statusCode int, body io.ReadCloser, err error) *RequestError {
return &RequestError{
statusCode: statusCode,
Expand All @@ -35,17 +34,17 @@ func NewRequestError(statusCode int, body io.ReadCloser, err error) *RequestErro
}
}

//Error inherits error interface to pass as error
// Error inherits error interface to pass as error
func (r *RequestError) Error() string {
return r.err.Error()
}

//StatusCode to get response's status code
// StatusCode to get response's status code
func (r *RequestError) StatusCode() int {
return r.statusCode
}

//GetResponse to get error response from OpenSearch
// GetResponse to get error response from OpenSearch
func (r *RequestError) GetResponse() string {
var data map[string]interface{}
if err := json.Unmarshal(r.response, &data); err != nil {
Expand All @@ -55,9 +54,9 @@ func (r *RequestError) GetResponse() string {
return string(formattedResponse)
}

//getResponseBody to extract response body from OpenSearch server
// getResponseBody to extract response body from OpenSearch server
func getResponseBody(b io.Reader) []byte {
resBytes, err := ioutil.ReadAll(b)
resBytes, err := io.ReadAll(b)
if err != nil {
fmt.Println("failed to read response")
}
Expand Down
15 changes: 8 additions & 7 deletions gateway/ad/ad_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,13 @@ import (
"bytes"
"context"
"encoding/json"
"io/ioutil"
"io"
"net/http"
"opensearch-cli/client"
"opensearch-cli/client/mocks"
"opensearch-cli/entity"
"opensearch-cli/entity/ad"
"os"
"path/filepath"
"testing"

Expand All @@ -29,7 +30,7 @@ import (

func helperLoadBytes(t *testing.T, name string) []byte {
path := filepath.Join("testdata", name) // relative path
contents, err := ioutil.ReadFile(path)
contents, err := os.ReadFile(path)
if err != nil {
t.Fatal(err)
}
Expand All @@ -45,7 +46,7 @@ func getTestClient(t *testing.T, response string, code int, method string, actio
return &http.Response{
StatusCode: code,
// Send response to be tested
Body: ioutil.NopCloser(bytes.NewBufferString(response)),
Body: io.NopCloser(bytes.NewBufferString(response)),
// Must be set to non-nil value or it panics
Header: make(http.Header),
Status: "SOME OUTPUT",
Expand Down Expand Up @@ -231,7 +232,7 @@ func getSearchClient(t *testing.T, responseData []byte, code int) *client.Client
// Test request parameters
assert.Equal(t, req.URL.String(), "http://localhost:9200/_plugins/_anomaly_detection/detectors/_search")
assert.EqualValues(t, req.Method, http.MethodPost)
resBytes, _ := ioutil.ReadAll(req.Body)
resBytes, _ := io.ReadAll(req.Body)
var body ad.SearchRequest
err := json.Unmarshal(resBytes, &body)
assert.NoError(t, err)
Expand All @@ -240,7 +241,7 @@ func getSearchClient(t *testing.T, responseData []byte, code int) *client.Client
return &http.Response{
StatusCode: code,
// Send response to be tested
Body: ioutil.NopCloser(bytes.NewBufferString(string(responseData))),
Body: io.NopCloser(bytes.NewBufferString(string(responseData))),
// Must be set to non-nil value or it panics
Header: make(http.Header),
Status: "SOME OUTPUT",
Expand Down Expand Up @@ -288,7 +289,7 @@ func getCreateClient(t *testing.T, responseData []byte, code int) *client.Client
// Test request parameters
assert.Equal(t, req.URL.String(), "http://localhost:9200/_plugins/_anomaly_detection/detectors")
assert.EqualValues(t, req.Method, http.MethodPost)
resBytes, _ := ioutil.ReadAll(req.Body)
resBytes, _ := io.ReadAll(req.Body)
var body ad.CreateDetector
err := json.Unmarshal(resBytes, &body)
assert.NoError(t, err)
Expand All @@ -297,7 +298,7 @@ func getCreateClient(t *testing.T, responseData []byte, code int) *client.Client
return &http.Response{
StatusCode: code,
// Send response to be tested
Body: ioutil.NopCloser(bytes.NewBufferString(string(responseData))),
Body: io.NopCloser(bytes.NewBufferString(string(responseData))),
// Must be set to non-nil value or it panics
Header: make(http.Header),
Status: "SOME OUTPUT",
Expand Down
5 changes: 2 additions & 3 deletions gateway/gateway.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import (
"errors"
"fmt"
"io"
"io/ioutil"
"mime/multipart"
"net/http"
"net/url"
Expand Down Expand Up @@ -61,7 +60,7 @@ func GetTLSConfig(trust *entity.Trust) (*tls.Config, error) {
}
caCertPool := x509.NewCertPool()
if trust.CAFilePath != nil {
caCert, err := ioutil.ReadFile(*trust.CAFilePath)
caCert, err := os.ReadFile(*trust.CAFilePath)
if err != nil {
return nil, fmt.Errorf("error opening certificate file %s, error: %s", *trust.CAFilePath, err)
}
Expand Down Expand Up @@ -164,7 +163,7 @@ func (g *HTTPGateway) Execute(req *retryablehttp.Request) ([]byte, error) {
if err = g.isValidResponse(response); err != nil {
return nil, err
}
return ioutil.ReadAll(response.Body)
return io.ReadAll(response.Body)
}

// Call calls request using http and return error if status code is not expected
Expand Down
Loading

0 comments on commit 054d7ac

Please sign in to comment.