Skip to content

Commit

Permalink
refactor: move from io/ioutil to io and os packages (#607)
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 <engzerjun@gmail.com>
  • Loading branch information
Juneezee committed Dec 14, 2021
1 parent 7e9ee8a commit e44f13d
Show file tree
Hide file tree
Showing 26 changed files with 55 additions and 63 deletions.
9 changes: 5 additions & 4 deletions benchmark/internal/cireport/tablereport.go
Expand Up @@ -5,13 +5,14 @@ import (
"context"
"embed"
"fmt"
"golang.org/x/sync/errgroup"
"gopkg.in/yaml.v2"
"io/ioutil"
"math"
"os"
"sync"
"text/template"
"time"

"golang.org/x/sync/errgroup"
"gopkg.in/yaml.v2"
)

var (
Expand Down Expand Up @@ -60,7 +61,7 @@ func TableReportCli(q Querier, queriesFile string) (string, error) {
var qCfg QueriesConfig

// read the file
yamlFile, err := ioutil.ReadFile(queriesFile)
yamlFile, err := os.ReadFile(queriesFile)
if err != nil {
return "", err
}
Expand Down
3 changes: 1 addition & 2 deletions pkg/admin/admin_integration_test.go
Expand Up @@ -4,7 +4,6 @@
package admin_test

import (
"io/ioutil"
"net/http"
"os"
"time"
Expand Down Expand Up @@ -84,7 +83,7 @@ func genRandomDir() (func(), string) {
// so we first create a temporary directory
// and pass a well-known file name
// that way tests can be run concurrently
dir, err := ioutil.TempDir("", "")
dir, err := os.MkdirTemp("", "")
Expect(err).ToNot(HaveOccurred())

return func() {
Expand Down
4 changes: 2 additions & 2 deletions pkg/admin/controller_test.go
Expand Up @@ -4,7 +4,7 @@ import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"io"
"net/http"
"net/http/httptest"

Expand Down Expand Up @@ -65,7 +65,7 @@ var _ = Describe("controller", func() {

svr.Handler.ServeHTTP(response, request)

body, err := ioutil.ReadAll(response.Body)
body, err := io.ReadAll(response.Body)
Expect(err).To(BeNil())

Expect(response.Code).To(Equal(http.StatusOK))
Expand Down
4 changes: 2 additions & 2 deletions pkg/agent/pprof/proto.go
Expand Up @@ -9,7 +9,7 @@ import (
"compress/gzip"
"fmt"
"io"
"io/ioutil"
"os"
"runtime"
"sort"
"strconv"
Expand Down Expand Up @@ -602,7 +602,7 @@ func (b *profileBuilder) emitLocation() uint64 {
// It saves the address ranges of the mappings in b.mem for use
// when emitting locations.
func (b *profileBuilder) readMapping() {
data, _ := ioutil.ReadFile("/proc/self/maps")
data, _ := os.ReadFile("/proc/self/maps")
parseProcSelfMaps(data, b.addMapping)
if len(b.mem) == 0 { // pprof expects a map entry, so fake one.
b.addMappingEntry(0, 0, 0, "", "", true)
Expand Down
4 changes: 2 additions & 2 deletions pkg/agent/upstream/remote/remote.go
Expand Up @@ -4,7 +4,7 @@ import (
"bytes"
"errors"
"fmt"
"io/ioutil"
"io"
"net/http"
"net/url"
"path"
Expand Down Expand Up @@ -136,7 +136,7 @@ func (r *Remote) uploadProfile(j *upstream.UploadJob) error {
defer response.Body.Close()

// read all the response body
_, err = ioutil.ReadAll(response.Body)
_, err = io.ReadAll(response.Body)
if err != nil {
return fmt.Errorf("read response body: %v", err)
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/agent/upstream/remote/remote_test.go
Expand Up @@ -3,7 +3,7 @@ package remote
import (
"fmt"
"html"
"io/ioutil"
"io"
"net/http"
"net/http/httptest"
"sync"
Expand Down Expand Up @@ -34,7 +34,7 @@ var _ = Describe("remote.Remote", func() {
timestampsMutex.Lock()
timestamps = append(timestamps, time.Now())
timestampsMutex.Unlock()
_, err := ioutil.ReadAll(r.Body)
_, err := io.ReadAll(r.Body)
Expect(err).ToNot(HaveOccurred())

fmt.Fprintf(w, "Hello, %q", html.EscapeString(r.URL.Path))
Expand Down
4 changes: 2 additions & 2 deletions pkg/analytics/analytics.go
Expand Up @@ -18,7 +18,7 @@ package analytics
import (
"bytes"
"encoding/json"
"io/ioutil"
"io"
"net/http"
"runtime"
"time"
Expand Down Expand Up @@ -179,7 +179,7 @@ func (s *Service) sendReport() {
logrus.WithField("err", err).Error("Error happened when uploading anonymized usage data")
}
if resp != nil {
_, err := ioutil.ReadAll(resp.Body)
_, err := io.ReadAll(resp.Body)
if err != nil {
logrus.WithField("err", err).Error("Error happened when uploading reading server response")
return
Expand Down
4 changes: 2 additions & 2 deletions pkg/analytics/analytics_test.go
Expand Up @@ -7,7 +7,7 @@ import (
"encoding/json"
"fmt"
"html"
"io/ioutil"
"io"
"net/http"
"net/http/httptest"
"sync"
Expand Down Expand Up @@ -47,7 +47,7 @@ var _ = Describe("analytics", func() {
timestamps := []time.Time{}
myHandler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
timestamps = append(timestamps, time.Now())
bytes, err := ioutil.ReadAll(r.Body)
bytes, err := io.ReadAll(r.Body)
Expect(err).ToNot(HaveOccurred())

v := make(map[string]interface{})
Expand Down
6 changes: 3 additions & 3 deletions pkg/cli/agent.go
Expand Up @@ -2,7 +2,7 @@ package cli

import (
"fmt"
"io/ioutil"
"io"
"os"
"path/filepath"

Expand Down Expand Up @@ -54,7 +54,7 @@ func (svc *agentService) Stop(_ service.Service) error {
// https://github.com/spf13/viper#accessing-nested-keys.
// TODO(kolesnikovae): find a way to get rid of the function.
func loadAgentConfig(c *config.Agent) error {
b, err := ioutil.ReadFile(c.Config)
b, err := os.ReadFile(c.Config)
switch {
case err == nil:
case os.IsNotExist(err):
Expand Down Expand Up @@ -90,7 +90,7 @@ func mergeTags(a, b map[string]string) map[string]string {

func createLogger(cfg *config.Agent) (*logrus.Logger, error) {
if cfg.NoLogging {
logrus.SetOutput(ioutil.Discard)
logrus.SetOutput(io.Discard)
return logrus.StandardLogger(), nil
}
l, err := logrus.ParseLevel(cfg.LogLevel)
Expand Down
3 changes: 1 addition & 2 deletions pkg/convert/parser.go
Expand Up @@ -5,7 +5,6 @@ import (
"bytes"
"compress/gzip"
"io"
"io/ioutil"
"strconv"

"google.golang.org/protobuf/proto"
Expand Down Expand Up @@ -46,7 +45,7 @@ func ParsePprof(r io.Reader) (*tree.Profile, error) {
r = bufioReader
}

b, err := ioutil.ReadAll(r)
b, err := io.ReadAll(r)
if err != nil {
return nil, err
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/convert/parser_test.go
Expand Up @@ -4,7 +4,7 @@ import (
"bytes"
"compress/gzip"
"fmt"
"io/ioutil"
"os"

. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
Expand All @@ -16,7 +16,7 @@ var _ = Describe("convert", func() {
It("parses data correctly", func() {
result := []string{}

b, err := ioutil.ReadFile("testdata/cpu.pprof")
b, err := os.ReadFile("testdata/cpu.pprof")
Expect(err).ToNot(HaveOccurred())
r := bytes.NewReader(b)
g, err := gzip.NewReader(r)
Expand Down
4 changes: 2 additions & 2 deletions pkg/convert/profile_extra_bench_test.go
Expand Up @@ -3,14 +3,14 @@ package convert
import (
"bytes"
"compress/gzip"
"io/ioutil"
"os"
"testing"

"github.com/pyroscope-io/pyroscope/pkg/agent/spy"
)

func BenchmarkProfile_Get(b *testing.B) {
buf, _ := ioutil.ReadFile("testdata/cpu.pprof")
buf, _ := os.ReadFile("testdata/cpu.pprof")
g, _ := gzip.NewReader(bytes.NewReader(buf))
p, _ := ParsePprof(g)
noop := func(labels *spy.Labels, name []byte, val int) {}
Expand Down
9 changes: 4 additions & 5 deletions pkg/scrape/config/config_http.go
Expand Up @@ -25,7 +25,6 @@ import (
"crypto/x509"
"encoding/json"
"fmt"
"io/ioutil"
"net"
"net/http"
"net/url"
Expand Down Expand Up @@ -503,7 +502,7 @@ func NewAuthorizationCredentialsFileRoundTripper(authType, authCredentialsFile s

func (rt *authorizationCredentialsFileRoundTripper) RoundTrip(req *http.Request) (*http.Response, error) {
if len(req.Header.Get("Authorization")) == 0 {
b, err := ioutil.ReadFile(rt.authCredentialsFile)
b, err := os.ReadFile(rt.authCredentialsFile)
if err != nil {
return nil, fmt.Errorf("unable to read authorization credentials file %s: %s", rt.authCredentialsFile, err)
}
Expand Down Expand Up @@ -541,7 +540,7 @@ func (rt *basicAuthRoundTripper) RoundTrip(req *http.Request) (*http.Response, e
}
req = cloneRequest(req)
if rt.passwordFile != "" {
bs, err := ioutil.ReadFile(rt.passwordFile)
bs, err := os.ReadFile(rt.passwordFile)
if err != nil {
return nil, fmt.Errorf("unable to read basic auth password file %s: %s", rt.passwordFile, err)
}
Expand Down Expand Up @@ -580,7 +579,7 @@ func (rt *oauth2RoundTripper) RoundTrip(req *http.Request) (*http.Response, erro
)

if rt.config.ClientSecretFile != "" {
data, err := ioutil.ReadFile(rt.config.ClientSecretFile)
data, err := os.ReadFile(rt.config.ClientSecretFile)
if err != nil {
return nil, fmt.Errorf("unable to read oauth2 client secret file %s: %s", rt.config.ClientSecretFile, err)
}
Expand Down Expand Up @@ -744,7 +743,7 @@ func (c *TLSConfig) getClientCertificate(*tls.CertificateRequestInfo) (*tls.Cert

// readCAFile reads the CA cert file from disk.
func readCAFile(f string) ([]byte, error) {
data, err := ioutil.ReadFile(f)
data, err := os.ReadFile(f)
if err != nil {
return nil, fmt.Errorf("unable to load specified CA cert %s: %s", f, err)
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/server/build_test.go
Expand Up @@ -2,7 +2,7 @@ package server

import (
"encoding/json"
"io/ioutil"
"io"
"net/http"
"net/http/httptest"
"runtime"
Expand Down Expand Up @@ -47,7 +47,7 @@ var _ = Describe("server", func() {
Expect(err).ToNot(HaveOccurred())
Expect(res.StatusCode).To(Equal(200))

b, err := ioutil.ReadAll(res.Body)
b, err := io.ReadAll(res.Body)
Expect(err).ToNot(HaveOccurred())

actual := make(map[string]interface{})
Expand Down
4 changes: 2 additions & 2 deletions pkg/server/config_test.go
Expand Up @@ -2,7 +2,7 @@ package server

import (
"encoding/json"
"io/ioutil"
"io"
"net/http"
"net/http/httptest"

Expand Down Expand Up @@ -46,7 +46,7 @@ var _ = Describe("server", func() {
Expect(err).ToNot(HaveOccurred())
Expect(res.StatusCode).To(Equal(200))

b, err := ioutil.ReadAll(res.Body)
b, err := io.ReadAll(res.Body)
Expect(err).ToNot(HaveOccurred())

actual := make(map[string]interface{})
Expand Down
6 changes: 3 additions & 3 deletions pkg/server/controller_gzip_test.go
Expand Up @@ -2,9 +2,9 @@ package server

import (
"fmt"
"io/ioutil"
"net/http"
"net/http/httptest"
"os"
"path/filepath"

. "github.com/onsi/ginkgo"
Expand All @@ -26,8 +26,8 @@ var _ = Describe("server", func() {
var tempAssetDir *testing.TmpDirectory
BeforeSuite(func() {
tempAssetDir = testing.TmpDirSync()
ioutil.WriteFile(filepath.Join(tempAssetDir.Path, assetLtCompressionThreshold), make([]byte, gzHTTPCompressionThreshold-1), 0644)
ioutil.WriteFile(filepath.Join(tempAssetDir.Path, assetAtCompressionThreshold), make([]byte, gzHTTPCompressionThreshold), 0644)
os.WriteFile(filepath.Join(tempAssetDir.Path, assetLtCompressionThreshold), make([]byte, gzHTTPCompressionThreshold-1), 0644)
os.WriteFile(filepath.Join(tempAssetDir.Path, assetAtCompressionThreshold), make([]byte, gzHTTPCompressionThreshold), 0644)
})
AfterSuite(func() {
tempAssetDir.Close()
Expand Down
5 changes: 2 additions & 3 deletions pkg/server/handler.go
Expand Up @@ -6,7 +6,6 @@ import (
"encoding/json"
"fmt"
"io"
"io/ioutil"
"net/http"
"os"
"strconv"
Expand Down Expand Up @@ -220,7 +219,7 @@ func (ctrl *Controller) getTemplate(path string) (*template.Template, error) {
return nil, fmt.Errorf("could not find file %s: %q", path, err)
}

b, err := ioutil.ReadAll(f)
b, err := io.ReadAll(f)
if err != nil {
return nil, fmt.Errorf("could not read file %s: %q", path, err)
}
Expand Down Expand Up @@ -253,7 +252,7 @@ func (ctrl *Controller) renderIndexPage(w http.ResponseWriter, _ *http.Request)
var extraMetadataStr string
extraMetadataPath := os.Getenv("PYROSCOPE_EXTRA_METADATA")
if extraMetadataPath != "" {
b, err = ioutil.ReadFile(extraMetadataPath)
b, err = os.ReadFile(extraMetadataPath)
if err != nil {
logrus.Errorf("failed to read file at %s", extraMetadataPath)
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/server/render_test.go
Expand Up @@ -4,7 +4,7 @@ import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"io"
"net/http"
"net/http/httptest"
"net/url"
Expand Down Expand Up @@ -121,7 +121,7 @@ var _ = Describe("server", func() {
Expect(resp.Header.Get("Content-Disposition")).To(MatchRegexp(
"^attachment; filename=.+\\.pprof$",
))
body, _ := ioutil.ReadAll(resp.Body)
body, _ := io.ReadAll(resp.Body)
profile := &tree.Profile{}
err = proto.Unmarshal(body, profile)
Expect(err).ToNot(HaveOccurred())
Expand Down
4 changes: 2 additions & 2 deletions pkg/storage/mem_linux.go
Expand Up @@ -3,7 +3,7 @@
package storage

import (
"io/ioutil"
"io"
"os"
"strconv"
"strings"
Expand All @@ -20,7 +20,7 @@ func getCgroupMemLimit() (uint64, error) {
if err != nil {
return 0, err
}
b, err := ioutil.ReadAll(f)
b, err := io.ReadAll(f)
if err != nil {
return 0, err
}
Expand Down

0 comments on commit e44f13d

Please sign in to comment.