Remove deprecated io/ioutil package

Miniflux now requires at least Go 1.16 and io/util is deprecated.

https://golang.org/doc/go1.16#ioutil
This commit is contained in:
Frédéric Guillot 2021-02-16 21:19:03 -08:00 committed by fguillot
parent 713d575bad
commit a352aff93b
16 changed files with 26 additions and 36 deletions

View file

@ -20,7 +20,7 @@ package main
import ( import (
"fmt" "fmt"
"io/ioutil" "os"
miniflux "miniflux.app/client" miniflux "miniflux.app/client"
) )
@ -47,7 +47,7 @@ func main() {
return return
} }
err = ioutil.WriteFile("opml.xml", opml, 0644) err = os.WriteFile("opml.xml", opml, 0644)
if err != nil { if err != nil {
fmt.Println(err) fmt.Println(err)
return return

View file

@ -8,7 +8,6 @@ import (
"encoding/json" "encoding/json"
"fmt" "fmt"
"io" "io"
"io/ioutil"
"net/url" "net/url"
"strconv" "strconv"
) )
@ -270,7 +269,7 @@ func (c *Client) Export() ([]byte, error) {
} }
defer body.Close() defer body.Close()
opml, err := ioutil.ReadAll(body) opml, err := io.ReadAll(body)
if err != nil { if err != nil {
return nil, err return nil, err
} }

View file

@ -10,7 +10,6 @@ import (
"errors" "errors"
"fmt" "fmt"
"io" "io"
"io/ioutil"
"log" "log"
"net/http" "net/http"
"net/url" "net/url"
@ -87,7 +86,7 @@ func (r *request) execute(method, path string, data interface{}) (io.ReadCloser,
case io.ReadCloser: case io.ReadCloser:
request.Body = data.(io.ReadCloser) request.Body = data.(io.ReadCloser)
default: default:
request.Body = ioutil.NopCloser(bytes.NewBuffer(r.toJSON(data))) request.Body = io.NopCloser(bytes.NewBuffer(r.toJSON(data)))
} }
} }

View file

@ -5,7 +5,6 @@
package config // import "miniflux.app/config" package config // import "miniflux.app/config"
import ( import (
"io/ioutil"
"os" "os"
"testing" "testing"
) )
@ -1309,7 +1308,7 @@ DEBUG = yes
Invalid text Invalid text
`) `)
tmpfile, err := ioutil.TempFile(".", "miniflux.*.unit_test.conf") tmpfile, err := os.CreateTemp(".", "miniflux.*.unit_test.conf")
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }

View file

