Skip to content

Commit

Permalink
Merge pull request prometheus#3009 from inosato/remove-ioutil
Browse files Browse the repository at this point in the history
Remove ioutil
  • Loading branch information
SuperQ authored Jul 25, 2022
2 parents 155a47f + 791e542 commit b55097f
Show file tree
Hide file tree
Showing 24 changed files with 57 additions and 61 deletions.
6 changes: 3 additions & 3 deletions api/v1/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import (
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"io"
"net/http"
"net/http/httptest"
"regexp"
Expand Down Expand Up @@ -150,7 +150,7 @@ func TestAddAlerts(t *testing.T) {

api.addAlerts(w, r)
res := w.Result()
body, _ := ioutil.ReadAll(res.Body)
body, _ := io.ReadAll(res.Body)

require.Equal(t, tc.code, w.Code, fmt.Sprintf("test case: %d, StartsAt %v, EndsAt %v, Response: %s", i, tc.start, tc.end, string(body)))
}
Expand Down Expand Up @@ -282,7 +282,7 @@ func TestListAlerts(t *testing.T) {
w := httptest.NewRecorder()

api.listAlerts(w, r)
body, _ := ioutil.ReadAll(w.Result().Body)
body, _ := io.ReadAll(w.Result().Body)

var res response
err = json.Unmarshal(body, &res)
Expand Down
6 changes: 3 additions & 3 deletions api/v2/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ package v2
import (
"bytes"
"fmt"
"io/ioutil"
"io"
"net/http"
"net/http/httptest"
"strconv"
Expand Down Expand Up @@ -204,7 +204,7 @@ func TestDeleteSilenceHandler(t *testing.T) {
HTTPRequest: r,
})
responder.WriteResponse(w, p)
body, _ := ioutil.ReadAll(w.Result().Body)
body, _ := io.ReadAll(w.Result().Body)

require.Equal(t, tc.expectedCode, w.Code, fmt.Sprintf("test case: %d, response: %s", i, string(body)))
}
Expand Down Expand Up @@ -290,7 +290,7 @@ func TestPostSilencesHandler(t *testing.T) {
Silence: &silence,
})
responder.WriteResponse(w, p)
body, _ := ioutil.ReadAll(w.Result().Body)
body, _ := io.ReadAll(w.Result().Body)

require.Equal(t, tc.expectedCode, w.Code, fmt.Sprintf("test case: %d, response: %s", i, string(body)))
})
Expand Down
3 changes: 1 addition & 2 deletions cli/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
package config

