Skip to content

Commit 791e542

Browse files
committed
Remove ioutil
Signed-off-by: inosato <[email protected]>
1 parent b1438cc commit 791e542

24 files changed

+57
-61
lines changed

api/v1/api_test.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import (
1818
"encoding/json"
1919
"errors"
2020
"fmt"
21-
"io/ioutil"
21+
"io"
2222
"net/http"
2323
"net/http/httptest"
2424
"regexp"
@@ -150,7 +150,7 @@ func TestAddAlerts(t *testing.T) {
150150

151151
api.addAlerts(w, r)
152152
res := w.Result()
153-
body, _ := ioutil.ReadAll(res.Body)
153+
body, _ := io.ReadAll(res.Body)
154154

155155
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)))
156156
}
@@ -282,7 +282,7 @@ func TestListAlerts(t *testing.T) {
282282
w := httptest.NewRecorder()
283283

284284
api.listAlerts(w, r)
285-
body, _ := ioutil.ReadAll(w.Result().Body)
285+
body, _ := io.ReadAll(w.Result().Body)
286286

287287
var res response
288288
err = json.Unmarshal(body, &res)

api/v2/api_test.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ package v2
1616
import (
1717
"bytes"
1818
"fmt"
19-
"io/ioutil"
19+
"io"
2020
"net/http"
2121
"net/http/httptest"
2222
"strconv"
@@ -204,7 +204,7 @@ func TestDeleteSilenceHandler(t *testing.T) {
204204
HTTPRequest: r,
205205
})
206206
responder.WriteResponse(w, p)
207-
body, _ := ioutil.ReadAll(w.Result().Body)
207+
body, _ := io.ReadAll(w.Result().Body)
208208

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

295295
require.Equal(t, tc.expectedCode, w.Code, fmt.Sprintf("test case: %d, response: %s", i, string(body)))
296296
})

cli/config/config.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
package config
1515