@ -10,7 +10,6 @@ import (
"errors" "errors"
"fmt" "fmt"
"io" "io"
"io/ioutil"
url_parser "net/url" url_parser "net/url"
"os" "os"
"strconv" "strconv"
@ -269,7 +268,7 @@ func parseStringList(value string, fallback []string) []string {
} }
func readSecretFile(filename, fallback string) string { func readSecretFile(filename, fallback string) string {
data, err := ioutil.ReadFile(filename) data, err := os.ReadFile(filename)
if err != nil { if err != nil {
return fallback return fallback
} }

View file

@ -10,7 +10,6 @@ import (
"crypto/sha256" "crypto/sha256"
"encoding/base64" "encoding/base64"
"fmt" "fmt"
"io/ioutil"
"os" "os"
"path" "path"
"path/filepath" "path/filepath"
@ -66,7 +65,7 @@ func NewBundle(pkg, mapName, importPath string) *Bundle {
} }
func readFile(filename string) []byte { func readFile(filename string) []byte {
data, err := ioutil.ReadFile(filename) data, err := os.ReadFile(filename)
if err != nil { if err != nil {
panic(err) panic(err)
} }

View file

@ -10,7 +10,6 @@ import (
"encoding/json" "encoding/json"
"fmt" "fmt"
"io" "io"
"io/ioutil"
"net" "net"
"net/http" "net/http"
"net/url" "net/url"
@ -219,7 +218,7 @@ func (c *Client) executeRequest(request *http.Request) (*Response, error) {
return nil, fmt.Errorf("client: response too large (%d bytes)", resp.ContentLength) return nil, fmt.Errorf("client: response too large (%d bytes)", resp.ContentLength)
} }
buf, err := ioutil.ReadAll(resp.Body) buf, err := io.ReadAll(resp.Body)
if err != nil { if err != nil {
return nil, fmt.Errorf("client: error while reading body %v", err) return nil, fmt.Errorf("client: error while reading body %v", err)
} }

View file

@ -8,7 +8,6 @@ import (
"bytes" "bytes"
"fmt" "fmt"
"io" "io"
"io/ioutil"
"regexp" "regexp"
"strings" "strings"
"unicode/utf8" "unicode/utf8"
@ -87,7 +86,7 @@ func (r *Response) IsModified(etag, lastModified string) bool {
// - Feeds with encoding specified only in XML document and not in HTTP header // - Feeds with encoding specified only in XML document and not in HTTP header
// - Feeds with wrong encoding defined and already in UTF-8 // - Feeds with wrong encoding defined and already in UTF-8
func (r *Response) EnsureUnicodeBody() (err error) { func (r *Response) EnsureUnicodeBody() (err error) {
buffer, err := ioutil.ReadAll(r.Body) buffer, err := io.ReadAll(r.Body)
if err != nil { if err != nil {
return err return err
} }
@ -116,6 +115,6 @@ func (r *Response) EnsureUnicodeBody() (err error) {
// BodyAsString returns the response body as string. // BodyAsString returns the response body as string.
func (r *Response) BodyAsString() string { func (r *Response) BodyAsString() string {
bytes, _ := ioutil.ReadAll(r.Body) bytes, _ := io.ReadAll(r.Body)
return string(bytes) return string(bytes)
} }

View file

@ -6,7 +6,7 @@ package client // import "miniflux.app/http/client"
import ( import (
"bytes" "bytes"
"io/ioutil" "os"
"strings" "strings"
"testing" "testing"
"unicode/utf8" "unicode/utf8"
@ -129,7 +129,7 @@ func TestEnsureUnicodeWithHTMLDocuments(t *testing.T) {
} }
for _, tc := range unicodeTestCases { for _, tc := range unicodeTestCases {
content, err := ioutil.ReadFile("testdata/" + tc.filename) content, err := os.ReadFile("testdata/" + tc.filename)
if err != nil { if err != nil {
t.Fatalf(`Unable to read file %q: %v`, tc.filename, err) t.Fatalf(`Unable to read file %q: %v`, tc.filename, err)
} }

View file

@ -7,7 +7,7 @@ package pocket // import "miniflux.app/integration/pocket"
import ( import (
"errors" "errors"
"fmt" "fmt"
"io/ioutil" "io"
"net/url" "net/url"
"miniflux.app/http/client" "miniflux.app/http/client"
@ -35,7 +35,7 @@ func (c *Connector) RequestToken(redirectURL string) (string, error) {
return "", fmt.Errorf("pocket: unable to fetch request token, status=%d", response.StatusCode) return "", fmt.Errorf("pocket: unable to fetch request token, status=%d", response.StatusCode)
} }
body, err := ioutil.ReadAll(response.Body) body, err := io.ReadAll(response.Body)
if err != nil { if err != nil {
return "", fmt.Errorf("pocket: unable to read response body: %v", err) return "", fmt.Errorf("pocket: unable to read response body: %v", err)
} }
@ -70,7 +70,7 @@ func (c *Connector) AccessToken(requestToken string) (string, error) {
return "", fmt.Errorf("pocket: unable to fetch access token, status=%d", response.StatusCode) return "", fmt.Errorf("pocket: unable to fetch access token, status=%d", response.StatusCode)
} }
body, err := ioutil.ReadAll(response.Body) body, err := io.ReadAll(response.Body)
if err != nil { if err != nil {
return "", fmt.Errorf("pocket: unable to read response body: %v", err) return "", fmt.Errorf("pocket: unable to read response body: %v", err)
} }

View file

@ -7,7 +7,6 @@ package encoding // import "miniflux.app/reader/encoding"
import ( import (
"bytes" "bytes"
"io" "io"
"io/ioutil"
"unicode/utf8" "unicode/utf8"
"golang.org/x/net/html/charset" "golang.org/x/net/html/charset"
@ -25,7 +24,7 @@ import (
// - Feeds with encoding specified only in XML document and not in HTTP header // - Feeds with encoding specified only in XML document and not in HTTP header
// - Feeds with wrong encoding defined and already in UTF-8 // - Feeds with wrong encoding defined and already in UTF-8
func CharsetReader(label string, input io.Reader) (io.Reader, error) { func CharsetReader(label string, input io.Reader) (io.Reader, error) {
buffer, _ := ioutil.ReadAll(input) buffer, _ := io.ReadAll(input)
r := bytes.NewReader(buffer) r := bytes.NewReader(buffer)
// The document is already UTF-8, do not do anything (avoid double-encoding). // The document is already UTF-8, do not do anything (avoid double-encoding).

View file

@ -8,7 +8,6 @@ import (
"encoding/base64" "encoding/base64"
"fmt" "fmt"
"io" "io"
"io/ioutil"
"strings" "strings"
"miniflux.app/config" "miniflux.app/config"
@ -104,7 +103,7 @@ func downloadIcon(iconURL string, fetchViaProxy bool) (*model.Icon, error) {
return nil, fmt.Errorf("unable to download icon: status=%d", response.StatusCode) return nil, fmt.Errorf("unable to download icon: status=%d", response.StatusCode)
} }
body, err := ioutil.ReadAll(response.Body) body, err := io.ReadAll(response.Body)
if err != nil { if err != nil {
return nil, fmt.Errorf("unable to read downloaded icon: %v", err) return nil, fmt.Errorf("unable to read downloaded icon: %v", err)
} }

View file

@ -6,7 +6,7 @@ package parser // import "miniflux.app/reader/parser"
import ( import (
"bytes" "bytes"
"io/ioutil" "os"
"testing" "testing"
"miniflux.app/http/client" "miniflux.app/http/client"
@ -329,7 +329,7 @@ func TestDifferentEncodingWithResponse(t *testing.T) {
} }
for _, tc := range unicodeTestCases { for _, tc := range unicodeTestCases {
content, err := ioutil.ReadFile("testdata/" + tc.filename) content, err := os.ReadFile("testdata/" + tc.filename)
if err != nil { if err != nil {
t.Fatalf(`Unable to read file %q: %v`, tc.filename, err) t.Fatalf(`Unable to read file %q: %v`, tc.filename, err)
} }

View file

@ -6,7 +6,7 @@ package scraper // import "miniflux.app/reader/scraper"
import ( import (
"bytes" "bytes"
"io/ioutil" "os"
"strings" "strings"
"testing" "testing"
) )
@ -54,7 +54,7 @@ func TestSelectorRules(t *testing.T) {
} }
for filename, rule := range ruleTestCases { for filename, rule := range ruleTestCases {
html, err := ioutil.ReadFile("testdata/" + filename) html, err := os.ReadFile("testdata/" + filename)
if err != nil { if err != nil {
t.Fatalf(`Unable to read file %q: %v`, filename, err) t.Fatalf(`Unable to read file %q: %v`, filename, err)
} }
@ -64,7 +64,7 @@ func TestSelectorRules(t *testing.T) {
t.Fatalf(`Scraping error for %q - %q: %v`, filename, rule, err) t.Fatalf(`Scraping error for %q - %q: %v`, filename, rule, err)
} }
expectedResult, err := ioutil.ReadFile("testdata/" + filename + "-result") expectedResult, err := os.ReadFile("testdata/" + filename + "-result")
if err != nil { if err != nil {
t.Fatalf(`Unable to read file %q: %v`, filename, err) t.Fatalf(`Unable to read file %q: %v`, filename, err)
} }

View file

@ -9,7 +9,6 @@ import (
"encoding/xml" "encoding/xml"
"fmt" "fmt"
"io" "io"
"io/ioutil"
"strings" "strings"
"miniflux.app/reader/encoding" "miniflux.app/reader/encoding"
@ -18,7 +17,7 @@ import (
// NewDecoder returns a XML decoder that filters illegal characters. // NewDecoder returns a XML decoder that filters illegal characters.
func NewDecoder(data io.Reader) *xml.Decoder { func NewDecoder(data io.Reader) *xml.Decoder {
var decoder *xml.Decoder var decoder *xml.Decoder
buffer, _ := ioutil.ReadAll(data) buffer, _ := io.ReadAll(data)
enc := procInst("encoding", string(buffer)) enc := procInst("encoding", string(buffer))
if enc != "" && enc != "utf-8" && enc != "UTF-8" && !strings.EqualFold(enc, "utf-8") { if enc != "" && enc != "utf-8" && enc != "UTF-8" && !strings.EqualFold(enc, "utf-8") {
// filter invalid chars later within decoder.CharsetReader // filter invalid chars later within decoder.CharsetReader
@ -36,7 +35,7 @@ func NewDecoder(data io.Reader) *xml.Decoder {
if err != nil { if err != nil {
return nil, err return nil, err
} }
rawData, err := ioutil.ReadAll(utf8Reader) rawData, err := io.ReadAll(utf8Reader)
if err != nil { if err != nil {
return nil, fmt.Errorf("Unable to read data: %q", err) return nil, fmt.Errorf("Unable to read data: %q", err)
} }

View file

@ -8,7 +8,7 @@ package tests
import ( import (
"bytes" "bytes"
"io/ioutil" "io"
"strings" "strings"
"testing" "testing"
) )
@ -39,7 +39,7 @@ func TestImport(t *testing.T) {
</opml>` </opml>`
b := bytes.NewReader([]byte(data)) b := bytes.NewReader([]byte(data))
err := client.Import(ioutil.NopCloser(b)) err := client.Import(io.NopCloser(b))
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }