refactor: replace the ioutil by the os and io

Signed-off-by: saltbo <saltbo@foxmail.com>
This commit is contained in:
saltbo 2022-10-20 15:13:28 +08:00
parent 0a689af469
commit cc90e819bc
No known key found for this signature in database
GPG Key ID: ED13862C4BCD86D8
7 changed files with 18 additions and 27 deletions

View File

@ -17,14 +17,13 @@ limitations under the License.
package filesystem
import (
"io/ioutil"
"os"
"path/filepath"
"strings"
"time"
)
// DefaultFs implements Filesystem using same-named functions from "os" and "io/ioutil"
// DefaultFs implements Filesystem using same-named functions from "os" and "io"
type DefaultFs struct {
root string
}
@ -33,10 +32,7 @@ var _ Filesystem = &DefaultFs{}
// NewTempFs returns a fake Filesystem in temporary directory, useful for unit tests
func NewTempFs() Filesystem {
path, _ := ioutil.TempDir(
"",
"tmpfs",
)
path, _ := os.MkdirTemp("", "tmpfs")
return &DefaultFs{
root: path,
}
@ -94,28 +90,28 @@ func (fs *DefaultFs) Remove(name string) error {
return os.Remove(fs.prefix(name))
}
// ReadFile via ioutil.ReadFile
// ReadFile via os.ReadFile
func (fs *DefaultFs) ReadFile(filename string) ([]byte, error) {
return ioutil.ReadFile(fs.prefix(filename))
return os.ReadFile(fs.prefix(filename))
}
// TempDir via ioutil.TempDir
// TempDir via os.MkdirTemp
func (fs *DefaultFs) TempDir(dir, prefix string) (string, error) {
return ioutil.TempDir(fs.prefix(dir), prefix)
return os.MkdirTemp(fs.prefix(dir), prefix)
}
// TempFile via ioutil.TempFile
// TempFile via os.CreateTemp
func (fs *DefaultFs) TempFile(dir, prefix string) (File, error) {
file, err := ioutil.TempFile(fs.prefix(dir), prefix)
file, err := os.CreateTemp(fs.prefix(dir), prefix)
if err != nil {
return nil, err
}
return &defaultFile{file}, nil
}
// ReadDir via ioutil.ReadDir
func (fs *DefaultFs) ReadDir(dirname string) ([]os.FileInfo, error) {
return ioutil.ReadDir(fs.prefix(dirname))
// ReadDir via os.ReadDir
func (fs *DefaultFs) ReadDir(dirname string) ([]os.DirEntry, error) {
return os.ReadDir(fs.prefix(dirname))
}
// Walk via filepath.Walk

View File

@ -37,7 +37,7 @@ type Filesystem interface {
ReadFile(filename string) ([]byte, error)
TempDir(dir, prefix string) (string, error)
TempFile(dir, prefix string) (File, error)
ReadDir(dirname string) ([]os.FileInfo, error)
ReadDir(dirname string) ([]os.DirEntry, error)
Walk(root string, walkFn filepath.WalkFunc) error
}

View File

@ -21,7 +21,6 @@ package oom
import (
"fmt"
"io/ioutil"
"os"
"path"
"path/filepath"
@ -66,7 +65,7 @@ func applyOOMScoreAdj(pid int, oomScoreAdj int) error {
klog.V(4).Infof("attempting to set %q to %q", oomScoreAdjPath, value)
var err error
for i := 0; i < maxTries; i++ {
err = ioutil.WriteFile(oomScoreAdjPath, []byte(value), 0700)
err = os.WriteFile(oomScoreAdjPath, []byte(value), 0700)
if err != nil {
if os.IsNotExist(err) {
klog.V(2).Infof("%q does not exist", oomScoreAdjPath)

View File

@ -23,7 +23,6 @@ import (
"bytes"
"fmt"
"io"
"io/ioutil"
"os"
"path"
"path/filepath"
@ -61,7 +60,7 @@ func containerNameFromProcCgroup(content string) (string, error) {
// return docker/nginx. Assumes that the process is part of exactly one cgroup hierarchy.
func (pfs *ProcFS) GetFullContainerName(pid int) (string, error) {
filePath := path.Join("/proc", strconv.Itoa(pid), "cgroup")
content, err := ioutil.ReadFile(filePath)
content, err := os.ReadFile(filePath)
if err != nil {
if os.IsNotExist(err) {
return "", os.ErrNotExist
@ -138,7 +137,7 @@ func getPids(re *regexp.Regexp) []int {
continue
}
cmdline, err := ioutil.ReadFile(filepath.Join("/proc", entry.Name(), "cmdline"))
cmdline, err := os.ReadFile(filepath.Join("/proc", entry.Name(), "cmdline"))
if err != nil {
klog.V(4).Infof("Error reading file %s: %+v", filepath.Join("/proc", entry.Name(), "cmdline"), err)
continue

View File

@ -20,7 +20,6 @@ limitations under the License.
package procfs
import (
"io/ioutil"
"os"
"os/signal"
"path/filepath"
@ -56,7 +55,7 @@ func TestContainerNameFromProcCgroup(t *testing.T) {
procCgroupEmpty := ""
verifyContainerName(procCgroupEmpty, "", true, t)
content, err := ioutil.ReadFile("example_proc_cgroup")
content, err := os.ReadFile("example_proc_cgroup")
if err != nil {
t.Errorf("Could not read example /proc cgroup file")
}

View File

@ -19,7 +19,6 @@ package tail
import (
"bytes"
"io"
"io/ioutil"
"os"
)
@ -57,7 +56,7 @@ func ReadAtMost(path string, max int64) ([]byte, bool, error) {
if err != nil {
return nil, false, err
}
data, err := ioutil.ReadAll(f)
data, err := io.ReadAll(f)
return data, offset > 0, err
}

View File

@ -18,14 +18,13 @@ package tail
import (
"bytes"
"io/ioutil"
"os"
"strings"
"testing"
)
func TestReadAtMost(t *testing.T) {
file, err := ioutil.TempFile("", "TestFileReadAtMost")
file, err := os.CreateTemp("", "TestFileReadAtMost")
if err != nil {
t.Fatalf("unable to create temp file")
}