replace the ioutil by the os and io

Signed-off-by: chaunceyjiang <chaunceyjiang@gmail.com>
This commit is contained in:
chaunceyjiang 2022-11-01 14:15:26 +08:00
parent f68da6d64e
commit 1a6b35649c
8 changed files with 13 additions and 19 deletions

View File

@ -4,7 +4,6 @@ import (
"fmt" "fmt"
"io" "io"
"io/fs" "io/fs"
"io/ioutil"
"os" "os"
"path/filepath" "path/filepath"
"strings" "strings"
@ -116,7 +115,7 @@ func main() {
if info.IsDir() { if info.IsDir() {
return nil return nil
} }
data, err := ioutil.ReadFile(path) data, err := os.ReadFile(path)
if err != nil { if err != nil {
return err return err
} }

View File

@ -30,7 +30,6 @@ import (
"go/format" "go/format"
"go/parser" "go/parser"
"go/token" "go/token"
"io/ioutil"
"log" "log"
"os" "os"
"path/filepath" "path/filepath"
@ -133,7 +132,7 @@ func (a *analyzer) collect(dir string) {
panic(fmt.Sprintf("Error stat'ing file: %s\n%s\n", pathToFile, err.Error())) panic(fmt.Sprintf("Error stat'ing file: %s\n%s\n", pathToFile, err.Error()))
} }
err = ioutil.WriteFile(pathToFile, buffer.Bytes(), fileInfo.Mode()) err = os.WriteFile(pathToFile, buffer.Bytes(), fileInfo.Mode())
if err != nil { if err != nil {
panic(fmt.Sprintf("Error writing file: %s\n%s\n", pathToFile, err.Error())) panic(fmt.Sprintf("Error writing file: %s\n%s\n", pathToFile, err.Error()))
} }
@ -244,7 +243,7 @@ func main() {
sort.Strings(c.dirs) sort.Strings(c.dirs)
if len(*importAliases) > 0 { if len(*importAliases) > 0 {
bytes, err := ioutil.ReadFile(*importAliases) bytes, err := os.ReadFile(*importAliases)
if err != nil { if err != nil {
log.Fatalf("Error reading import aliases: %v", err) log.Fatalf("Error reading import aliases: %v", err)
} }

View File

@ -4,7 +4,7 @@ import (
"context" "context"
"encoding/json" "encoding/json"
"fmt" "fmt"
"io/ioutil" "os"
"path" "path"
"path/filepath" "path/filepath"
"regexp" "regexp"
@ -176,7 +176,7 @@ func createExtralResources(clientSet *kubernetes.Clientset, dir string) error {
} }
func crdPatchesResources(filename, caBundle string) ([]byte, error) { func crdPatchesResources(filename, caBundle string) ([]byte, error) {
data, err := ioutil.ReadFile(filename) data, err := os.ReadFile(filename)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -193,7 +193,7 @@ func crdPatchesResources(filename, caBundle string) ([]byte, error) {
// createCRDs create crd resource // createCRDs create crd resource
func createCRDs(crdClient *clientset.Clientset, filename string) error { func createCRDs(crdClient *clientset.Clientset, filename string) error {
obj := apiextensionsv1.CustomResourceDefinition{} obj := apiextensionsv1.CustomResourceDefinition{}
data, err := ioutil.ReadFile(filename) data, err := os.ReadFile(filename)
if err != nil { if err != nil {
return err return err
} }

View File

@ -3,7 +3,7 @@ package utils
import ( import (
"bytes" "bytes"
"fmt" "fmt"
"io/ioutil" "io"
"net" "net"
"net/http" "net/http"
"os" "os"
@ -60,7 +60,7 @@ func InternetIP() (net.IP, error) {
defer resp.Body.Close() defer resp.Body.Close()
content, err := ioutil.ReadAll(resp.Body) content, err := io.ReadAll(resp.Body)
if err != nil { if err != nil {
return nil, err return nil, err
} }

View File

@ -9,7 +9,6 @@ import (
"crypto/x509/pkix" "crypto/x509/pkix"
"encoding/pem" "encoding/pem"
"fmt" "fmt"
"io/ioutil"
"os" "os"
"path/filepath" "path/filepath"
"strings" "strings"
@ -262,7 +261,7 @@ func (o *CommandRegisterOption) Complete(args []string) error {
} }
if len(o.ClusterName) == 0 { if len(o.ClusterName) == 0 {
configBytes, err := ioutil.ReadFile(o.KubeConfig) configBytes, err := os.ReadFile(o.KubeConfig)
if err != nil { if err != nil {
return fmt.Errorf("failed to read kubeconfig file %s, err: %w", o.KubeConfig, err) return fmt.Errorf("failed to read kubeconfig file %s, err: %w", o.KubeConfig, err)
} }

View File

@ -4,7 +4,6 @@ import (
"bytes" "bytes"
"context" "context"
"io" "io"
"io/ioutil"
"net/http" "net/http"
corev1 "k8s.io/api/core/v1" corev1 "k8s.io/api/core/v1"
@ -121,7 +120,7 @@ func modifyRequest(req *http.Request, cluster string) error {
_ = req.Body.Close() _ = req.Body.Close()
defer func() { defer func() {
req.Body = ioutil.NopCloser(body) req.Body = io.NopCloser(body)
req.ContentLength = int64(body.Len()) req.ContentLength = int64(body.Len())
}() }()

View File

@ -7,7 +7,6 @@ import (
"errors" "errors"
"fmt" "fmt"
"io" "io"
"io/ioutil"
"net/http" "net/http"
"net/http/httptest" "net/http/httptest"
"net/url" "net/url"
@ -137,7 +136,7 @@ func TestModifyRequest(t *testing.T) {
var get runtime.Object var get runtime.Object
if req.ContentLength != 0 { if req.ContentLength != 0 {
data, err := ioutil.ReadAll(req.Body) data, err := io.ReadAll(req.Body)
if err != nil { if err != nil {
t.Error(err) t.Error(err)
return return
@ -368,7 +367,7 @@ func Test_clusterProxy_connect(t *testing.T) {
request: (&http.Request{ request: (&http.Request{
Method: "PUT", Method: "PUT",
URL: &url.URL{Scheme: "https", Host: "localhost", Path: "/test"}, URL: &url.URL{Scheme: "https", Host: "localhost", Path: "/test"},
Body: ioutil.NopCloser(&alwaysErrorReader{}), Body: io.NopCloser(&alwaysErrorReader{}),
ContentLength: 10, ContentLength: 10,
Header: make(http.Header), Header: make(http.Header),
}).WithContext(reqCtx), }).WithContext(reqCtx),

View File

@ -5,7 +5,6 @@ import (
"errors" "errors"
"fmt" "fmt"
"io" "io"
"io/ioutil"
"net/http" "net/http"
"k8s.io/apimachinery/pkg/runtime" "k8s.io/apimachinery/pkg/runtime"
@ -34,7 +33,7 @@ func (wh *Webhook) ServeHTTP(w http.ResponseWriter, r *http.Request) {
} }
defer r.Body.Close() defer r.Body.Close()
if body, err = ioutil.ReadAll(r.Body); err != nil { if body, err = io.ReadAll(r.Body); err != nil {
klog.Errorf("unable to read the body from the incoming request: %w", err) klog.Errorf("unable to read the body from the incoming request: %w", err)
res = Errored(http.StatusBadRequest, err) res = Errored(http.StatusBadRequest, err)
wh.writeResponse(w, res) wh.writeResponse(w, res)