import (
"io/ioutil"
"os"

"gopkg.in/alecthomas/kingpin.v2"
Expand All @@ -37,7 +36,7 @@ func NewResolver(files []string, legacyFlags map[string]string) (*Resolver, erro
if _, err := os.Stat(f); err != nil {
continue
}
b, err := ioutil.ReadFile(f)
b, err := os.ReadFile(f)
if err != nil {
if os.IsNotExist(err) {
continue
Expand Down
6 changes: 3 additions & 3 deletions cli/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
package config

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

"gopkg.in/alecthomas/kingpin.v2"
Expand All @@ -30,8 +30,8 @@ func newApp() *kingpin.Application {
id = new(string)

app := kingpin.New("app", "")
app.UsageWriter(ioutil.Discard)
app.ErrorWriter(ioutil.Discard)
app.UsageWriter(io.Discard)
app.ErrorWriter(io.Discard)
app.Terminate(nil)

app.Flag("url", "").StringVar(url)
Expand Down
4 changes: 2 additions & 2 deletions cli/config/http_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
package config

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

promconfig "github.com/prometheus/common/config"
Expand All @@ -23,7 +23,7 @@ import (

// LoadHTTPConfigFile returns HTTPClientConfig for the given http_config file
func LoadHTTPConfigFile(filename string) (*promconfig.HTTPClientConfig, error) {
b, err := ioutil.ReadFile(filename)
b, err := os.ReadFile(filename)
if err != nil {
return nil, err
}
Expand Down
4 changes: 2 additions & 2 deletions cli/template_render.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import (
"context"
"encoding/json"
"fmt"
"io/ioutil"
"io"
"os"
"time"

Expand Down Expand Up @@ -121,7 +121,7 @@ func (c *templateRenderCmd) render(ctx context.Context, _ *kingpin.ParseContext)
if c.templateData == nil {
data = defaultData
} else {
content, err := ioutil.ReadAll(c.templateData)
content, err := io.ReadAll(c.templateData)
if err != nil {
return err
}
Expand Down
4 changes: 2 additions & 2 deletions cluster/tls_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
package cluster

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

"github.com/prometheus/common/config"
Expand All @@ -31,7 +31,7 @@ func GetTLSTransportConfig(configPath string) (*TLSTransportConfig, error) {
if configPath == "" {
return nil, nil
}
bytes, err := ioutil.ReadFile(configPath)
bytes, err := os.ReadFile(configPath)
if err != nil {
return nil, err
}
Expand Down
4 changes: 2 additions & 2 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ package config
import (
"encoding/json"
"fmt"
"io/ioutil"
"net"
"net/url"
"os"
"path/filepath"
"regexp"
"sort"
Expand Down Expand Up @@ -192,7 +192,7 @@ func Load(s string) (*Config, error) {

// LoadFile parses the given YAML file into a Config.
func LoadFile(filename string) (*Config, error) {
content, err := ioutil.ReadFile(filename)
content, err := os.ReadFile(filename)
if err != nil {
return nil, err
}
Expand Down
4 changes: 2 additions & 2 deletions examples/webhook/echo.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,14 @@ package main
import (
"bytes"
"encoding/json"
"io/ioutil"
"io"
"log"
"net/http"
)

func main() {
log.Fatal(http.ListenAndServe(":5001", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
b, err := ioutil.ReadAll(r.Body)
b, err := io.ReadAll(r.Body)
if err != nil {
panic(err)
}
Expand Down
10 changes: 5 additions & 5 deletions nflog/nflog_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ package nflog

import (
"bytes"
"io/ioutil"
"io"
"os"
"path/filepath"
"sync"
Expand Down Expand Up @@ -98,7 +98,7 @@ func TestLogSnapshot(t *testing.T) {
}

for _, c := range cases {
f, err := ioutil.TempFile("", "snapshot")
f, err := os.CreateTemp("", "snapshot")
require.NoError(t, err, "creating temp file failed")

l1 := &Log{
Expand Down Expand Up @@ -127,7 +127,7 @@ func TestLogSnapshot(t *testing.T) {
}

func TestWithMaintenance_SupportsCustomCallback(t *testing.T) {
f, err := ioutil.TempFile("", "snapshot")
f, err := os.CreateTemp("", "snapshot")
require.NoError(t, err, "creating temp file failed")

stopc := make(chan struct{})
Expand All @@ -154,7 +154,7 @@ func TestWithMaintenance_SupportsCustomCallback(t *testing.T) {
}

func TestReplaceFile(t *testing.T) {
dir, err := ioutil.TempDir("", "replace_file")
dir, err := os.MkdirTemp("", "replace_file")
require.NoError(t, err, "creating temp dir failed")

origFilename := filepath.Join(dir, "testfile")
Expand All @@ -176,7 +176,7 @@ func TestReplaceFile(t *testing.T) {
require.NoError(t, err, "opening original file failed")
defer ofr.Close()

res, err := ioutil.ReadAll(ofr)
res, err := io.ReadAll(ofr)
require.NoError(t, err, "reading original file failed")
require.Equal(t, "test", string(res), "unexpected file contents")
}
Expand Down
6 changes: 3 additions & 3 deletions notify/email/email_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ package email
import (
"context"
"fmt"
"io/ioutil"
"io"
"net/http"
"net/url"
"os"
Expand Down Expand Up @@ -128,7 +128,7 @@ func (m *mailDev) doEmailRequest(method, path string) (int, []byte, error) {
return 0, nil, err
}
defer res.Body.Close()
b, err := ioutil.ReadAll(res.Body)
b, err := io.ReadAll(res.Body)
if err != nil {
return 0, nil, err
}
Expand All @@ -145,7 +145,7 @@ type emailTestConfig struct {

func loadEmailTestConfiguration(f string) (emailTestConfig, error) {
c := emailTestConfig{}
b, err := ioutil.ReadFile(f)
b, err := os.ReadFile(f)
if err != nil {
return c, err
}
Expand Down
4 changes: 2 additions & 2 deletions notify/opsgenie/opsgenie.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ import (
"context"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"os"
"strings"

"github.com/go-kit/log"
Expand Down Expand Up @@ -276,7 +276,7 @@ func (n *Notifier) createRequests(ctx context.Context, as ...*types.Alert) ([]*h
if n.conf.APIKey != "" {
apiKey = tmpl(string(n.conf.APIKey))
} else {
content, err := ioutil.ReadFile(n.conf.APIKeyFile)
content, err := os.ReadFile(n.conf.APIKeyFile)
if err != nil {
return nil, false, errors.Wrap(err, "read key_file error")
}
Expand Down
7 changes: 4 additions & 3 deletions notify/opsgenie/opsgenie_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,10 @@ package opsgenie
import (
"context"
"fmt"
"io/ioutil"
"io"
"net/http"
"net/url"
"os"
"testing"
"time"

Expand Down Expand Up @@ -75,7 +76,7 @@ func TestGettingOpsGegineApikeyFromFile(t *testing.T) {

key := "key"

f, err := ioutil.TempFile("", "opsgenie_test")
f, err := os.CreateTemp("", "opsgenie_test")
require.NoError(t, err, "creating temp file failed")
_, err = f.WriteString(key)
require.NoError(t, err, "writing to temp file failed")
Expand Down Expand Up @@ -322,7 +323,7 @@ func TestOpsGenieWithUpdate(t *testing.T) {

func readBody(t *testing.T, r *http.Request) string {
t.Helper()
body, err := ioutil.ReadAll(r.Body)
body, err := io.ReadAll(r.Body)
require.NoError(t, err)
return string(body)
}
4 changes: 2 additions & 2 deletions notify/slack/slack.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ import (
"context"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"os"

"github.com/go-kit/log"
"github.com/go-kit/log/level"
Expand Down Expand Up @@ -190,7 +190,7 @@ func (n *Notifier) Notify(ctx context.Context, as ...*types.Alert) (bool, error)
if n.conf.APIURL != nil {
u = n.conf.APIURL.String()
} else {
content, err := ioutil.ReadFile(n.conf.APIURLFile)
content, err := os.ReadFile(n.conf.APIURLFile)
if err != nil {
return false, err
}
Expand Down
4 changes: 2 additions & 2 deletions notify/slack/slack_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ package slack

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

"github.com/go-kit/log"
Expand Down Expand Up @@ -63,7 +63,7 @@ func TestGettingSlackURLFromFile(t *testing.T) {
ctx, u, fn := test.GetContextWithCancelingURL()
defer fn()

f, err := ioutil.TempFile("", "slack_test")
f, err := os.CreateTemp("", "slack_test")
require.NoError(t, err, "creating temp file failed")
_, err = f.WriteString(u.String())
require.NoError(t, err, "writing to temp file failed")
Expand Down
5 changes: 2 additions & 3 deletions notify/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import (
"crypto/sha256"
"fmt"
"io"
"io/ioutil"
"net/http"
"net/url"

Expand Down Expand Up @@ -78,7 +77,7 @@ func request(ctx context.Context, client *http.Client, method, url, bodyType str
// Drain consumes and closes the response's body to make sure that the
// HTTP client can reuse existing connections.
func Drain(r *http.Response) {
io.Copy(ioutil.Discard, r.Body)
io.Copy(io.Discard, r.Body)
r.Body.Close()
}

Expand Down Expand Up @@ -161,7 +160,7 @@ func readAll(r io.Reader) string {
if r == nil {
return ""
}
bs, err := ioutil.ReadAll(r)
bs, err := io.ReadAll(r)
if err != nil {
return ""
}
Expand Down
3 changes: 1 addition & 2 deletions notify/util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ import (
"bytes"
"fmt"
"io"
"io/ioutil"
"net/http"
"testing"

Expand Down Expand Up @@ -156,7 +155,7 @@ func TestRetrierCheck(t *testing.T) {
if status != http.StatusServiceUnavailable {
return "invalid"
}
bs, _ := ioutil.ReadAll(b)
bs, _ := io.ReadAll(b)
return fmt.Sprintf("server response is %q", string(bs))
}},
status: http.StatusServiceUnavailable,
Expand Down
Loading

0 comments on commit b55097f

Please sign in to comment.