From 6802830c62c144781c1cfdfb5df82b188b94cb78 Mon Sep 17 00:00:00 2001 From: Ingo Oppermann Date: Thu, 18 Aug 2022 10:27:33 +0300 Subject: [PATCH] Don't use deprecated functions from io/ioutil --- app/api/api.go | 3 +-- app/import/import.go | 3 +-- app/import/import_test.go | 3 +-- config/ip.go | 4 ++-- ffmpeg/skills/alsa.go | 6 +++--- http/graph/resolver/resolver.go | 4 ++-- http/handler/api/memfs.go | 4 ++-- http/handler/api/playout.go | 18 +++++++++--------- http/handler/util/util.go | 4 ++-- http/jwt/jwks/jwks.go | 4 ++-- http/middleware/gzip/gzip.go | 5 ++--- http/mock/mock.go | 8 ++++---- restream/store/json.go | 5 ++--- service/api/api.go | 9 ++++----- session/collector.go | 5 ++--- update/update.go | 4 ++-- 16 files changed, 41 insertions(+), 48 deletions(-) diff --git a/app/api/api.go b/app/api/api.go index da8bd165..b5b95e26 100644 --- a/app/api/api.go +++ b/app/api/api.go @@ -5,7 +5,6 @@ import ( "crypto/tls" "fmt" "io" - "io/ioutil" golog "log" gonet "net" gohttp "net/http" @@ -120,7 +119,7 @@ func New(configpath string, logwriter io.Writer) (API, error) { a.log.writer = logwriter if a.log.writer == nil { - a.log.writer = ioutil.Discard + a.log.writer = io.Discard } a.errorChan = make(chan error, 1) diff --git a/app/import/import.go b/app/import/import.go index 524d1762..b453c4a9 100644 --- a/app/import/import.go +++ b/app/import/import.go @@ -6,7 +6,6 @@ package main import ( gojson "encoding/json" "fmt" - "io/ioutil" "math" "net/url" "os" @@ -503,7 +502,7 @@ func importV1(path string, cfg importConfig) (store.StoreData, error) { r := store.NewStoreData() - jsondata, err := ioutil.ReadFile(path) + jsondata, err := os.ReadFile(path) if err != nil { return r, fmt.Errorf("failed to read data from %s: %w", path, err) } diff --git a/app/import/import_test.go b/app/import/import_test.go index 5fb82ea5..e0e8f3d6 100644 --- a/app/import/import_test.go +++ b/app/import/import_test.go @@ -2,7 +2,6 @@ package main import ( gojson "encoding/json" - "io/ioutil" "os" "testing" @@ -51,7 +50,7 @@ func testV1Import(t *testing.T, v1Fixture, v4Fixture string, config importConfig require.Equal(t, nil, err) // Read the wanted result - wantdatav4, err := ioutil.ReadFile(v4Fixture) + wantdatav4, err := os.ReadFile(v4Fixture) require.Equal(t, nil, err) var wantv4 store.StoreData diff --git a/config/ip.go b/config/ip.go index 3e027817..1ac57d51 100644 --- a/config/ip.go +++ b/config/ip.go @@ -1,7 +1,7 @@ package config import ( - "io/ioutil" + "io" "net/http" "sync" "time" @@ -58,7 +58,7 @@ func doRequest(url string) string { defer resp.Body.Close() - body, err := ioutil.ReadAll(resp.Body) + body, err := io.ReadAll(resp.Body) if err != nil { return "" } diff --git a/ffmpeg/skills/alsa.go b/ffmpeg/skills/alsa.go index 469dc059..a772e664 100644 --- a/ffmpeg/skills/alsa.go +++ b/ffmpeg/skills/alsa.go @@ -3,7 +3,7 @@ package skills import ( "bufio" "bytes" - "io/ioutil" + "os" "regexp" ) @@ -16,14 +16,14 @@ type alsaCard struct { func DevicesALSA() ([]HWDevice, error) { devices := []HWDevice{} - content, err := ioutil.ReadFile("/proc/asound/cards") + content, err := os.ReadFile("/proc/asound/cards") if err != nil { return devices, err } cards := parseALSACards(content) - content, err = ioutil.ReadFile("/proc/asound/devices") + content, err = os.ReadFile("/proc/asound/devices") if err != nil { return devices, err } diff --git a/http/graph/resolver/resolver.go b/http/graph/resolver/resolver.go index b567713c..8705fe80 100644 --- a/http/graph/resolver/resolver.go +++ b/http/graph/resolver/resolver.go @@ -2,7 +2,7 @@ package resolver import ( "bytes" - "io/ioutil" + "io" "net/http" "time" @@ -79,7 +79,7 @@ func (r *queryResolver) playoutRequest(method, addr, path, contentType string, d defer response.Body.Close() // Read the whole response - data, err = ioutil.ReadAll(response.Body) + data, err = io.ReadAll(response.Body) if err != nil { return nil, err } diff --git a/http/handler/api/memfs.go b/http/handler/api/memfs.go index d500e2be..2c6bd101 100644 --- a/http/handler/api/memfs.go +++ b/http/handler/api/memfs.go @@ -1,7 +1,7 @@ package api import ( - "io/ioutil" + "io" "net/http" "net/url" "sort" @@ -96,7 +96,7 @@ func (h *MemFSHandler) PatchFile(c echo.Context) error { req := c.Request() - body, err := ioutil.ReadAll(req.Body) + body, err := io.ReadAll(req.Body) if err != nil { return api.Err(http.StatusBadRequest, "Failed reading request body", "%s", err) } diff --git a/http/handler/api/playout.go b/http/handler/api/playout.go index b496a305..9387172f 100644 --- a/http/handler/api/playout.go +++ b/http/handler/api/playout.go @@ -3,7 +3,7 @@ package api import ( "bytes" "encoding/json" - "io/ioutil" + "io" "net/http" "strings" "time" @@ -59,7 +59,7 @@ func (h *PlayoutHandler) Status(c echo.Context) error { defer response.Body.Close() // Read the whole response - data, err := ioutil.ReadAll(response.Body) + data, err := io.ReadAll(response.Body) if err != nil { return api.Err(http.StatusInternalServerError, "", "%s", err) } @@ -122,7 +122,7 @@ func (h *PlayoutHandler) Keyframe(c echo.Context) error { defer response.Body.Close() // Read the whole response - data, err := ioutil.ReadAll(response.Body) + data, err := io.ReadAll(response.Body) if err != nil { return api.Err(http.StatusInternalServerError, "", "%s", err) } @@ -162,7 +162,7 @@ func (h *PlayoutHandler) EncodeErrorframe(c echo.Context) error { defer response.Body.Close() // Read the whole response - data, err := ioutil.ReadAll(response.Body) + data, err := io.ReadAll(response.Body) if err != nil { 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) } - data, err := ioutil.ReadAll(c.Request().Body) + data, err := io.ReadAll(c.Request().Body) if err != nil { 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() // Read the whole response - data, err = ioutil.ReadAll(response.Body) + data, err = io.ReadAll(response.Body) if err != nil { return api.Err(http.StatusInternalServerError, "", "%s", err) } @@ -249,7 +249,7 @@ func (h *PlayoutHandler) ReopenInput(c echo.Context) error { defer response.Body.Close() // Read the whole response - data, err := ioutil.ReadAll(response.Body) + data, err := io.ReadAll(response.Body) if err != nil { 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) } - data, err := ioutil.ReadAll(c.Request().Body) + data, err := io.ReadAll(c.Request().Body) if err != nil { 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() // Read the whole response - data, err = ioutil.ReadAll(response.Body) + data, err = io.ReadAll(response.Body) if err != nil { return api.Err(http.StatusInternalServerError, "", "%s", err) } diff --git a/http/handler/util/util.go b/http/handler/util/util.go index d62b0983..fbbcd2fd 100644 --- a/http/handler/util/util.go +++ b/http/handler/util/util.go @@ -2,7 +2,7 @@ package util import ( "fmt" - "io/ioutil" + "io" "net/url" "strings" @@ -24,7 +24,7 @@ func ShouldBindJSONValidation(c echo.Context, obj interface{}, validate bool) er return fmt.Errorf("request doesn't contain JSON content") } - body, err := ioutil.ReadAll(req.Body) + body, err := io.ReadAll(req.Body) if err != nil { return err } diff --git a/http/jwt/jwks/jwks.go b/http/jwt/jwks/jwks.go index 1faa2b49..dd9f3d51 100644 --- a/http/jwt/jwks/jwks.go +++ b/http/jwt/jwks/jwks.go @@ -6,7 +6,7 @@ import ( "crypto" "encoding/json" "errors" - "io/ioutil" + "io" "net/http" "sync" "time" @@ -324,7 +324,7 @@ func (j *jwksImpl) refresh() (err error) { // Read the raw JWKs from the body of the response. var jwksBytes []byte - if jwksBytes, err = ioutil.ReadAll(resp.Body); err != nil { + if jwksBytes, err = io.ReadAll(resp.Body); err != nil { return err } diff --git a/http/middleware/gzip/gzip.go b/http/middleware/gzip/gzip.go index 945a008f..22c7a8c0 100644 --- a/http/middleware/gzip/gzip.go +++ b/http/middleware/gzip/gzip.go @@ -4,7 +4,6 @@ import ( "bufio" "compress/gzip" "io" - "io/ioutil" "net" "net/http" "strings" @@ -108,7 +107,7 @@ func NewWithConfig(config Config) echo.MiddlewareFunc { // nothing is written to body or error is returned. // See issue #424, #407. res.Writer = rw - w.Reset(ioutil.Discard) + w.Reset(io.Discard) } w.Close() pool.Put(w) @@ -183,7 +182,7 @@ func (w *gzipResponseWriter) Push(target string, opts *http.PushOptions) error { func gzipPool(config Config) sync.Pool { return sync.Pool{ New: func() interface{} { - w, err := gzip.NewWriterLevel(ioutil.Discard, config.Level) + w, err := gzip.NewWriterLevel(io.Discard, config.Level) if err != nil { return err } diff --git a/http/mock/mock.go b/http/mock/mock.go index 0fc16e19..8bdc0c55 100644 --- a/http/mock/mock.go +++ b/http/mock/mock.go @@ -5,9 +5,9 @@ import ( "encoding/json" "fmt" "io" - "io/ioutil" "net/http" "net/http/httptest" + "os" "path/filepath" "strings" "testing" @@ -57,7 +57,7 @@ func DummyEcho() *echo.Echo { router.HideBanner = true router.HidePort = true router.HTTPErrorHandler = errorhandler.HTTPErrorHandler - router.Logger.SetOutput(ioutil.Discard) + router.Logger.SetOutput(io.Discard) router.Validator = validator.New() return router @@ -89,7 +89,7 @@ func CheckResponse(t *testing.T, res *http.Response) *Response { Code: res.StatusCode, } - body, err := ioutil.ReadAll(res.Body) + body, err := io.ReadAll(res.Body) require.Equal(t, nil, err) 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 { - data, err := ioutil.ReadFile(path) + data, err := os.ReadFile(path) require.Equal(t, nil, err) return bytes.NewReader(data) diff --git a/restream/store/json.go b/restream/store/json.go index e4b5e04c..41e7956a 100644 --- a/restream/store/json.go +++ b/restream/store/json.go @@ -3,7 +3,6 @@ package store import ( gojson "encoding/json" "fmt" - "io/ioutil" "os" "sync" @@ -78,7 +77,7 @@ func (s *jsonStore) store(data StoreData) error { return err } - tmpfile, err := ioutil.TempFile(s.dir, s.filename) + tmpfile, err := os.CreateTemp(s.dir, s.filename) if err != nil { return err } @@ -122,7 +121,7 @@ func (s *jsonStore) load(version uint64) (StoreData, error) { return r, err } - jsondata, err := ioutil.ReadFile(filename) + jsondata, err := os.ReadFile(filename) if err != nil { return r, err } diff --git a/service/api/api.go b/service/api/api.go index 429d24f7..30060289 100644 --- a/service/api/api.go +++ b/service/api/api.go @@ -6,7 +6,6 @@ import ( "errors" "fmt" "io" - "io/ioutil" "net/http" "strings" "time" @@ -149,14 +148,14 @@ func (a *api) refreshAccessToken(refreshToken string) error { return errStatusAuthorization } - data, _ := ioutil.ReadAll(res.Body) + data, _ := io.ReadAll(res.Body) return statusError{ code: res.StatusCode, response: data, } } - data, err := ioutil.ReadAll(res.Body) + data, err := io.ReadAll(res.Body) if err != nil { return statusError{ code: res.StatusCode, @@ -199,14 +198,14 @@ func (a *api) call(method, path string, body io.Reader) ([]byte, error) { return nil, errStatusAuthorization } - data, _ := ioutil.ReadAll(res.Body) + data, _ := io.ReadAll(res.Body) return nil, statusError{ code: res.StatusCode, response: data, } } - data, err := ioutil.ReadAll(res.Body) + data, err := io.ReadAll(res.Body) if err != nil { return nil, fmt.Errorf("error reading response: %w", statusError{ code: res.StatusCode, diff --git a/session/collector.go b/session/collector.go index e5f1aa7e..a7084bc2 100644 --- a/session/collector.go +++ b/session/collector.go @@ -3,7 +3,6 @@ package session import ( "context" "encoding/json" - "io/ioutil" "os" "path/filepath" "sort" @@ -461,7 +460,7 @@ func (c *collector) loadHistory(path string, data *history) { c.lock.persist.Lock() defer c.lock.persist.Unlock() - jsondata, err := ioutil.ReadFile(path) + jsondata, err := os.ReadFile(path) if err != nil { return } @@ -489,7 +488,7 @@ func (c *collector) saveHistory(path string, data *history) { dir := filepath.Dir(path) filename := filepath.Base(path) - tmpfile, err := ioutil.TempFile(dir, filename) + tmpfile, err := os.CreateTemp(dir, filename) if err != nil { return } diff --git a/update/update.go b/update/update.go index 227ab046..923f663e 100644 --- a/update/update.go +++ b/update/update.go @@ -5,7 +5,7 @@ import ( "context" "encoding/json" "fmt" - "io/ioutil" + "io" "net/http" "regexp" "sync" @@ -179,7 +179,7 @@ func (s *checker) check() error { 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 { return fmt.Errorf("error reading response: %w", err) }