replace all the deprecated ioutil with io and os

This commit is contained in:
ahrtr 2021-11-15 13:26:49 +08:00 committed by ahrtr
parent e8ac6d8a8e
commit 584d994133
17 changed files with 41 additions and 40 deletions

View File

@ -1 +1 @@
2.36 2.37

View File

@ -17,7 +17,7 @@ limitations under the License.
package auditproxy package auditproxy
import ( import (
"io/ioutil" "io"
"log" "log"
"net/http" "net/http"
"os" "os"
@ -57,7 +57,7 @@ func main(cmd *cobra.Command, args []string) {
} }
func handler(w http.ResponseWriter, req *http.Request) { func handler(w http.ResponseWriter, req *http.Request) {
body, err := ioutil.ReadAll(req.Body) body, err := io.ReadAll(req.Body)
if err != nil { if err != nil {
log.Printf("could not read request body: %v", err) log.Printf("could not read request body: %v", err)
w.WriteHeader(http.StatusInternalServerError) w.WriteHeader(http.StatusInternalServerError)

View File

@ -18,7 +18,7 @@ package converter
import ( import (
"fmt" "fmt"
"io/ioutil" "io"
"net/http" "net/http"
"strings" "strings"
@ -117,7 +117,7 @@ func doConversionV1(convertRequest *v1.ConversionRequest, convert convertFunc) *
func serve(w http.ResponseWriter, r *http.Request, convert convertFunc) { func serve(w http.ResponseWriter, r *http.Request, convert convertFunc) {
var body []byte var body []byte
if r.Body != nil { if r.Body != nil {
if data, err := ioutil.ReadAll(r.Body); err == nil { if data, err := io.ReadAll(r.Body); err == nil {
body = data body = data
} }
} }

View File

@ -18,7 +18,7 @@ package dns
import ( import (
"fmt" "fmt"
"io/ioutil" "os"
"strings" "strings"
"github.com/spf13/cobra" "github.com/spf13/cobra"
@ -66,7 +66,7 @@ func printHostsFile(cmd *cobra.Command, args []string) {
} }
func readFile(fileName string) string { func readFile(fileName string) string {
fileData, err := ioutil.ReadFile(fileName) fileData, err := os.ReadFile(fileName)
if err != nil { if err != nil {
panic(err) panic(err)
} }

View File

@ -19,7 +19,7 @@ package guestbook
import ( import (
"encoding/json" "encoding/json"
"fmt" "fmt"
"io/ioutil" "io"
"log" "log"
"net" "net"
"net/http" "net/http"
@ -278,7 +278,7 @@ func dialHTTP(request, hostPort string) (string, error) {
defer transport.CloseIdleConnections() defer transport.CloseIdleConnections()
if err == nil { if err == nil {
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 string(body), nil return string(body), nil
} }

View File

@ -18,7 +18,6 @@ package mounttest
import ( import (
"fmt" "fmt"
"io/ioutil"
"os" "os"
"time" "time"
@ -146,7 +145,7 @@ func readFileContent(path string) error {
return nil return nil
} }
contentBytes, err := ioutil.ReadFile(path) contentBytes, err := os.ReadFile(path)
if err != nil { if err != nil {
fmt.Printf("error reading file content for %q: %v\n", path, err) fmt.Printf("error reading file content for %q: %v\n", path, err)
return err return err
@ -164,7 +163,7 @@ func readWriteNewFile(path string, perm os.FileMode) error {
return nil return nil
} }
err := ioutil.WriteFile(path, []byte(initialContent), perm) err := os.WriteFile(path, []byte(initialContent), perm)
if err != nil { if err != nil {
fmt.Printf("error writing new file %q: %v\n", path, err) fmt.Printf("error writing new file %q: %v\n", path, err)
return err return err
@ -188,7 +187,7 @@ func testFileContent(filePath string, retryDuration int, breakOnExpectedContent
retryTime := time.Second * time.Duration(retryDuration) retryTime := time.Second * time.Duration(retryDuration)
for start := time.Now(); time.Since(start) < retryTime; time.Sleep(2 * time.Second) { for start := time.Now(); time.Since(start) < retryTime; time.Sleep(2 * time.Second) {
contentBytes, err = ioutil.ReadFile(filePath) contentBytes, err = os.ReadFile(filePath)
if err != nil { if err != nil {
fmt.Printf("Error reading file %s: %v, retrying\n", filePath, err) fmt.Printf("Error reading file %s: %v, retrying\n", filePath, err)
continue continue

View File

@ -20,7 +20,7 @@ import (
"bytes" "bytes"
"encoding/json" "encoding/json"
"fmt" "fmt"
"io/ioutil" "io"
"log" "log"
"net/http" "net/http"
"os" "os"
@ -164,7 +164,7 @@ func handleRunRequest(w http.ResponseWriter, r *http.Request) {
return return
} }
body, err := ioutil.ReadAll(r.Body) body, err := io.ReadAll(r.Body)
if err != nil { if err != nil {
http.Error(w, fmt.Sprintf("error reading body: %v", err), 400) http.Error(w, fmt.Sprintf("error reading body: %v", err), 400)
return return

View File

@ -21,7 +21,6 @@ import (
"encoding/json" "encoding/json"
"fmt" "fmt"
"io" "io"
"io/ioutil"
"log" "log"
"net" "net"
"net/http" "net/http"
@ -426,7 +425,7 @@ func dialHTTP(request string, addr net.Addr) (string, error) {
defer transport.CloseIdleConnections() defer transport.CloseIdleConnections()
if err == nil { if err == nil {
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 string(body), nil return string(body), nil
} }
@ -524,7 +523,7 @@ func uploadHandler(w http.ResponseWriter, r *http.Request) {
} }
defer file.Close() defer file.Close()
f, err := ioutil.TempFile("/uploads", "upload") f, err := os.CreateTemp("/uploads", "upload")
if err != nil { if err != nil {
result["error"] = "Unable to open file for write" result["error"] = "Unable to open file for write"
bytes, err := json.Marshal(result) bytes, err := json.Marshal(result)

View File

@ -35,7 +35,7 @@ import (
"context" "context"
"encoding/json" "encoding/json"
"fmt" "fmt"
"io/ioutil" "io"
"log" "log"
"net" "net"
"net/http" "net/http"
@ -349,7 +349,7 @@ func contactSingle(e string, state *State) {
} }
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 {
state.Logf("Warning: unable to read response from '%v': '%v'", e, err) state.Logf("Warning: unable to read response from '%v': '%v'", e, err)
return return

View File

@ -18,7 +18,7 @@ package nosnatproxy
import ( import (
"fmt" "fmt"
"io/ioutil" "io"
"net/http" "net/http"
"os" "os"
@ -80,7 +80,7 @@ func checknosnat(w http.ResponseWriter, req *http.Request) {
w.WriteHeader(500) w.WriteHeader(500)
fmt.Fprintf(w, "error querying %q, err: %v", url, err) fmt.Fprintf(w, "error querying %q, err: %v", url, err)
} else { } else {
body, err := ioutil.ReadAll(resp.Body) body, err := io.ReadAll(resp.Body)
if err != nil { if err != nil {
w.WriteHeader(500) w.WriteHeader(500)
fmt.Fprintf(w, "error reading body of response from %q, err: %v", url, err) fmt.Fprintf(w, "error reading body of response from %q, err: %v", url, err)

View File

@ -18,7 +18,7 @@ package nosnat
import ( import (
"fmt" "fmt"
"io/ioutil" "io"
"net/http" "net/http"
"os" "os"
"strings" "strings"
@ -141,7 +141,7 @@ func check(ip string, pip string, nip string) error {
return err return err
} }
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 err return err
} }

View File

@ -21,7 +21,6 @@ package openidmetadata
import ( import (
"context" "context"
"fmt" "fmt"
"io/ioutil"
"log" "log"
"net" "net"
"net/http" "net/http"
@ -163,7 +162,7 @@ func (k *claims) String() string {
} }
func gettoken() (string, error) { func gettoken() (string, error) {
b, err := ioutil.ReadFile(tokenPath) b, err := os.ReadFile(tokenPath)
return string(b), err return string(b), err
} }

View File

@ -19,7 +19,7 @@ package webhook
import ( import (
"encoding/json" "encoding/json"
"fmt" "fmt"
"io/ioutil" "io"
"net/http" "net/http"
"github.com/spf13/cobra" "github.com/spf13/cobra"
@ -93,7 +93,7 @@ func delegateV1beta1AdmitToV1(f admitv1Func) admitv1beta1Func {
func serve(w http.ResponseWriter, r *http.Request, admit admitHandler) { func serve(w http.ResponseWriter, r *http.Request, admit admitHandler) {
var body []byte var body []byte
if r.Body != nil { if r.Body != nil {
if data, err := ioutil.ReadAll(r.Body); err == nil { if data, err := io.ReadAll(r.Body); err == nil {
body = data body = data
} }
} }

View File

@ -1 +1 @@
1.4 1.5

View File

@ -21,7 +21,6 @@ import (
"bytes" "bytes"
"flag" "flag"
"fmt" "fmt"
"io/ioutil"
"os" "os"
"os/exec" "os/exec"
"path" "path"
@ -118,7 +117,7 @@ func loadNewProfiles() (success bool, newProfiles []string) {
success = true success = true
for _, dir := range dirs { for _, dir := range dirs {
infos, err := ioutil.ReadDir(dir) infos, err := os.ReadDir(dir)
if err != nil { if err != nil {
klog.Warningf("Error reading %s: %v", dir, err) klog.Warningf("Error reading %s: %v", dir, err)
success = false success = false
@ -207,12 +206,17 @@ func loadProfiles(path string) error {
// If the given fileinfo is a symlink, return the FileInfo of the target. Otherwise, return the // If the given fileinfo is a symlink, return the FileInfo of the target. Otherwise, return the
// given fileinfo. // given fileinfo.
func resolveSymlink(basePath string, info os.FileInfo) (os.FileInfo, error) { func resolveSymlink(basePath string, entry os.DirEntry) (os.FileInfo, error) {
if info.Mode()&os.ModeSymlink == 0 { if info, err := entry.Info(); err != nil {
// Not a symlink. return nil, fmt.Errorf("error getting the fileInfo: %v", err)
return info, nil } else {
if info.Mode()&os.ModeSymlink == 0 {
// Not a symlink.
return info, nil
}
} }
fpath := filepath.Join(basePath, info.Name())
fpath := filepath.Join(basePath, entry.Name())
resolvedName, err := filepath.EvalSymlinks(fpath) resolvedName, err := filepath.EvalSymlinks(fpath)
if err != nil { if err != nil {
return nil, fmt.Errorf("error resolving symlink %s: %v", fpath, err) return nil, fmt.Errorf("error resolving symlink %s: %v", fpath, err)

View File

@ -1 +1 @@
1.5 1.6

View File

@ -20,7 +20,7 @@ package main
import ( import (
"flag" "flag"
"fmt" "fmt"
"io/ioutil" "io"
"log" "log"
"net" "net"
"os" "os"
@ -77,7 +77,7 @@ func shellOut(sendStdin, script string) {
stdin.Write([]byte(sendStdin)) stdin.Write([]byte(sendStdin))
stdin.Close() stdin.Close()
out, err := ioutil.ReadAll(stdout) out, err := io.ReadAll(stdout)
if err != nil { if err != nil {
log.Fatalf("Failed to execute %v: %v, err: %v", script, string(out), err) log.Fatalf("Failed to execute %v: %v, err: %v", script, string(out), err)
} }
@ -104,7 +104,7 @@ func main() {
// If domain is not provided, try to get it from resolv.conf // If domain is not provided, try to get it from resolv.conf
if *domain == "" { if *domain == "" {
resolvConfBytes, err := ioutil.ReadFile("/etc/resolv.conf") resolvConfBytes, err := os.ReadFile("/etc/resolv.conf")
resolvConf := string(resolvConfBytes) resolvConf := string(resolvConfBytes)
if err != nil { if err != nil {
log.Fatal("Unable to read /etc/resolv.conf") log.Fatal("Unable to read /etc/resolv.conf")