Remove ioutil usage

This has been deprecated in Go for some time, in favour of the
io package.

Signed-off-by: Alex Ellis (OpenFaaS Ltd) <alex@openfaas.com>
This commit is contained in:
Alex Ellis (OpenFaaS Ltd) 2023-08-22 18:29:28 +01:00
parent 55776acc0d
commit 2a88b5d2f7
8 changed files with 21 additions and 20 deletions

View File

@ -6,7 +6,7 @@ package handlers
import ( import (
"encoding/json" "encoding/json"
"fmt" "fmt"
"io/ioutil" "io"
"log" "log"
"math" "math"
"net/http" "net/http"
@ -27,7 +27,7 @@ func MakeAlertHandler(service scaling.ServiceQuery, defaultNamespace string) htt
defer r.Body.Close() defer r.Body.Close()
body, err := ioutil.ReadAll(r.Body) body, err := io.ReadAll(r.Body)
if err != nil { if err != nil {
w.WriteHeader(http.StatusBadRequest) w.WriteHeader(http.StatusBadRequest)
w.Write([]byte("Unable to read alert.")) w.Write([]byte("Unable to read alert."))

View File

@ -6,7 +6,7 @@ package handlers
import ( import (
"bytes" "bytes"
"fmt" "fmt"
"io/ioutil" "io"
"net/http" "net/http"
"net/url" "net/url"
"testing" "testing"
@ -33,7 +33,7 @@ func Test_buildUpstreamRequest_Body_Method_Query(t *testing.T) {
t.Fail() t.Fail()
} }
upstreamBytes, _ := ioutil.ReadAll(upstream.Body) upstreamBytes, _ := io.ReadAll(upstream.Body)
if string(upstreamBytes) != string(srcBytes) { if string(upstreamBytes) != string(srcBytes) {
t.Errorf("Body - want: %s, got: %s", string(upstreamBytes), string(srcBytes)) t.Errorf("Body - want: %s, got: %s", string(upstreamBytes), string(srcBytes))
@ -212,7 +212,7 @@ func Test_buildUpstreamRequest_WithPathNoQuery(t *testing.T) {
t.Fail() t.Fail()
} }
upstreamBytes, _ := ioutil.ReadAll(upstream.Body) upstreamBytes, _ := io.ReadAll(upstream.Body)
if string(upstreamBytes) != string(srcBytes) { if string(upstreamBytes) != string(srcBytes) {
t.Errorf("Body - want: %s, got: %s", string(upstreamBytes), string(srcBytes)) t.Errorf("Body - want: %s, got: %s", string(upstreamBytes), string(srcBytes))
@ -268,7 +268,7 @@ func Test_buildUpstreamRequest_WithNoPathNoQuery(t *testing.T) {
t.Fail() t.Fail()
} }
upstreamBytes, _ := ioutil.ReadAll(upstream.Body) upstreamBytes, _ := io.ReadAll(upstream.Body)
if string(upstreamBytes) != string(srcBytes) { if string(upstreamBytes) != string(srcBytes) {
t.Errorf("Body - want: %s, got: %s", string(upstreamBytes), string(srcBytes)) t.Errorf("Body - want: %s, got: %s", string(upstreamBytes), string(srcBytes))
@ -322,7 +322,7 @@ func Test_buildUpstreamRequest_WithPathAndQuery(t *testing.T) {
t.Fail() t.Fail()
} }
upstreamBytes, _ := ioutil.ReadAll(upstream.Body) upstreamBytes, _ := io.ReadAll(upstream.Body)
if string(upstreamBytes) != string(srcBytes) { if string(upstreamBytes) != string(srcBytes) {
t.Errorf("Body - want: %s, got: %s", string(upstreamBytes), string(srcBytes)) t.Errorf("Body - want: %s, got: %s", string(upstreamBytes), string(srcBytes))

View File

@ -3,7 +3,7 @@ package handlers
import ( import (
"context" "context"
"fmt" "fmt"
"io/ioutil" "io"
"net/http" "net/http"
"net/http/httptest" "net/http/httptest"
"net/url" "net/url"
@ -52,7 +52,7 @@ func Test_logsProxyDoesNotLeakGoroutinesWhenProviderClosesConnection(t *testing.
t.Fatalf("unexpected error sending log request: %s", err) t.Fatalf("unexpected error sending log request: %s", err)
} }
body, err := ioutil.ReadAll(resp.Body) body, err := io.ReadAll(resp.Body)
if err != nil { if err != nil {
t.Fatalf("unexpected error reading the response body: %s", err) t.Fatalf("unexpected error reading the response body: %s", err)
} }
@ -126,7 +126,7 @@ func Test_logsProxyDoesNotLeakGoroutinesWhenClientClosesConnection(t *testing.T)
go func() { go func() {
defer resp.Body.Close() defer resp.Body.Close()
defer close(errCh) defer close(errCh)
_, err := ioutil.ReadAll(resp.Body) _, err := io.ReadAll(resp.Body)
errCh <- err errCh <- err
}() }()
cancel() cancel()

View File

@ -5,7 +5,7 @@ package handlers
import ( import (
"fmt" "fmt"
"io/ioutil" "io"
"log" "log"
"net/http" "net/http"
"net/url" "net/url"
@ -27,7 +27,7 @@ func MakeQueuedProxy(metrics metrics.MetricOptions, queuer ftypes.RequestQueuer,
defer r.Body.Close() defer r.Body.Close()
var err error var err error
body, err = ioutil.ReadAll(r.Body) body, err = io.ReadAll(r.Body)
if err != nil { if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest) http.Error(w, err.Error(), http.StatusBadRequest)
return return

View File

@ -3,7 +3,7 @@ package metrics
import ( import (
"encoding/json" "encoding/json"
"fmt" "fmt"
"io/ioutil" "io"
"log" "log"
"net/http" "net/http"
"net/http/httptest" "net/http/httptest"
@ -28,7 +28,7 @@ func AddMetricsHandler(handler http.HandlerFunc, prometheusQuery PrometheusQuery
} }
defer upstreamCall.Body.Close() defer upstreamCall.Body.Close()
upstreamBody, _ := ioutil.ReadAll(upstreamCall.Body) upstreamBody, _ := io.ReadAll(upstreamCall.Body)
if recorder.Code != http.StatusOK { if recorder.Code != http.StatusOK {
log.Printf("List functions responded with code %d, body: %s", log.Printf("List functions responded with code %d, body: %s",

View File

@ -7,7 +7,7 @@ package metrics
import ( import (
"encoding/json" "encoding/json"
"fmt" "fmt"
"io/ioutil" "io"
"net" "net"
"net/http" "net/http"
"net/url" "net/url"
@ -159,7 +159,7 @@ func (e *Exporter) getFunctions(endpointURL url.URL, namespace string) ([]types.
return services, err return services, err
} }
bytesOut, readErr := ioutil.ReadAll(res.Body) bytesOut, readErr := io.ReadAll(res.Body)
if readErr != nil { if readErr != nil {
return services, readErr return services, readErr
} }
@ -193,7 +193,7 @@ func (e *Exporter) getNamespaces(endpointURL url.URL) ([]string, error) {
return namespaces, nil return namespaces, nil
} }
bytesOut, readErr := ioutil.ReadAll(res.Body) bytesOut, readErr := io.ReadAll(res.Body)
if readErr != nil { if readErr != nil {
return namespaces, readErr return namespaces, readErr
} }

View File

@ -3,7 +3,7 @@ package metrics
import ( import (
"encoding/json" "encoding/json"
"fmt" "fmt"
"io/ioutil" "io"
"net/http" "net/http"
) )
@ -44,7 +44,7 @@ func (q PrometheusQuery) Fetch(query string) (*VectorQueryResponse, error) {
defer res.Body.Close() defer res.Body.Close()
} }
bytesOut, readErr := ioutil.ReadAll(res.Body) bytesOut, readErr := io.ReadAll(res.Body)
if readErr != nil { if readErr != nil {
return nil, readErr return nil, readErr
} }

View File

@ -3,6 +3,7 @@ package scaling
import ( import (
"bytes" "bytes"
"encoding/json" "encoding/json"
"io"
"io/ioutil" "io/ioutil"
"net/http" "net/http"
@ -43,7 +44,7 @@ func MakeHorizontalScalingHandler(next http.HandlerFunc) http.HandlerFunc {
return return
} }
body, err := ioutil.ReadAll(r.Body) body, err := io.ReadAll(r.Body)
if err != nil { if err != nil {
http.Error(w, "Error reading request body", http.StatusBadRequest) http.Error(w, "Error reading request body", http.StatusBadRequest)
return return