Don't use deprecated functions from io/ioutil

This commit is contained in:
Ingo Oppermann
2022-08-18 10:27:33 +03:00
parent 5bd04817cc
commit 6802830c62
16 changed files with 41 additions and 48 deletions

View File

@@ -5,7 +5,6 @@ import (
"crypto/tls" "crypto/tls"
"fmt" "fmt"
"io" "io"
"io/ioutil"
golog "log" golog "log"
gonet "net" gonet "net"
gohttp "net/http" gohttp "net/http"
@@ -120,7 +119,7 @@ func New(configpath string, logwriter io.Writer) (API, error) {
a.log.writer = logwriter a.log.writer = logwriter
if a.log.writer == nil { if a.log.writer == nil {
a.log.writer = ioutil.Discard a.log.writer = io.Discard
} }
a.errorChan = make(chan error, 1) a.errorChan = make(chan error, 1)

View File

@@ -6,7 +6,6 @@ package main
import ( import (
gojson "encoding/json" gojson "encoding/json"
"fmt" "fmt"
"io/ioutil"
"math" "math"
"net/url" "net/url"
"os" "os"
@@ -503,7 +502,7 @@ func importV1(path string, cfg importConfig) (store.StoreData, error) {
r := store.NewStoreData() r := store.NewStoreData()
jsondata, err := ioutil.ReadFile(path) jsondata, err := os.ReadFile(path)
if err != nil { if err != nil {
return r, fmt.Errorf("failed to read data from %s: %w", path, err) return r, fmt.Errorf("failed to read data from %s: %w", path, err)
} }

View File

@@ -2,7 +2,6 @@ package main
import ( import (
gojson "encoding/json" gojson "encoding/json"
"io/ioutil"
"os" "os"
"testing" "testing"
@@ -51,7 +50,7 @@ func testV1Import(t *testing.T, v1Fixture, v4Fixture string, config importConfig
require.Equal(t, nil, err) require.Equal(t, nil, err)
// Read the wanted result // Read the wanted result
wantdatav4, err := ioutil.ReadFile(v4Fixture) wantdatav4, err := os.ReadFile(v4Fixture)
require.Equal(t, nil, err) require.Equal(t, nil, err)
var wantv4 store.StoreData var wantv4 store.StoreData

View File

@@ -1,7 +1,7 @@
package config package config
import ( import (
"io/ioutil" "io"
"net/http" "net/http"
"sync" "sync"
"time" "time"
@@ -58,7 +58,7 @@ func doRequest(url string) string {
defer resp.Body.Close() defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body) body, err := io.ReadAll(resp.Body)
if err != nil { if err != nil {
return "" return ""
} }

View File

@@ -3,7 +3,7 @@ package skills
import ( import (
"bufio" "bufio"
"bytes" "bytes"
"io/ioutil" "os"
"regexp" "regexp"
) )
@@ -16,14 +16,14 @@ type alsaCard struct {
func DevicesALSA() ([]HWDevice, error) { func DevicesALSA() ([]HWDevice, error) {
devices := []HWDevice{} devices := []HWDevice{}
content, err := ioutil.ReadFile("/proc/asound/cards") content, err := os.ReadFile("/proc/asound/cards")
if err != nil { if err != nil {
return devices, err return devices, err
} }
cards := parseALSACards(content) cards := parseALSACards(content)
content, err = ioutil.ReadFile("/proc/asound/devices") content, err = os.ReadFile("/proc/asound/devices")
if err != nil { if err != nil {
return devices, err return devices, err
} }

View File

@@ -2,7 +2,7 @@ package resolver
import ( import (
"bytes" "bytes"
"io/ioutil" "io"
"net/http" "net/http"
"time" "time"
@@ -79,7 +79,7 @@ func (r *queryResolver) playoutRequest(method, addr, path, contentType string, d
defer response.Body.Close() defer response.Body.Close()
// Read the whole response // Read the whole response
data, err = ioutil.ReadAll(response.Body) data, err = io.ReadAll(response.Body)
if err != nil { if err != nil {
return nil, err return nil, err
} }

View File

@@ -1,7 +1,7 @@
package api package api
import ( import (
"io/ioutil" "io"
"net/http" "net/http"
"net/url" "net/url"
"sort" "sort"
@@ -96,7 +96,7 @@ func (h *MemFSHandler) PatchFile(c echo.Context) error {
req := c.Request() req := c.Request()
body, err := ioutil.ReadAll(req.Body) body, err := io.ReadAll(req.Body)
if err != nil { if err != nil {
return api.Err(http.StatusBadRequest, "Failed reading request body", "%s", err) return api.Err(http.StatusBadRequest, "Failed reading request body", "%s", err)
} }

View File

@@ -3,7 +3,7 @@ package api
import ( import (
"bytes" "bytes"
"encoding/json" "encoding/json"
"io/ioutil" "io"
"net/http" "net/http"
"strings" "strings"
"time" "time"
@@ -59,7 +59,7 @@ func (h *PlayoutHandler) Status(c echo.Context) error {
defer response.Body.Close() defer response.Body.Close()
// Read the whole response // Read the whole response
data, err := ioutil.ReadAll(response.Body) data, err := io.ReadAll(response.Body)
if err != nil { if err != nil {
return api.Err(http.StatusInternalServerError, "", "%s", err) return api.Err(http.StatusInternalServerError, "", "%s", err)
} }
@@ -122,7 +122,7 @@ func (h *PlayoutHandler) Keyframe(c echo.Context) error {
defer response.Body.Close() defer response.Body.Close()
// Read the whole response // Read the whole response
data, err := ioutil.ReadAll(response.Body) data, err := io.ReadAll(response.Body)
if err != nil { if err != nil {
return api.Err(http.StatusInternalServerError, "", "%s", err) return api.Err(http.StatusInternalServerError, "", "%s", err)
} }
@@ -162,7 +162,7 @@ func (h *PlayoutHandler) EncodeErrorframe(c echo.Context) error {
defer response.Body.Close() defer response.Body.Close()
// Read the whole response // Read the whole response
data, err := ioutil.ReadAll(response.Body) data, err := io.ReadAll(response.Body)
if err != nil { if err != nil {
return api.Err(http.StatusInternalServerError, "", "%s", err) return api.Err(http.StatusInternalServerError, "", "%s", err)
} }
@@ -195,7 +195,7 @@ func (h *PlayoutHandler) SetErrorframe(c echo.Context) error {
return api.Err(http.StatusNotFound, "Unknown process or input", "%s", err) return api.Err(http.StatusNotFound, "Unknown process or input", "%s", err)
} }
data, err := ioutil.ReadAll(c.Request().Body) data, err := io.ReadAll(c.Request().Body)
if err != nil { if err != nil {
return api.Err(http.StatusBadRequest, "Failed to read request body", "%s", err) return api.Err(http.StatusBadRequest, "Failed to read request body", "%s", err)
} }
@@ -210,7 +210,7 @@ func (h *PlayoutHandler) SetErrorframe(c echo.Context) error {
defer response.Body.Close() defer response.Body.Close()
// Read the whole response // Read the whole response
data, err = ioutil.ReadAll(response.Body) data, err = io.ReadAll(response.Body)
if err != nil { if err != nil {
return api.Err(http.StatusInternalServerError, "", "%s", err) return api.Err(http.StatusInternalServerError, "", "%s", err)
} }
@@ -249,7 +249,7 @@ func (h *PlayoutHandler) ReopenInput(c echo.Context) error {
defer response.Body.Close() defer response.Body.Close()
// Read the whole response // Read the whole response
data, err := ioutil.ReadAll(response.Body) data, err := io.ReadAll(response.Body)
if err != nil { if err != nil {
return api.Err(http.StatusInternalServerError, "", "%s", err) return api.Err(http.StatusInternalServerError, "", "%s", err)
} }
@@ -281,7 +281,7 @@ func (h *PlayoutHandler) SetStream(c echo.Context) error {
return api.Err(http.StatusNotFound, "Unknown process or input", "%s", err) return api.Err(http.StatusNotFound, "Unknown process or input", "%s", err)
} }
data, err := ioutil.ReadAll(c.Request().Body) data, err := io.ReadAll(c.Request().Body)
if err != nil { if err != nil {
return api.Err(http.StatusBadRequest, "Failed to read request body", "%s", err) return api.Err(http.StatusBadRequest, "Failed to read request body", "%s", err)
} }
@@ -296,7 +296,7 @@ func (h *PlayoutHandler) SetStream(c echo.Context) error {
defer response.Body.Close() defer response.Body.Close()
// Read the whole response // Read the whole response
data, err = ioutil.ReadAll(response.Body) data, err = io.ReadAll(response.Body)
if err != nil { if err != nil {
return api.Err(http.StatusInternalServerError, "", "%s", err) return api.Err(http.StatusInternalServerError, "", "%s", err)
} }

View File

@@ -2,7 +2,7 @@ package util
import ( import (
"fmt" "fmt"
"io/ioutil" "io"
"net/url" "net/url"
"strings" "strings"
@@ -24,7 +24,7 @@ func ShouldBindJSONValidation(c echo.Context, obj interface{}, validate bool) er
return fmt.Errorf("request doesn't contain JSON content") return fmt.Errorf("request doesn't contain JSON content")
} }
body, err := ioutil.ReadAll(req.Body) body, err := io.ReadAll(req.Body)
if err != nil { if err != nil {
return err return err
} }

View File

@@ -6,7 +6,7 @@ import (
"crypto" "crypto"
"encoding/json" "encoding/json"
"errors" "errors"
"io/ioutil" "io"
"net/http" "net/http"
"sync" "sync"
"time" "time"
@@ -324,7 +324,7 @@ func (j *jwksImpl) refresh() (err error) {
// Read the raw JWKs from the body of the response. // Read the raw JWKs from the body of the response.
var jwksBytes []byte var jwksBytes []byte
if jwksBytes, err = ioutil.ReadAll(resp.Body); err != nil { if jwksBytes, err = io.ReadAll(resp.Body); err != nil {
return err return err
} }

View File

@@ -4,7 +4,6 @@ import (
"bufio" "bufio"
"compress/gzip" "compress/gzip"
"io" "io"
"io/ioutil"
"net" "net"
"net/http" "net/http"
"strings" "strings"
@@ -108,7 +107,7 @@ func NewWithConfig(config Config) echo.MiddlewareFunc {
// nothing is written to body or error is returned. // nothing is written to body or error is returned.
// See issue #424, #407. // See issue #424, #407.
res.Writer = rw res.Writer = rw
w.Reset(ioutil.Discard) w.Reset(io.Discard)
} }
w.Close() w.Close()
pool.Put(w) pool.Put(w)
@@ -183,7 +182,7 @@ func (w *gzipResponseWriter) Push(target string, opts *http.PushOptions) error {
func gzipPool(config Config) sync.Pool { func gzipPool(config Config) sync.Pool {
return sync.Pool{ return sync.Pool{
New: func() interface{} { New: func() interface{} {
w, err := gzip.NewWriterLevel(ioutil.Discard, config.Level) w, err := gzip.NewWriterLevel(io.Discard, config.Level)
if err != nil { if err != nil {
return err return err
} }

View File

@@ -5,9 +5,9 @@ import (
"encoding/json" "encoding/json"
"fmt" "fmt"
"io" "io"
"io/ioutil"
"net/http" "net/http"
"net/http/httptest" "net/http/httptest"
"os"
"path/filepath" "path/filepath"
"strings" "strings"
"testing" "testing"
@@ -57,7 +57,7 @@ func DummyEcho() *echo.Echo {
router.HideBanner = true router.HideBanner = true
router.HidePort = true router.HidePort = true
router.HTTPErrorHandler = errorhandler.HTTPErrorHandler router.HTTPErrorHandler = errorhandler.HTTPErrorHandler
router.Logger.SetOutput(ioutil.Discard) router.Logger.SetOutput(io.Discard)
router.Validator = validator.New() router.Validator = validator.New()
return router return router
@@ -89,7 +89,7 @@ func CheckResponse(t *testing.T, res *http.Response) *Response {
Code: res.StatusCode, Code: res.StatusCode,
} }
body, err := ioutil.ReadAll(res.Body) body, err := io.ReadAll(res.Body)
require.Equal(t, nil, err) require.Equal(t, nil, err)
if strings.Contains(res.Header.Get("Content-Type"), "application/json") { if strings.Contains(res.Header.Get("Content-Type"), "application/json") {
@@ -122,7 +122,7 @@ func Validate(t *testing.T, datatype, data interface{}) bool {
} }
func Read(t *testing.T, path string) io.Reader { func Read(t *testing.T, path string) io.Reader {
data, err := ioutil.ReadFile(path) data, err := os.ReadFile(path)
require.Equal(t, nil, err) require.Equal(t, nil, err)
return bytes.NewReader(data) return bytes.NewReader(data)

View File

@@ -3,7 +3,6 @@ package store
import ( import (
gojson "encoding/json" gojson "encoding/json"
"fmt" "fmt"
"io/ioutil"
"os" "os"
"sync" "sync"
@@ -78,7 +77,7 @@ func (s *jsonStore) store(data StoreData) error {
return err return err
} }
tmpfile, err := ioutil.TempFile(s.dir, s.filename) tmpfile, err := os.CreateTemp(s.dir, s.filename)
if err != nil { if err != nil {
return err return err
} }
@@ -122,7 +121,7 @@ func (s *jsonStore) load(version uint64) (StoreData, error) {
return r, err return r, err
} }
jsondata, err := ioutil.ReadFile(filename) jsondata, err := os.ReadFile(filename)
if err != nil { if err != nil {
return r, err return r, err
} }

View File

@@ -6,7 +6,6 @@ import (
"errors" "errors"
"fmt" "fmt"
"io" "io"
"io/ioutil"
"net/http" "net/http"
"strings" "strings"
"time" "time"
@@ -149,14 +148,14 @@ func (a *api) refreshAccessToken(refreshToken string) error {
return errStatusAuthorization return errStatusAuthorization
} }
data, _ := ioutil.ReadAll(res.Body) data, _ := io.ReadAll(res.Body)
return statusError{ return statusError{
code: res.StatusCode, code: res.StatusCode,
response: data, response: data,
} }
} }
data, err := ioutil.ReadAll(res.Body) data, err := io.ReadAll(res.Body)
if err != nil { if err != nil {
return statusError{ return statusError{
code: res.StatusCode, code: res.StatusCode,
@@ -199,14 +198,14 @@ func (a *api) call(method, path string, body io.Reader) ([]byte, error) {
return nil, errStatusAuthorization return nil, errStatusAuthorization
} }
data, _ := ioutil.ReadAll(res.Body) data, _ := io.ReadAll(res.Body)
return nil, statusError{ return nil, statusError{
code: res.StatusCode, code: res.StatusCode,
response: data, response: data,
} }
} }
data, err := ioutil.ReadAll(res.Body) data, err := io.ReadAll(res.Body)
if err != nil { if err != nil {
return nil, fmt.Errorf("error reading response: %w", statusError{ return nil, fmt.Errorf("error reading response: %w", statusError{
code: res.StatusCode, code: res.StatusCode,

View File

@@ -3,7 +3,6 @@ package session
import ( import (
"context" "context"
"encoding/json" "encoding/json"
"io/ioutil"
"os" "os"
"path/filepath" "path/filepath"
"sort" "sort"
@@ -461,7 +460,7 @@ func (c *collector) loadHistory(path string, data *history) {
c.lock.persist.Lock() c.lock.persist.Lock()
defer c.lock.persist.Unlock() defer c.lock.persist.Unlock()
jsondata, err := ioutil.ReadFile(path) jsondata, err := os.ReadFile(path)
if err != nil { if err != nil {
return return
} }
@@ -489,7 +488,7 @@ func (c *collector) saveHistory(path string, data *history) {
dir := filepath.Dir(path) dir := filepath.Dir(path)
filename := filepath.Base(path) filename := filepath.Base(path)
tmpfile, err := ioutil.TempFile(dir, filename) tmpfile, err := os.CreateTemp(dir, filename)
if err != nil { if err != nil {
return return
} }

View File

@@ -5,7 +5,7 @@ import (
"context" "context"
"encoding/json" "encoding/json"
"fmt" "fmt"
"io/ioutil" "io"
"net/http" "net/http"
"regexp" "regexp"
"sync" "sync"
@@ -179,7 +179,7 @@ func (s *checker) check() error {
return fmt.Errorf("request failed: %s", http.StatusText(res.StatusCode)) return fmt.Errorf("request failed: %s", http.StatusText(res.StatusCode))
} }
body, err := ioutil.ReadAll(res.Body) body, err := io.ReadAll(res.Body)
if err != nil { if err != nil {
return fmt.Errorf("error reading response: %w", err) return fmt.Errorf("error reading response: %w", err)
} }