From 0940eb6fe071547ee78041e6cd8fe974ab9e9686 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matthias=20L=C3=BCdtke?= Date: Thu, 3 Oct 2024 11:47:08 +0200 Subject: [PATCH] Fix ioutil deprecations --- besticon/besticon.go | 7 ++++--- besticon/besticon/cmd.go | 4 ++-- besticon/besticon_test.go | 3 +-- besticon/http.go | 3 +-- besticon/iconserver/server_test.go | 4 ++-- colorfinder/colorfinder_test.go | 5 ++--- ico/ico.go | 3 +-- lettericon/lettericon.go | 2 +- lettericon/lettericon_test.go | 9 +++++---- vcr/vcr.go | 9 ++++----- 10 files changed, 23 insertions(+), 26 deletions(-) diff --git a/besticon/besticon.go b/besticon/besticon.go index 2366f6b2..0ea4fa74 100644 --- a/besticon/besticon.go +++ b/besticon/besticon.go @@ -7,15 +7,16 @@ import ( "crypto/sha1" "errors" "fmt" - "github.com/golang/groupcache" "image" "image/color" - "io/ioutil" + "io" "net/http" "net/url" "os" "strings" + "github.com/golang/groupcache" + // Load supported image formats. _ "image/gif" _ "image/jpeg" @@ -255,7 +256,7 @@ func (b *Besticon) fetchHTML(url string) ([]byte, *url.URL, error) { if e != nil { return nil, nil, e } - utf8bytes, e := ioutil.ReadAll(utf8reader) + utf8bytes, e := io.ReadAll(utf8reader) if e != nil { return nil, nil, e } diff --git a/besticon/besticon/cmd.go b/besticon/besticon/cmd.go index 0e0e12c8..dc7b6539 100644 --- a/besticon/besticon/cmd.go +++ b/besticon/besticon/cmd.go @@ -3,7 +3,7 @@ package main import ( "flag" "fmt" - "io/ioutil" + "io" "os" "github.com/mat/besticon/v3/besticon" @@ -20,7 +20,7 @@ func main() { url := os.Args[len(os.Args)-1] - b := besticon.New(besticon.WithLogger(besticon.NewDefaultLogger(ioutil.Discard))) // Disable verbose logging + b := besticon.New(besticon.WithLogger(besticon.NewDefaultLogger(io.Discard))) // Disable verbose logging finder := b.NewIconFinder() icons, err := finder.FetchIcons(url) diff --git a/besticon/besticon_test.go b/besticon/besticon_test.go index c4272f05..f5b583c5 100644 --- a/besticon/besticon_test.go +++ b/besticon/besticon_test.go @@ -4,7 +4,6 @@ import ( "fmt" "image" "image/color" - "io/ioutil" "net/url" "os" "reflect" @@ -361,7 +360,7 @@ func getImageWidthForFile(filename string) int { } func mustReadFile(filename string) []byte { - bytes, e := ioutil.ReadFile(filename) + bytes, e := os.ReadFile(filename) check(e) return bytes } diff --git a/besticon/http.go b/besticon/http.go index a34095fd..9db254dc 100644 --- a/besticon/http.go +++ b/besticon/http.go @@ -3,7 +3,6 @@ package besticon import ( "errors" "io" - "io/ioutil" "net" "net/http" "net/http/cookiejar" @@ -89,7 +88,7 @@ func isPrivateIP(ipAddr *net.IPAddr) bool { func (b *Besticon) GetBodyBytes(r *http.Response) ([]byte, error) { limitReader := io.LimitReader(r.Body, b.maxResponseBodySize) - data, e := ioutil.ReadAll(limitReader) + data, e := io.ReadAll(limitReader) r.Body.Close() if int64(len(data)) >= b.maxResponseBodySize { diff --git a/besticon/iconserver/server_test.go b/besticon/iconserver/server_test.go index a9b02a35..3029cbb6 100644 --- a/besticon/iconserver/server_test.go +++ b/besticon/iconserver/server_test.go @@ -2,7 +2,7 @@ package main import ( "fmt" - "io/ioutil" + "io" "log" "net/http" "net/http/httptest" @@ -300,6 +300,6 @@ func newTestServer() *server { return &server{ maxIconSize: 500, cacheDuration: 720 * time.Hour, - besticon: besticon.New(besticon.WithLogger(besticon.NewDefaultLogger(ioutil.Discard))), + besticon: besticon.New(besticon.WithLogger(besticon.NewDefaultLogger(io.Discard))), } } diff --git a/colorfinder/colorfinder_test.go b/colorfinder/colorfinder_test.go index 0227c33d..b75f058c 100644 --- a/colorfinder/colorfinder_test.go +++ b/colorfinder/colorfinder_test.go @@ -6,7 +6,6 @@ import ( "fmt" "image" "io" - "io/ioutil" "log" "os" "reflect" @@ -76,7 +75,7 @@ func TestFindColors11(t *testing.T) { func BenchmarkFindMainColor152x152(b *testing.B) { file, _ := os.Open(testdataDir + "icon02.png.gz") gzReader, _ := gzip.NewReader(file) - byts, _ := ioutil.ReadAll(gzReader) + byts, _ := io.ReadAll(gzReader) imgReader := bytes.NewReader(byts) img, _, err := image.Decode(imgReader) if err != nil { @@ -102,7 +101,7 @@ func BenchmarkFindMainColor152x152(b *testing.B) { func BenchmarkFindMainColor57x57(b *testing.B) { file, _ := os.Open(testdataDir + "icon07.png.gz") gzReader, _ := gzip.NewReader(file) - byts, _ := ioutil.ReadAll(gzReader) + byts, _ := io.ReadAll(gzReader) imgReader := bytes.NewReader(byts) img, _, err := image.Decode(imgReader) if err != nil { diff --git a/ico/ico.go b/ico/ico.go index ced6cb3b..bc23ef07 100644 --- a/ico/ico.go +++ b/ico/ico.go @@ -8,7 +8,6 @@ import ( "errors" "image" "io" - "io/ioutil" "image/png" @@ -169,7 +168,7 @@ var errInvalid = errors.New("ico: invalid ICO image") // Decode returns the largest image contained in the icon // which might be a bmp or png func Decode(r io.Reader) (image.Image, error) { - icoBytes, err := ioutil.ReadAll(r) + icoBytes, err := io.ReadAll(r) if err != nil { return nil, err } diff --git a/lettericon/lettericon.go b/lettericon/lettericon.go index dfd1149e..3fad2681 100644 --- a/lettericon/lettericon.go +++ b/lettericon/lettericon.go @@ -156,7 +156,7 @@ func foo(col uint32) float64 { } var ( - errMalformedColorString = errors.New("Malformed hex color string") + errMalformedColorString = errors.New("malformed hex color string") ) func ColorFromHex(hex string) (*color.RGBA, error) { diff --git a/lettericon/lettericon_test.go b/lettericon/lettericon_test.go index d3764a04..56f6b78a 100644 --- a/lettericon/lettericon_test.go +++ b/lettericon/lettericon_test.go @@ -4,7 +4,8 @@ import ( "bytes" "fmt" "image/color" - "io/ioutil" + "io" + "os" "reflect" "testing" ) @@ -114,7 +115,7 @@ func BenchmarkColorFromHex(b *testing.B) { } func bytesFromFile(file string) ([]byte, error) { - dat, err := ioutil.ReadFile(file) + dat, err := os.ReadFile(file) if err != nil { return nil, err } @@ -132,11 +133,11 @@ func renderPNGBytes(letter string, bgColor color.Color, width int) ([]byte, erro } func BenchmarkRenderPNG(b *testing.B) { - RenderPNG("X", DefaultBackgroundColor, 144, ioutil.Discard) // warmup + RenderPNG("X", DefaultBackgroundColor, 144, io.Discard) // warmup b.ResetTimer() for i := 0; i < b.N; i++ { - RenderPNG("X", DefaultBackgroundColor, 144, ioutil.Discard) + RenderPNG("X", DefaultBackgroundColor, 144, io.Discard) } } diff --git a/vcr/vcr.go b/vcr/vcr.go index 96e34607..4b575ef5 100644 --- a/vcr/vcr.go +++ b/vcr/vcr.go @@ -7,7 +7,6 @@ import ( "errors" "fmt" "io" - "io/ioutil" "net/http" "net/http/httputil" "os" @@ -77,11 +76,11 @@ func logResponse(w io.Writer, res *http.Response, body bool) { var err error if body { defer res.Body.Close() - bodyBytes, err = ioutil.ReadAll(res.Body) + bodyBytes, err = io.ReadAll(res.Body) if err != nil { fmt.Printf("could not record response: %s", err) } - res.Body = ioutil.NopCloser(bytes.NewReader(bodyBytes)) + res.Body = io.NopCloser(bytes.NewReader(bodyBytes)) } dumpResonse(w, res, bodyBytes) } @@ -141,7 +140,7 @@ func NewReplayerTransport(reader io.Reader) (*replayerTransport, error) { t := &replayerTransport{mutex: sync.Mutex{}} t.mutex.Lock() defer t.mutex.Unlock() - conversation, err := ioutil.ReadAll(reader) + conversation, err := io.ReadAll(reader) if err != nil { return nil, fmt.Errorf("vcr: failed to read vcr file: %s", err) } @@ -184,7 +183,7 @@ func NewReplayerTransport(reader io.Reader) (*replayerTransport, error) { if err == io.EOF || separatorReached { bodyReader := bytes.NewReader(bodyBytes) - res.Body = ioutil.NopCloser(bodyReader) + res.Body = io.NopCloser(bodyReader) break } else if err == nil { } else {