Skip to content

Commit

Permalink
refactor: move from io/ioutil to io and os package
Browse files Browse the repository at this point in the history
The io/ioutil package has been deprecated as of Go 1.16, see
https://golang.org/doc/go1.16#ioutil. This commit replaces the existing
io/ioutil functions with their new definitions in io and os packages.

Signed-off-by: Eng Zer Jun <[email protected]>
  • Loading branch information
Juneezee committed Oct 18, 2021
1 parent c062121 commit d7ab773
Show file tree
Hide file tree
Showing 34 changed files with 82 additions and 93 deletions.
6 changes: 3 additions & 3 deletions pkg/components/standalone_loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ package components
import (
"bufio"
"bytes"
"io/ioutil"
"os"
"path/filepath"
"strings"

Expand Down Expand Up @@ -38,7 +38,7 @@ func NewStandaloneComponents(configuration config.StandaloneConfig) *StandaloneC

// LoadComponents loads dapr components from a given directory.
func (s *StandaloneComponents) LoadComponents() ([]components_v1alpha1.Component, error) {
files, err := ioutil.ReadDir(s.config.ComponentsPath)
files, err := os.ReadDir(s.config.ComponentsPath)
if err != nil {
return nil, err
}
Expand All @@ -63,7 +63,7 @@ func (s *StandaloneComponents) loadComponentsFromFile(filename string) []compone
components := []components_v1alpha1.Component{}
path := filepath.Join(s.config.ComponentsPath, filename)

b, err := ioutil.ReadFile(path)
b, err := os.ReadFile(path)
if err != nil {
log.Warnf("daprd load components error when reading file %s : %s", path, err)
return components
Expand Down
3 changes: 1 addition & 2 deletions pkg/config/configuration.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ package config
import (
"context"
"encoding/json"
"io/ioutil"
"os"
"sort"
"strings"
Expand Down Expand Up @@ -220,7 +219,7 @@ func LoadStandaloneConfiguration(config string) (*Configuration, string, error)
return nil, "", err
}

b, err := ioutil.ReadFile(config)
b, err := os.ReadFile(config)
if err != nil {
return nil, "", err
}
Expand Down
8 changes: 4 additions & 4 deletions pkg/credentials/certchain.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package credentials

import (
"io/ioutil"
"os"
)

const (
Expand All @@ -22,15 +22,15 @@ type CertChain struct {

// LoadFromDisk retruns a CertChain from a given directory.
func LoadFromDisk(rootCertPath, issuerCertPath, issuerKeyPath string) (*CertChain, error) {
rootCert, err := ioutil.ReadFile(rootCertPath)
rootCert, err := os.ReadFile(rootCertPath)
if err != nil {
return nil, err
}
cert, err := ioutil.ReadFile(issuerCertPath)
cert, err := os.ReadFile(issuerCertPath)
if err != nil {
return nil, err
}
key, err := ioutil.ReadFile(issuerKeyPath)
key, err := os.ReadFile(issuerKeyPath)
if err != nil {
return nil, err
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/http/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
"context"
"encoding/json"
"fmt"
"io/ioutil"
"io"
"net"
gohttp "net/http"
"os"
Expand Down Expand Up @@ -2185,7 +2185,7 @@ func (f *fakeHTTPServer) doRequest(basicAuth, method, path string, body []byte,
panic(fmt.Errorf("failed to request: %v", err))
}

bodyBytes, _ := ioutil.ReadAll(res.Body)
bodyBytes, _ := io.ReadAll(res.Body)
defer res.Body.Close()
response := fakeHTTPResponse{
StatusCode: res.StatusCode,
Expand Down
4 changes: 2 additions & 2 deletions pkg/injector/injector.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"context"
"encoding/json"
"fmt"
"io/ioutil"
"io"
"net/http"
"time"

Expand Down Expand Up @@ -172,7 +172,7 @@ func (i *injector) handleRequest(w http.ResponseWriter, r *http.Request) {

var body []byte
if r.Body != nil {
if data, err := ioutil.ReadAll(r.Body); err == nil {
if data, err := io.ReadAll(r.Body); err == nil {
body = data
}
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/injector/injector_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ package injector
import (
"bytes"
"encoding/json"
"io/ioutil"
"io"
"net/http"
"net/http/httptest"
"testing"
Expand Down Expand Up @@ -615,7 +615,7 @@ func TestHandleRequest(t *testing.T) {
assert.Equal(t, tc.expectStatusCode, resp.StatusCode)

if resp.StatusCode == http.StatusOK {
body, err := ioutil.ReadAll(resp.Body)
body, err := io.ReadAll(resp.Body)
assert.NoError(t, err)

var ar v1.AdmissionReview
Expand Down
4 changes: 2 additions & 2 deletions pkg/placement/raft/fsm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ package raft

import (
"bytes"
"io/ioutil"
"io"
"testing"

"github.com/hashicorp/raft"
Expand Down Expand Up @@ -81,7 +81,7 @@ func TestRestore(t *testing.T) {
assert.NoError(t, err)

// act
err = fsm.Restore(ioutil.NopCloser(buf))
err = fsm.Restore(io.NopCloser(buf))

// assert
assert.NoError(t, err)
Expand Down
3 changes: 1 addition & 2 deletions pkg/placement/raft/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ package raft

import (
"io"
"io/ioutil"
"log"

"github.com/hashicorp/go-hclog"
Expand Down Expand Up @@ -84,5 +83,5 @@ func (l *loggerAdapter) StandardLogger(opts *hclog.StandardLoggerOptions) *log.L
}

func (l *loggerAdapter) StandardWriter(opts *hclog.StandardLoggerOptions) io.Writer {
return ioutil.Discard
return io.Discard
}
5 changes: 2 additions & 3 deletions pkg/runtime/pubsub/subscriptions.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package pubsub
import (
"context"
"encoding/json"
"io/ioutil"
"net/http"
"os"
"path/filepath"
Expand Down Expand Up @@ -175,7 +174,7 @@ func DeclarativeSelfHosted(componentsPath string, log logger.Logger) []Subscript
return subs
}

files, err := ioutil.ReadDir(componentsPath)
files, err := os.ReadDir(componentsPath)
if err != nil {
log.Errorf("failed to read subscriptions from path %s: %s", err)
return subs
Expand All @@ -184,7 +183,7 @@ func DeclarativeSelfHosted(componentsPath string, log logger.Logger) []Subscript
for _, f := range files {
if !f.IsDir() {
filePath := filepath.Join(componentsPath, f.Name())
b, err := ioutil.ReadFile(filePath)
b, err := os.ReadFile(filePath)
if err != nil {
log.Errorf("failed to read file %s: %s", filePath, err)
continue
Expand Down
3 changes: 1 addition & 2 deletions pkg/runtime/pubsub/subscriptions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"context"
"encoding/json"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"testing"
Expand Down Expand Up @@ -108,7 +107,7 @@ func testDeclarativeSubscriptionV2() subscriptionsapi_v2alpha1.Subscription {

func writeSubscriptionToDisk(subscription interface{}, filePath string) {
b, _ := yaml.Marshal(subscription)
ioutil.WriteFile(filePath, b, 0600)
os.WriteFile(filePath, b, 0600)
}

func TestDeclarativeSubscriptionsV1(t *testing.T) {
Expand Down
3 changes: 1 addition & 2 deletions pkg/runtime/runtime_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (
"encoding/hex"
"encoding/json"
"fmt"
"io/ioutil"
"net"
"net/http"
"net/http/httptest"
Expand Down Expand Up @@ -207,7 +206,7 @@ func testDeclarativeSubscription() subscriptionsapi.Subscription {

func writeSubscriptionToDisk(subscription subscriptionsapi.Subscription, filePath string) {
b, _ := yaml.Marshal(subscription)
ioutil.WriteFile(filePath, b, 0600)
os.WriteFile(filePath, b, 0600)
}

func TestProcessComponentsAndDependents(t *testing.T) {
Expand Down
3 changes: 1 addition & 2 deletions pkg/runtime/security/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"context"
"crypto/x509"
"encoding/pem"
"io/ioutil"
"os"
"sync"
"time"
Expand Down Expand Up @@ -155,7 +154,7 @@ func (a *authenticator) CreateSignedWorkloadCert(id, namespace, trustDomain stri

// currently we support Kubernetes identities.
func getToken() string {
b, _ := ioutil.ReadFile(kubeTknPath)
b, _ := os.ReadFile(kubeTknPath)
return string(b)
}

Expand Down
4 changes: 2 additions & 2 deletions pkg/runtime/wait.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ package runtime

import (
"fmt"
"io/ioutil"
"io"
"net/http"
"time"
)
Expand Down Expand Up @@ -59,7 +59,7 @@ func checkIfOutboundReady(client *http.Client, outboundReadyHealthURL string) er
return err
}
defer func() { _ = resp.Body.Close() }()
_, err = ioutil.ReadAll(resp.Body)
_, err = io.ReadAll(resp.Body)
if err != nil {
return err
}
Expand Down
8 changes: 3 additions & 5 deletions pkg/sentry/ca/certificate_authority_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"crypto/x509/pkix"
"encoding/asn1"
"encoding/pem"
"io/ioutil"
"os"
"strings"
"sync"
Expand Down Expand Up @@ -80,11 +79,10 @@ func getTestCertAuth() CertificateAuthority {
return certAuth
}

// nolint:gosec
func writeTestCredentialsToDisk() {
ioutil.WriteFile("ca.crt", []byte(rootCert), 0644)
ioutil.WriteFile("issuer.crt", []byte(issuerCert), 0644)
ioutil.WriteFile("issuer.key", []byte(issuerKey), 0644)
os.WriteFile("ca.crt", []byte(rootCert), 0644)
os.WriteFile("issuer.crt", []byte(issuerCert), 0644)
os.WriteFile("issuer.key", []byte(issuerKey), 0644)
}

func cleanupCredentials() {
Expand Down
7 changes: 3 additions & 4 deletions pkg/sentry/certs/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package certs

import (
"context"
"io/ioutil"
"os"

"github.com/pkg/errors"
Expand Down Expand Up @@ -82,17 +81,17 @@ func CredentialsExist(conf config.SentryConfig) (bool, error) {

/* #nosec. */
func storeSelfhosted(rootCertPem, issuerCertPem, issuerKeyPem []byte, rootCertPath, issuerCertPath, issuerKeyPath string) error {
err := ioutil.WriteFile(rootCertPath, rootCertPem, 0644)
err := os.WriteFile(rootCertPath, rootCertPem, 0644)
if err != nil {
return errors.Wrapf(err, "failed saving file to %s", rootCertPath)
}

err = ioutil.WriteFile(issuerCertPath, issuerCertPem, 0644)
err = os.WriteFile(issuerCertPath, issuerCertPem, 0644)
if err != nil {
return errors.Wrapf(err, "failed saving file to %s", issuerCertPath)
}

err = ioutil.WriteFile(issuerKeyPath, issuerKeyPem, 0644)
err = os.WriteFile(issuerKeyPath, issuerKeyPem, 0644)
if err != nil {
return errors.Wrapf(err, "failed saving file to %s", issuerKeyPath)
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/testing/app_mock.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
"context"
"encoding/json"
"fmt"
"io/ioutil"
"io"
"log"
"net/http"
"os"
Expand Down Expand Up @@ -87,7 +87,7 @@ func (a *MockApp) handler(w http.ResponseWriter, r *http.Request) {
return
}
defer r.Body.Close()
body, _ := ioutil.ReadAll(r.Body)
body, _ := io.ReadAll(r.Body)

var msg Event
if err := json.Unmarshal(body, &msg); err != nil {
Expand Down
4 changes: 2 additions & 2 deletions tests/apps/actorapp/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"io"
"log"
"net/http"
"sync"
Expand Down Expand Up @@ -181,7 +181,7 @@ func testCallActorHandler(w http.ResponseWriter, r *http.Request) {
}

defer res.Body.Close()
body, err := ioutil.ReadAll(res.Body)
body, err := io.ReadAll(res.Body)
if err != nil {
log.Printf("Could not read actor's test response: %s", err.Error())
w.WriteHeader(http.StatusInternalServerError)
Expand Down
4 changes: 2 additions & 2 deletions tests/apps/actorclientapp/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"io"
"log"
"net/http"
"time"
Expand Down Expand Up @@ -97,7 +97,7 @@ func httpCall(method string, url string, requestBody interface{}, expectedHTTPSt
return nil, t
}

resBody, err := ioutil.ReadAll(res.Body)
resBody, err := io.ReadAll(res.Body)
if err != nil {
return nil, err
}
Expand Down
8 changes: 4 additions & 4 deletions tests/apps/actorfeatures/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"io"
"log"
"net/http"
"os"
Expand Down Expand Up @@ -304,7 +304,7 @@ func testCallActorHandler(w http.ResponseWriter, r *http.Request) {
case "timers":
fallthrough
case "reminders":
body, err := ioutil.ReadAll(r.Body)
body, err := io.ReadAll(r.Body)
defer r.Body.Close()
if err != nil {
log.Printf("Could not get reminder request: %s", err.Error())
Expand Down Expand Up @@ -517,7 +517,7 @@ func httpCall(method string, url string, requestBody interface{}, expectedHTTPSt
defer res.Body.Close()

if res.StatusCode != expectedHTTPStatusCode {
errBody, err := ioutil.ReadAll(res.Body)
errBody, err := io.ReadAll(res.Body)
if err == nil {
t := fmt.Errorf("Expected http status %d, received %d, payload ='%s'", expectedHTTPStatusCode, res.StatusCode, string(errBody))
return nil, t
Expand All @@ -527,7 +527,7 @@ func httpCall(method string, url string, requestBody interface{}, expectedHTTPSt
return nil, t
}

resBody, err := ioutil.ReadAll(res.Body)
resBody, err := io.ReadAll(res.Body)
if err != nil {
return nil, err
}
Expand Down
Loading

0 comments on commit d7ab773

Please sign in to comment.