Skip to content

Commit

Permalink
Removed io/ioutil package as it is deprecated and used io and os pack…
Browse files Browse the repository at this point in the history
…age (#804)
  • Loading branch information
LikithaVemulapalli authored Apr 10, 2023
1 parent ffede62 commit 5c44f5a
Show file tree
Hide file tree
Showing 7 changed files with 17 additions and 18 deletions.
6 changes: 3 additions & 3 deletions pkg/ec2metadata/ec2metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ package ec2metadata
import (
"encoding/json"
"fmt"
"io/ioutil"
"io"
"math/rand"
"net/http"
"strconv"
Expand Down Expand Up @@ -202,7 +202,7 @@ func (e *Service) GetMetadataInfo(path string) (info string, err error) {
}
if resp != nil {
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
body, err := io.ReadAll(resp.Body)
if err != nil {
return "", fmt.Errorf("Unable to parse http response. Status code: %d. %w", resp.StatusCode, err)
}
Expand Down Expand Up @@ -284,7 +284,7 @@ func (e *Service) getV2Token() (string, int, error) {
if resp.StatusCode < 200 || resp.StatusCode >= 300 {
return "", -1, fmt.Errorf("Received an http status code %d", resp.StatusCode)
}
token, err := ioutil.ReadAll(resp.Body)
token, err := io.ReadAll(resp.Body)
if err != nil {
return "", -1, fmt.Errorf("Unable to read token response from IMDSv2: %w", err)
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/ec2metadata/ec2metadata_internal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ package ec2metadata
import (
"bytes"
"errors"
"io/ioutil"
"io"
"net/http"
"net/http/httptest"
"strconv"
Expand All @@ -35,7 +35,7 @@ func TestRetry(t *testing.T) {
requestCount++
return &http.Response{
StatusCode: 400,
Body: ioutil.NopCloser(bytes.NewBufferString(`OK`)),
Body: io.NopCloser(bytes.NewBufferString(`OK`)),
Header: make(http.Header),
}, errors.New(errorMsg)
}
Expand Down
6 changes: 3 additions & 3 deletions pkg/ec2metadata/ec2metadata_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ package ec2metadata_test

import (
"fmt"
"io/ioutil"
"io"
"net/http"
"net/http/httptest"
"testing"
Expand Down Expand Up @@ -46,7 +46,7 @@ func TestRequestV1(t *testing.T) {
defer resp.Body.Close()
h.Equals(t, http.StatusOK, resp.StatusCode)

responseData, err := ioutil.ReadAll(resp.Body)
responseData, err := io.ReadAll(resp.Body)
if err != nil {
t.Error("Unable to parse response.")
}
Expand Down Expand Up @@ -80,7 +80,7 @@ func TestRequestV2(t *testing.T) {
defer resp.Body.Close()
h.Equals(t, http.StatusOK, resp.StatusCode)

responseData, err := ioutil.ReadAll(resp.Body)
responseData, err := io.ReadAll(resp.Body)
if err != nil {
t.Error("Unable to parse response.")
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/uptime/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ package uptime

import (
"fmt"
"io/ioutil"
"os"
"strconv"
"strings"
)
Expand All @@ -26,7 +26,7 @@ type UptimeFuncType func() (int64, error)
// UptimeFromFile reads system uptime information from filepath and returns
// the number of seconds since last system boot.
func UptimeFromFile(filepath string) (int64, error) {
data, err := ioutil.ReadFile(filepath)
data, err := os.ReadFile(filepath)
if err != nil {
return 0, fmt.Errorf("Not able to read %s: %w", filepath, err)
}
Expand Down
5 changes: 2 additions & 3 deletions pkg/uptime/common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
package uptime

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

Expand All @@ -25,7 +24,7 @@ const testFile = "test.out"

func TestUptimeFromFileSuccess(t *testing.T) {
d1 := []byte("350735.47 234388.90")
err := ioutil.WriteFile(testFile, d1, 0644)
err := os.WriteFile(testFile, d1, 0644)
h.Ok(t, err)

value, err := UptimeFromFile(testFile)
Expand All @@ -41,7 +40,7 @@ func TestUptimeFromFileReadFail(t *testing.T) {

func TestUptimeFromFileBadData(t *testing.T) {
d1 := []byte("Something not time")
err := ioutil.WriteFile(testFile, d1, 0644)
err := os.WriteFile(testFile, d1, 0644)
h.Ok(t, err)

_, err = UptimeFromFile(testFile)
Expand Down
6 changes: 3 additions & 3 deletions pkg/webhook/webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"net/url"
"os"
"text/template"
"time"

Expand All @@ -41,7 +41,7 @@ func Post(additionalInfo ec2metadata.NodeMetadata, event *monitor.InterruptionEv
var webhookTemplateContent string

if nthConfig.WebhookTemplateFile != "" {
content, err := ioutil.ReadFile(nthConfig.WebhookTemplateFile)
content, err := os.ReadFile(nthConfig.WebhookTemplateFile)
if err != nil {
log.Err(err).
Str("webhook_template_file", nthConfig.WebhookTemplateFile).
Expand Down Expand Up @@ -131,7 +131,7 @@ func ValidateWebhookConfig(nthConfig config.Config) error {
var webhookTemplateContent string

if nthConfig.WebhookTemplateFile != "" {
content, err := ioutil.ReadFile(nthConfig.WebhookTemplateFile)
content, err := os.ReadFile(nthConfig.WebhookTemplateFile)
if err != nil {
return fmt.Errorf("Webhook Error: Could not read template file %w", err)
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/webhook/webhook_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"io"
"net/http"
"net/http/httptest"
"testing"
Expand Down Expand Up @@ -88,7 +88,7 @@ func TestPostSuccess(t *testing.T) {
h.Equals(t, req.Header.Get("Content-type"), headerMap["Content-type"])

// Test request body
requestBody, err := ioutil.ReadAll(req.Body)
requestBody, err := io.ReadAll(req.Body)
if err != nil {
t.Error("Unable to read request body.")
}
Expand Down

0 comments on commit 5c44f5a

Please sign in to comment.