Skip to content

Commit

Permalink
Merge pull request #889 from CircleCI-Public/updating-deprecated-func…
Browse files Browse the repository at this point in the history
…tion-calls

Migrates the old calls to the deprecated ioutil package to io and os respectively
  • Loading branch information
elliotforbes authored Mar 24, 2023
2 parents 6271ba1 + f9bb773 commit 1d4e972
Show file tree
Hide file tree
Showing 22 changed files with 72 additions and 83 deletions.
6 changes: 3 additions & 3 deletions api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package api
import (
"encoding/json"
"fmt"
"io/ioutil"
"io"
"log"
"net/http"
"os"
Expand Down Expand Up @@ -484,9 +484,9 @@ func loadYaml(path string) (string, error) {
var err error
var config []byte
if path == "-" {
config, err = ioutil.ReadAll(os.Stdin)
config, err = io.ReadAll(os.Stdin)
} else {
config, err = ioutil.ReadFile(path)
config, err = os.ReadFile(path)
}

if err != nil {
Expand Down
17 changes: 8 additions & 9 deletions api/context_rest.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"encoding/json"
"fmt"
"io"
"io/ioutil"
"net/http"
"net/url"

Expand Down Expand Up @@ -71,7 +70,7 @@ func (c *ContextRestClient) DeleteEnvironmentVariable(contextID, variable string
return err
}

bodyBytes, err := ioutil.ReadAll(resp.Body)
bodyBytes, err := io.ReadAll(resp.Body)
defer resp.Body.Close()
if err != nil {
return err
Expand Down Expand Up @@ -99,7 +98,7 @@ func (c *ContextRestClient) CreateContextWithOrgID(orgID *string, name string) e
return err
}

bodyBytes, err := ioutil.ReadAll(resp.Body)
bodyBytes, err := io.ReadAll(resp.Body)
defer resp.Body.Close()
if err != nil {
return err
Expand Down Expand Up @@ -131,7 +130,7 @@ func (c *ContextRestClient) CreateContext(vcs, org, name string) error {
return err
}

bodyBytes, err := ioutil.ReadAll(resp.Body)
bodyBytes, err := io.ReadAll(resp.Body)
defer resp.Body.Close()
if err != nil {
return err
Expand Down Expand Up @@ -162,7 +161,7 @@ func (c *ContextRestClient) CreateEnvironmentVariable(contextID, variable, value
return err
}

bodyBytes, err := ioutil.ReadAll(resp.Body)
bodyBytes, err := io.ReadAll(resp.Body)
defer resp.Body.Close()
if err != nil {
return err
Expand Down Expand Up @@ -190,7 +189,7 @@ func (c *ContextRestClient) DeleteContext(contextID string) error {
return err
}

bodyBytes, err := ioutil.ReadAll(resp.Body)
bodyBytes, err := io.ReadAll(resp.Body)
defer resp.Body.Close()
if err != nil {
return err
Expand Down Expand Up @@ -300,7 +299,7 @@ func (c *ContextRestClient) listEnvironmentVariables(params *listEnvironmentVari
return nil, err
}

bodyBytes, err := ioutil.ReadAll(resp.Body)
bodyBytes, err := io.ReadAll(resp.Body)
defer resp.Body.Close()
if err != nil {
return nil, err
Expand Down Expand Up @@ -334,7 +333,7 @@ func (c *ContextRestClient) listContexts(params *listContextsParams) (*listConte
return nil, err
}

bodyBytes, err := ioutil.ReadAll(resp.Body)
bodyBytes, err := io.ReadAll(resp.Body)
defer resp.Body.Close()
if err != nil {
return nil, err
Expand Down Expand Up @@ -580,7 +579,7 @@ func (c *ContextRestClient) EnsureExists() error {
return errors.New("API v2 test request failed.")
}

bodyBytes, err := ioutil.ReadAll(resp.Body)
bodyBytes, err := io.ReadAll(resp.Body)
defer resp.Body.Close()
if err != nil {
return err
Expand Down
4 changes: 2 additions & 2 deletions api/context_rest_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package api

import (
"fmt"
"io/ioutil"
"io"
"net/http"

"github.com/CircleCI-Public/circleci-cli/settings"
Expand Down Expand Up @@ -32,7 +32,7 @@ func appendRESTPostHandler(server *ghttp.Server, combineHandlers ...MockRequestR
ghttp.VerifyRequest("POST", "/api/v2/context"),
ghttp.VerifyContentType("application/json"),
func(w http.ResponseWriter, req *http.Request) {
body, err := ioutil.ReadAll(req.Body)
body, err := io.ReadAll(req.Body)
Expect(err).ShouldNot(HaveOccurred())
err = req.Body.Close()
Expect(err).ShouldNot(HaveOccurred())
Expand Down
6 changes: 3 additions & 3 deletions api/graphql/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"context"
"encoding/json"
"fmt"
"io/ioutil"
"io"
"log"
"net/http"
"net/url"
Expand Down Expand Up @@ -254,15 +254,15 @@ func (cl *Client) Run(request *Request, resp interface{}) error {
if cl.Debug {
var bodyBytes []byte
if res.Body != nil {
bodyBytes, err = ioutil.ReadAll(res.Body)
bodyBytes, err = io.ReadAll(res.Body)
if err != nil {
return errors.Wrap(err, "reading response")
}

l.Printf("<< %s", string(bodyBytes))

// Restore the io.ReadCloser to its original state
res.Body = ioutil.NopCloser(bytes.NewBuffer(bodyBytes))
res.Body = io.NopCloser(bytes.NewBuffer(bodyBytes))
}
}

Expand Down
7 changes: 3 additions & 4 deletions api/graphql/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package graphql

import (
"io"
"io/ioutil"
"net/http"
"net/http/httptest"
"regexp"
Expand Down Expand Up @@ -53,7 +52,7 @@ func TestDoJSON(t *testing.T) {
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
calls++

b, err := ioutil.ReadAll(r.Body)
b, err := io.ReadAll(r.Body)
if err != nil {
t.Errorf(err.Error())
}
Expand Down Expand Up @@ -96,7 +95,7 @@ func TestQueryJSON(t *testing.T) {
var calls int
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
calls++
b, err := ioutil.ReadAll(r.Body)
b, err := io.ReadAll(r.Body)
if err != nil {
t.Errorf(err.Error())
}
Expand Down Expand Up @@ -146,7 +145,7 @@ func TestDoJSONErr(t *testing.T) {
server := httptest.NewServer(http.HandlerFunc(func(writer http.ResponseWriter, req *http.Request) {
calls++

body, err := ioutil.ReadAll(req.Body)
body, err := io.ReadAll(req.Body)
if err != nil {
t.Errorf(err.Error())
}
Expand Down
3 changes: 1 addition & 2 deletions api/info/info.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"errors"
"fmt"
"io"
"io/ioutil"
"net/http"
"net/url"

Expand Down Expand Up @@ -58,7 +57,7 @@ func (c *InfoRESTClient) GetInfo() (*[]Organization, error) {
return nil, err
}

bodyBytes, err := ioutil.ReadAll(resp.Body)
bodyBytes, err := io.ReadAll(resp.Body)
defer resp.Body.Close()
if err != nil {
return nil, err
Expand Down
13 changes: 6 additions & 7 deletions api/schedule_rest.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"encoding/json"
"fmt"
"io"
"io/ioutil"
"net/http"
"net/url"

Expand Down Expand Up @@ -49,7 +48,7 @@ func (c *ScheduleRestClient) CreateSchedule(vcs, org, project, name, description
return nil, err
}

bodyBytes, err := ioutil.ReadAll(resp.Body)
bodyBytes, err := io.ReadAll(resp.Body)
defer resp.Body.Close()
if err != nil {
return nil, err
Expand Down Expand Up @@ -86,7 +85,7 @@ func (c *ScheduleRestClient) UpdateSchedule(scheduleID, name, description string
return nil, err
}

bodyBytes, err := ioutil.ReadAll(resp.Body)
bodyBytes, err := io.ReadAll(resp.Body)
defer resp.Body.Close()
if err != nil {
return nil, err
Expand Down Expand Up @@ -121,7 +120,7 @@ func (c *ScheduleRestClient) DeleteSchedule(scheduleID string) error {
return err
}

bodyBytes, err := ioutil.ReadAll(resp.Body)
bodyBytes, err := io.ReadAll(resp.Body)
defer resp.Body.Close()
if err != nil {
return err
Expand Down Expand Up @@ -157,7 +156,7 @@ func (c *ScheduleRestClient) ScheduleByID(scheduleID string) (*Schedule, error)
return nil, err
}

bodyBytes, err := ioutil.ReadAll(resp.Body)
bodyBytes, err := io.ReadAll(resp.Body)
defer resp.Body.Close()
if err != nil {
return nil, err
Expand Down Expand Up @@ -231,7 +230,7 @@ func (c *ScheduleRestClient) listSchedules(vcs, org, project string, params *lis
return nil, err
}

bodyBytes, err := ioutil.ReadAll(resp.Body)
bodyBytes, err := io.ReadAll(resp.Body)
defer resp.Body.Close()
if err != nil {
return nil, err
Expand Down Expand Up @@ -438,7 +437,7 @@ func (c *ScheduleRestClient) EnsureExists() error {
return errors.New("API v2 test request failed.")
}

bodyBytes, err := ioutil.ReadAll(resp.Body)
bodyBytes, err := io.ReadAll(resp.Body)
defer resp.Body.Close()
if err != nil {
return err
Expand Down
11 changes: 5 additions & 6 deletions clitest/clitest.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"bytes"
"fmt"
"io"
"io/ioutil"
"net/http"
"os"
"path/filepath"
Expand Down Expand Up @@ -50,7 +49,7 @@ func (tempSettings TempSettings) AssertConfigRereadMatches(contents string) {
file, err := os.Open(tempSettings.Config.Path)
gomega.Expect(err).ShouldNot(gomega.HaveOccurred())

reread, err := ioutil.ReadAll(file)
reread, err := io.ReadAll(file)
gomega.Expect(err).ShouldNot(gomega.HaveOccurred())
gomega.Expect(string(reread)).To(gomega.ContainSubstring(contents))
}
Expand All @@ -61,7 +60,7 @@ func WithTempSettings() *TempSettings {

tempSettings := &TempSettings{}

tempSettings.Home, err = ioutil.TempDir("", "circleci-cli-test-")
tempSettings.Home, err = os.MkdirTemp("", "circleci-cli-test-")
gomega.Expect(err).ToNot(gomega.HaveOccurred())

settingsPath := filepath.Join(tempSettings.Home, ".circleci")
Expand Down Expand Up @@ -101,7 +100,7 @@ func (tempSettings *TempSettings) AppendRESTPostHandler(combineHandlers ...MockR
ghttp.VerifyRequest("POST", "/api/v2/context"),
ghttp.VerifyContentType("application/json"),
func(w http.ResponseWriter, req *http.Request) {
body, err := ioutil.ReadAll(req.Body)
body, err := io.ReadAll(req.Body)
gomega.Expect(err).ShouldNot(gomega.HaveOccurred())
err = req.Body.Close()
gomega.Expect(err).ShouldNot(gomega.HaveOccurred())
Expand Down Expand Up @@ -131,7 +130,7 @@ func (tempSettings *TempSettings) AppendPostHandler(authToken string, combineHan
// VerifyContentType("application/json") check
// that fails with "application/json; charset=utf-8"
func(w http.ResponseWriter, req *http.Request) {
body, err := ioutil.ReadAll(req.Body)
body, err := io.ReadAll(req.Body)
gomega.Expect(err).ShouldNot(gomega.HaveOccurred())
err = req.Body.Close()
gomega.Expect(err).ShouldNot(gomega.HaveOccurred())
Expand All @@ -152,7 +151,7 @@ func (tempSettings *TempSettings) AppendPostHandler(authToken string, combineHan
// VerifyContentType("application/json") check
// that fails with "application/json; charset=utf-8"
func(w http.ResponseWriter, req *http.Request) {
body, err := ioutil.ReadAll(req.Body)
body, err := io.ReadAll(req.Body)
gomega.Expect(err).ShouldNot(gomega.HaveOccurred())
err = req.Body.Close()
gomega.Expect(err).ShouldNot(gomega.HaveOccurred())
Expand Down
3 changes: 1 addition & 2 deletions cmd/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package cmd

import (
"fmt"
"io/ioutil"
"net/url"
"os"
"strings"
Expand Down Expand Up @@ -208,7 +207,7 @@ func processConfig(opts configOptions, flags *pflag.FlagSet) error {
if len(paramsYaml) > 0 {
// The 'src' value can be a filepath, or a yaml string. If the file cannot be read successfully,
// proceed with the assumption that the value is already valid yaml.
raw, err := ioutil.ReadFile(paramsYaml)
raw, err := os.ReadFile(paramsYaml)
if err != nil {
raw = []byte(paramsYaml)
}
Expand Down
4 changes: 2 additions & 2 deletions cmd/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package cmd
import (
"bufio"
"fmt"
"io/ioutil"
"io"
"os"
"strings"
"time"
Expand Down Expand Up @@ -189,7 +189,7 @@ func showContext(client api.ContextInterface, vcsType, orgName, contextName stri
func readSecretValue() (string, error) {
stat, _ := os.Stdin.Stat()
if (stat.Mode() & os.ModeCharDevice) == 0 {
bytes, err := ioutil.ReadAll(os.Stdin)
bytes, err := io.ReadAll(os.Stdin)
return string(bytes), err
} else {
fmt.Print("Enter secret value and press enter: ")
Expand Down
Loading

0 comments on commit 1d4e972

Please sign in to comment.