1616
import (
17-
"io/ioutil"
1817
"os"
1918

2019
"gopkg.in/alecthomas/kingpin.v2"
@@ -37,7 +36,7 @@ func NewResolver(files []string, legacyFlags map[string]string) (*Resolver, erro
3736
if _, err := os.Stat(f); err != nil {
3837
continue
3938
}
40-
b, err := ioutil.ReadFile(f)
39+
b, err := os.ReadFile(f)
4140
if err != nil {
4241
if os.IsNotExist(err) {
4342
continue

cli/config/config_test.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
package config
1515

1616
import (
17-
"io/ioutil"
17+
"io"
1818
"testing"
1919

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

3232
app := kingpin.New("app", "")
33-
app.UsageWriter(ioutil.Discard)
34-
app.ErrorWriter(ioutil.Discard)
33+
app.UsageWriter(io.Discard)
34+
app.ErrorWriter(io.Discard)
3535
app.Terminate(nil)
3636

3737
app.Flag("url", "").StringVar(url)

cli/config/http_config.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
package config
1515

1616
import (
17-
"io/ioutil"
17+
"os"
1818
"path/filepath"
1919

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

2424
// LoadHTTPConfigFile returns HTTPClientConfig for the given http_config file
2525
func LoadHTTPConfigFile(filename string) (*promconfig.HTTPClientConfig, error) {
26-
b, err := ioutil.ReadFile(filename)
26+
b, err := os.ReadFile(filename)
2727
if err != nil {
2828
return nil, err
2929
}

cli/template_render.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import (
1717
"context"
1818
"encoding/json"
1919
"fmt"
20-
"io/ioutil"
20+
"io"
2121
"os"
2222
"time"
2323

@@ -121,7 +121,7 @@ func (c *templateRenderCmd) render(ctx context.Context, _ *kingpin.ParseContext)
121121
if c.templateData == nil {
122122
data = defaultData
123123
} else {
124-
content, err := ioutil.ReadAll(c.templateData)
124+
content, err := io.ReadAll(c.templateData)
125125
if err != nil {
126126
return err
127127
}

cluster/tls_config.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
package cluster
1515

1616
import (
17-
"io/ioutil"
17+
"os"
1818
"path/filepath"
1919

2020
"github.com/prometheus/common/config"
@@ -31,7 +31,7 @@ func GetTLSTransportConfig(configPath string) (*TLSTransportConfig, error) {
3131
if configPath == "" {
3232
return nil, nil
3333
}
34-
bytes, err := ioutil.ReadFile(configPath)
34+
bytes, err := os.ReadFile(configPath)
3535
if err != nil {
3636
return nil, err
3737
}

config/config.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,9 @@ package config
1616
import (
1717
"encoding/json"
1818
"fmt"
19-
"io/ioutil"
2019
"net"
2120
"net/url"
21+
"os"
2222
"path/filepath"
2323
"regexp"
2424
"sort"
@@ -192,7 +192,7 @@ func Load(s string) (*Config, error) {
192192

193193
// LoadFile parses the given YAML file into a Config.
194194
func LoadFile(filename string) (*Config, error) {
195-
content, err := ioutil.ReadFile(filename)
195+
content, err := os.ReadFile(filename)
196196
if err != nil {
197197
return nil, err
198198
}

examples/webhook/echo.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,14 @@ package main
1616
import (
1717
"bytes"
1818
"encoding/json"
19-
"io/ioutil"
19+
"io"
2020
"log"
2121
"net/http"
2222
)
2323

2424
func main() {
2525
log.Fatal(http.ListenAndServe(":5001", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
26-
b, err := ioutil.ReadAll(r.Body)
26+
b, err := io.ReadAll(r.Body)
2727
if err != nil {
2828
panic(err)
2929
}

nflog/nflog_test.go

+5-5
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ package nflog
1515

1616
import (
1717
"bytes"
18-
"io/ioutil"
18+
"io"
1919
"os"
2020
"path/filepath"
2121
"sync"
@@ -98,7 +98,7 @@ func TestLogSnapshot(t *testing.T) {
9898
}
9999

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

104104
l1 := &Log{
@@ -127,7 +127,7 @@ func TestLogSnapshot(t *testing.T) {
127127
}
128128

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

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

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

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

179-
res, err := ioutil.ReadAll(ofr)
179+
res, err := io.ReadAll(ofr)
180180
require.NoError(t, err, "reading original file failed")
181181
require.Equal(t, "test", string(res), "unexpected file contents")
182182
}

notify/email/email_test.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ package email
3131
import (
3232
"context"
3333
"fmt"
34-
"io/ioutil"
34+
"io"
3535
"net/http"
3636
"net/url"
3737
"os"
@@ -128,7 +128,7 @@ func (m *mailDev) doEmailRequest(method, path string) (int, []byte, error) {
128128
return 0, nil, err
129129
}
130130
defer res.Body.Close()
131-
b, err := ioutil.ReadAll(res.Body)
131+
b, err := io.ReadAll(res.Body)
132132
if err != nil {
133133
return 0, nil, err
134134
}
@@ -145,7 +145,7 @@ type emailTestConfig struct {
145145

146146
func loadEmailTestConfiguration(f string) (emailTestConfig, error) {
147147
c := emailTestConfig{}
148-
b, err := ioutil.ReadFile(f)
148+
b, err := os.ReadFile(f)
149149
if err != nil {
150150
return c, err
151151
}

notify/opsgenie/opsgenie.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@ import (
1818
"context"
1919
"encoding/json"
2020
"fmt"
21-
"io/ioutil"
2221
"net/http"
22+
"os"
2323
"strings"
2424

2525
"github.com/go-kit/log"
@@ -276,7 +276,7 @@ func (n *Notifier) createRequests(ctx context.Context, as ...*types.Alert) ([]*h
276276
if n.conf.APIKey != "" {
277277
apiKey = tmpl(string(n.conf.APIKey))
278278
} else {
279-
content, err := ioutil.ReadFile(n.conf.APIKeyFile)
279+
content, err := os.ReadFile(n.conf.APIKeyFile)
280280
if err != nil {
281281
return nil, false, errors.Wrap(err, "read key_file error")
282282
}

notify/opsgenie/opsgenie_test.go

+4-3
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,10 @@ package opsgenie
1616
import (
1717
"context"
1818
"fmt"
19-
"io/ioutil"
19+
"io"
2020
"net/http"
2121
"net/url"
22+
"os"
2223
"testing"
2324
"time"
2425

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

7677
key := "key"
7778

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

323324
func readBody(t *testing.T, r *http.Request) string {
324325
t.Helper()
325-
body, err := ioutil.ReadAll(r.Body)
326+
body, err := io.ReadAll(r.Body)
326327
require.NoError(t, err)
327328
return string(body)
328329
}

notify/slack/slack.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@ import (
1818
"context"
1919
"encoding/json"
2020
"fmt"
21-
"io/ioutil"
2221
"net/http"
22+
"os"
2323

2424
"github.com/go-kit/log"
2525
"github.com/go-kit/log/level"
@@ -190,7 +190,7 @@ func (n *Notifier) Notify(ctx context.Context, as ...*types.Alert) (bool, error)
190190
if n.conf.APIURL != nil {
191191
u = n.conf.APIURL.String()
192192
} else {
193-
content, err := ioutil.ReadFile(n.conf.APIURLFile)
193+
content, err := os.ReadFile(n.conf.APIURLFile)
194194
if err != nil {
195195
return false, err
196196
}

notify/slack/slack_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ package slack
1515

1616
import (
1717
"fmt"
18-
"io/ioutil"
18+
"os"
1919
"testing"
2020

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

66-
f, err := ioutil.TempFile("", "slack_test")
66+
f, err := os.CreateTemp("", "slack_test")
6767
require.NoError(t, err, "creating temp file failed")
6868
_, err = f.WriteString(u.String())
6969
require.NoError(t, err, "writing to temp file failed")

notify/util.go

+2-3
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ import (
1818
"crypto/sha256"
1919
"fmt"
2020
"io"
21-
"io/ioutil"
2221
"net/http"
2322
"net/url"
2423

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

@@ -161,7 +160,7 @@ func readAll(r io.Reader) string {
161160
if r == nil {
162161
return ""
163162
}
164-
bs, err := ioutil.ReadAll(r)
163+
bs, err := io.ReadAll(r)
165164
if err != nil {
166165
return ""
167166
}

notify/util_test.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ import (
1717
"bytes"
1818
"fmt"
1919
"io"
20-
"io/ioutil"
2120
"net/http"
2221
"testing"
2322

@@ -156,7 +155,7 @@ func TestRetrierCheck(t *testing.T) {
156155
if status != http.StatusServiceUnavailable {
157156
return "invalid"
158157
}
159-
bs, _ := ioutil.ReadAll(b)
158+
bs, _ := io.ReadAll(b)
160159
return fmt.Sprintf("server response is %q", string(bs))
161160
}},
162161
status: http.StatusServiceUnavailable,

0 commit comments

Comments
 (0)