Skip to content

Commit

Permalink
Fix ioutil deprecations
Browse files Browse the repository at this point in the history
  • Loading branch information
mat committed Oct 3, 2024
1 parent 75837db commit 0940eb6
Show file tree
Hide file tree
Showing 10 changed files with 23 additions and 26 deletions.
7 changes: 4 additions & 3 deletions besticon/besticon.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
}
Expand Down
4 changes: 2 additions & 2 deletions besticon/besticon/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package main
import (
"flag"
"fmt"
"io/ioutil"
"io"
"os"

"github.com/mat/besticon/v3/besticon"
Expand All @@ -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)
Expand Down
3 changes: 1 addition & 2 deletions besticon/besticon_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"fmt"
"image"
"image/color"
"io/ioutil"
"net/url"
"os"
"reflect"
Expand Down Expand Up @@ -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
}
Expand Down
3 changes: 1 addition & 2 deletions besticon/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package besticon
import (
"errors"
"io"
"io/ioutil"
"net"
"net/http"
"net/http/cookiejar"
Expand Down Expand Up @@ -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 {
Expand Down
4 changes: 2 additions & 2 deletions besticon/iconserver/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package main

import (
"fmt"
"io/ioutil"
"io"
"log"
"net/http"
"net/http/httptest"
Expand Down Expand Up @@ -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))),
}
}
5 changes: 2 additions & 3 deletions colorfinder/colorfinder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"fmt"
"image"
"io"
"io/ioutil"
"log"
"os"
"reflect"
Expand Down Expand Up @@ -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 {
Expand All @@ -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 {
Expand Down
3 changes: 1 addition & 2 deletions ico/ico.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"errors"
"image"
"io"
"io/ioutil"

"image/png"

Expand Down Expand Up @@ -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
}
Expand Down
2 changes: 1 addition & 1 deletion lettericon/lettericon.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
9 changes: 5 additions & 4 deletions lettericon/lettericon_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ import (
"bytes"
"fmt"
"image/color"
"io/ioutil"
"io"
"os"
"reflect"
"testing"
)
Expand Down Expand Up @@ -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
}
Expand All @@ -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)
}
}

Expand Down
9 changes: 4 additions & 5 deletions vcr/vcr.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"errors"
"fmt"
"io"
"io/ioutil"
"net/http"
"net/http/httputil"
"os"
Expand Down Expand Up @@ -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)
}
Expand Down Expand Up @@ -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)
}
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit 0940eb6

Please sign in to comment.