Skip to content

Commit

Permalink
io/ioutil deprecated (#724)
Browse files Browse the repository at this point in the history
  • Loading branch information
testwill committed May 30, 2023
1 parent c2958ac commit be633f7
Show file tree
Hide file tree
Showing 28 changed files with 103 additions and 116 deletions.
2 changes: 1 addition & 1 deletion autorest/adal/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ if (err == nil) {
```Go
certificatePath := "./example-app.pfx"

certData, err := ioutil.ReadFile(certificatePath)
certData, err := os.ReadFile(certificatePath)
if err != nil {
return nil, fmt.Errorf("failed to read the certificate file (%s): %v", certificatePath, err)
}
Expand Down
4 changes: 2 additions & 2 deletions autorest/adal/cmd/adal.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@ import (
"flag"
"fmt"
"log"
"os"
"strings"

"crypto/rsa"
"crypto/x509"
"io/ioutil"
"net/http"
"os/user"

Expand Down Expand Up @@ -203,7 +203,7 @@ func acquireTokenClientCertFlow(oauthConfig adal.OAuthConfig,
resource string,
callbacks ...adal.TokenRefreshCallback) (*adal.ServicePrincipalToken, error) {

certData, err := ioutil.ReadFile(certificatePath)
certData, err := os.ReadFile(certificatePath)
if err != nil {
return nil, fmt.Errorf("failed to read the certificate file (%s): %v", certificatePath, err)
}
Expand Down
10 changes: 5 additions & 5 deletions autorest/adal/devicetoken.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import (
"context"
"encoding/json"
"fmt"
"io/ioutil"
"io"
"net/http"
"net/url"
"strings"
Expand Down Expand Up @@ -116,7 +116,7 @@ func InitiateDeviceAuthWithContext(ctx context.Context, sender Sender, oauthConf
}

s := v.Encode()
body := ioutil.NopCloser(strings.NewReader(s))
body := io.NopCloser(strings.NewReader(s))

req, err := http.NewRequest(http.MethodPost, oauthConfig.DeviceCodeEndpoint.String(), body)
if err != nil {
Expand All @@ -131,7 +131,7 @@ func InitiateDeviceAuthWithContext(ctx context.Context, sender Sender, oauthConf
}
defer resp.Body.Close()

rb, err := ioutil.ReadAll(resp.Body)
rb, err := io.ReadAll(resp.Body)
if err != nil {
return nil, fmt.Errorf("%s %s: %s", logPrefix, errCodeHandlingFails, err.Error())
}
Expand Down Expand Up @@ -175,7 +175,7 @@ func CheckForUserCompletionWithContext(ctx context.Context, sender Sender, code
}

s := v.Encode()
body := ioutil.NopCloser(strings.NewReader(s))
body := io.NopCloser(strings.NewReader(s))

req, err := http.NewRequest(http.MethodPost, code.OAuthConfig.TokenEndpoint.String(), body)
if err != nil {
Expand All @@ -190,7 +190,7 @@ func CheckForUserCompletionWithContext(ctx context.Context, sender Sender, code
}
defer resp.Body.Close()

rb, err := ioutil.ReadAll(resp.Body)
rb, err := io.ReadAll(resp.Body)
if err != nil {
return nil, fmt.Errorf("%s %s: %s", logPrefix, errTokenHandlingFails, err.Error())
}
Expand Down
3 changes: 1 addition & 2 deletions autorest/adal/persist.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import (
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"os"
"path/filepath"

Expand Down Expand Up @@ -62,7 +61,7 @@ func SaveToken(path string, mode os.FileMode, token Token) error {
return fmt.Errorf("failed to create directory (%s) to store token in: %v", dir, err)
}

newFile, err := ioutil.TempFile(dir, "token")
newFile, err := os.CreateTemp(dir, "token")
if err != nil {
return fmt.Errorf("failed to create the temp file to write the token: %v", err)
}
Expand Down
7 changes: 3 additions & 4 deletions autorest/adal/persist_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ package adal

import (
"encoding/json"
"io/ioutil"
"os"
"path"
"reflect"
Expand Down Expand Up @@ -46,7 +45,7 @@ var TestToken = Token{
}

func writeTestTokenFile(t *testing.T, suffix string, contents string) *os.File {
f, err := ioutil.TempFile(os.TempDir(), suffix)
f, err := os.CreateTemp(os.TempDir(), suffix)
if err != nil {
t.Fatalf("azure: unexpected error when creating temp file: %v", err)
}
Expand Down Expand Up @@ -108,7 +107,7 @@ func token() *Token {
}

func TestSaveToken(t *testing.T) {
f, err := ioutil.TempFile("", "testloadtoken")
f, err := os.CreateTemp("", "testloadtoken")
if err != nil {
t.Fatalf("azure: unexpected error when creating temp file: %v", err)
}
Expand All @@ -135,7 +134,7 @@ func TestSaveToken(t *testing.T) {

json.Unmarshal([]byte(MockTokenJSON), &expectedToken)

contents, err := ioutil.ReadFile(f.Name())
contents, err := os.ReadFile(f.Name())
if err != nil {
t.Fatal("!!")
}
Expand Down
9 changes: 4 additions & 5 deletions autorest/adal/token.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ import (
"errors"
"fmt"
"io"
"io/ioutil"
"math"
"net/http"
"net/url"
Expand Down Expand Up @@ -1061,7 +1060,7 @@ func (spt *ServicePrincipalToken) refreshInternal(ctx context.Context, resource
} else if msiSecret.clientResourceID != "" {
data.Set("msi_res_id", msiSecret.clientResourceID)
}
req.Body = ioutil.NopCloser(strings.NewReader(data.Encode()))
req.Body = io.NopCloser(strings.NewReader(data.Encode()))
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
break
case msiTypeIMDS:
Expand Down Expand Up @@ -1096,7 +1095,7 @@ func (spt *ServicePrincipalToken) refreshInternal(ctx context.Context, resource
}

s := v.Encode()
body := ioutil.NopCloser(strings.NewReader(s))
body := io.NopCloser(strings.NewReader(s))
req.ContentLength = int64(len(s))
req.Header.Set(contentType, mimeTypeFormPost)
req.Body = body
Expand All @@ -1113,7 +1112,7 @@ func (spt *ServicePrincipalToken) refreshInternal(ctx context.Context, resource

logger.Instance.WriteResponse(resp, logger.Filter{Body: authBodyFilter})
defer resp.Body.Close()
rb, err := ioutil.ReadAll(resp.Body)
rb, err := io.ReadAll(resp.Body)

if resp.StatusCode != http.StatusOK {
if err != nil {
Expand Down Expand Up @@ -1235,7 +1234,7 @@ func retryForIMDS(sender Sender, req *http.Request, maxAttempts int) (resp *http

for attempt < maxAttempts {
if resp != nil && resp.Body != nil {
io.Copy(ioutil.Discard, resp.Body)
io.Copy(io.Discard, resp.Body)
resp.Body.Close()
}
resp, err = sender.Do(req)
Expand Down
4 changes: 2 additions & 2 deletions autorest/adal/token_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import (
"crypto/x509/pkix"
"encoding/json"
"fmt"
"io/ioutil"
"io"
"math/big"
"net/http"
"net/http/httptest"
Expand Down Expand Up @@ -632,7 +632,7 @@ func testServicePrincipalTokenRefreshSetsBody(t *testing.T, spt *ServicePrincipa
(func() SendDecorator {
return func(s Sender) Sender {
return SenderFunc(func(r *http.Request) (*http.Response, error) {
b, err := ioutil.ReadAll(r.Body)
b, err := io.ReadAll(r.Body)
if err != nil {
t.Fatalf("adal: Failed to read body of Service Principal token request (%v)", err)
}
Expand Down
14 changes: 7 additions & 7 deletions autorest/azure/async.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import (
"context"
"encoding/json"
"fmt"
"io/ioutil"
"io"
"net/http"
"net/url"
"strings"
Expand Down Expand Up @@ -318,11 +318,11 @@ func (f Future) GetResult(sender autorest.Sender) (*http.Response, error) {
if err == nil && resp.Body != nil {
// copy the body and close it so callers don't have to
defer resp.Body.Close()
b, err := ioutil.ReadAll(resp.Body)
b, err := io.ReadAll(resp.Body)
if err != nil {
return resp, err
}
resp.Body = ioutil.NopCloser(bytes.NewReader(b))
resp.Body = io.NopCloser(bytes.NewReader(b))
}
return resp, err
}
Expand Down Expand Up @@ -459,12 +459,12 @@ func (pt *pollingTrackerBase) updateRawBody() error {
pt.rawBody = map[string]interface{}{}
if pt.resp.ContentLength != 0 {
defer pt.resp.Body.Close()
b, err := ioutil.ReadAll(pt.resp.Body)
b, err := io.ReadAll(pt.resp.Body)
if err != nil {
return autorest.NewErrorWithError(err, "pollingTrackerBase", "updateRawBody", nil, "failed to read response body")
}
// put the body back so it's available to other callers
pt.resp.Body = ioutil.NopCloser(bytes.NewReader(b))
pt.resp.Body = io.NopCloser(bytes.NewReader(b))
// observed in 204 responses over HTTP/2.0; the content length is -1 but body is empty
if len(b) == 0 {
return nil
Expand Down Expand Up @@ -516,11 +516,11 @@ func (pt *pollingTrackerBase) updateErrorFromResponse() {
re := respErr{}
defer pt.resp.Body.Close()
var b []byte
if b, err = ioutil.ReadAll(pt.resp.Body); err != nil {
if b, err = io.ReadAll(pt.resp.Body); err != nil {
goto Default
}
// put the body back so it's available to other callers
pt.resp.Body = ioutil.NopCloser(bytes.NewReader(b))
pt.resp.Body = io.NopCloser(bytes.NewReader(b))
if len(b) == 0 {
goto Default
}
Expand Down
10 changes: 5 additions & 5 deletions autorest/azure/auth/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import (
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"io"
"log"
"os"
"strings"
Expand Down Expand Up @@ -325,7 +325,7 @@ func GetSettingsFromFile() (FileSettings, error) {
return s, errors.New("environment variable AZURE_AUTH_LOCATION is not set")
}

contents, err := ioutil.ReadFile(fileLocation)
contents, err := os.ReadFile(fileLocation)
if err != nil {
return s, err
}
Expand Down Expand Up @@ -488,7 +488,7 @@ func decode(b []byte) ([]byte, error) {
}
return []byte(string(utf16.Decode(u16))), nil
}
return ioutil.ReadAll(reader)
return io.ReadAll(reader)
}

func (settings FileSettings) getResourceForToken(baseURI string) (string, error) {
Expand Down Expand Up @@ -636,7 +636,7 @@ func (ccc ClientCertificateConfig) ServicePrincipalToken() (*adal.ServicePrincip
if err != nil {
return nil, err
}
certData, err := ioutil.ReadFile(ccc.CertificatePath)
certData, err := os.ReadFile(ccc.CertificatePath)
if err != nil {
return nil, fmt.Errorf("failed to read the certificate file (%s): %v", ccc.CertificatePath, err)
}
Expand All @@ -653,7 +653,7 @@ func (ccc ClientCertificateConfig) MultiTenantServicePrincipalToken() (*adal.Mul
if err != nil {
return nil, err
}
certData, err := ioutil.ReadFile(ccc.CertificatePath)
certData, err := os.ReadFile(ccc.CertificatePath)
if err != nil {
return nil, fmt.Errorf("failed to read the certificate file (%s): %v", ccc.CertificatePath, err)
}
Expand Down
4 changes: 2 additions & 2 deletions autorest/azure/azure.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"io"
"net/http"
"regexp"
"strconv"
Expand Down Expand Up @@ -333,7 +333,7 @@ func WithErrorUnlessStatusCode(codes ...int) autorest.RespondDecorator {
// Copy and replace the Body in case it does not contain an error object.
// This will leave the Body available to the caller.
b, decodeErr := autorest.CopyAndDecode(encodedAs, resp.Body, &e)
resp.Body = ioutil.NopCloser(&b)
resp.Body = io.NopCloser(&b)
if decodeErr != nil {
return fmt.Errorf("autorest/azure: error response cannot be parsed: %q error: %v", b, decodeErr)
}
Expand Down
12 changes: 6 additions & 6 deletions autorest/azure/azure_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ package azure
import (
"encoding/json"
"fmt"
"io/ioutil"
"io"
"net/http"
"reflect"
"strconv"
Expand Down Expand Up @@ -175,7 +175,7 @@ func TestWithErrorUnlessStatusCode_NotAnAzureError(t *testing.T) {

// the error body should still be there
defer r.Body.Close()
b, err := ioutil.ReadAll(r.Body)
b, err := io.ReadAll(r.Body)
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -226,7 +226,7 @@ func TestWithErrorUnlessStatusCode_FoundAzureErrorWithoutDetails(t *testing.T) {

// the error body should still be there
defer r.Body.Close()
b, err := ioutil.ReadAll(r.Body)
b, err := io.ReadAll(r.Body)
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -305,7 +305,7 @@ func TestWithErrorUnlessStatusCode_FoundAzureFullError(t *testing.T) {

// the error body should still be there
defer r.Body.Close()
b, err := ioutil.ReadAll(r.Body)
b, err := io.ReadAll(r.Body)
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -394,7 +394,7 @@ func TestWithErrorUnlessStatusCode_NoAzureError(t *testing.T) {

// the error body should still be there
defer r.Body.Close()
b, err := ioutil.ReadAll(r.Body)
b, err := io.ReadAll(r.Body)
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -488,7 +488,7 @@ func TestWithErrorUnlessStatusCode_UnwrappedError(t *testing.T) {

// the error body should still be there
defer r.Body.Close()
b, err := ioutil.ReadAll(r.Body)
b, err := io.ReadAll(r.Body)
if err != nil {
t.Error(err)
}
Expand Down
3 changes: 1 addition & 2 deletions autorest/azure/cli/profile.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"os"
"path/filepath"

Expand Down Expand Up @@ -66,7 +65,7 @@ func ProfilePath() (string, error) {
// LoadProfile restores a Profile object from a file located at 'path'.
func LoadProfile(path string) (result Profile, err error) {
var contents []byte
contents, err = ioutil.ReadFile(path)
contents, err = os.ReadFile(path)
if err != nil {
err = fmt.Errorf("failed to open file (%s) while loading token: %v", path, err)
return
Expand Down
3 changes: 1 addition & 2 deletions autorest/azure/environments.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ package azure
import (
"encoding/json"
"fmt"
"io/ioutil"
"os"
"strings"
)
Expand Down Expand Up @@ -315,7 +314,7 @@ func EnvironmentFromName(name string) (Environment, error) {
// This function is particularly useful in the Hybrid Cloud model, where one must define their own
// endpoints.
func EnvironmentFromFile(location string) (unmarshaled Environment, err error) {
fileContents, err := ioutil.ReadFile(location)
fileContents, err := os.ReadFile(location)
if err != nil {
return
}
Expand Down
Loading

0 comments on commit be633f7

Please sign in to comment.