Skip to content

Commit

Permalink
refactor: replace the ioutil by the os and io
Browse files Browse the repository at this point in the history
Signed-off-by: saltbo <saltbo@foxmail.com>
  • Loading branch information
saltbo committed Oct 20, 2022
1 parent 0a689af commit cc90e81
Show file tree
Hide file tree
Showing 7 changed files with 18 additions and 27 deletions.
26 changes: 11 additions & 15 deletions pkg/util/filesystem/defaultfs.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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,
}
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion pkg/util/filesystem/filesystem.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
3 changes: 1 addition & 2 deletions pkg/util/oom/oom_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ package oom

import (
"fmt"
"io/ioutil"
"os"
"path"
"path/filepath"
Expand Down Expand Up @@ -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)
Expand Down
5 changes: 2 additions & 3 deletions pkg/util/procfs/procfs_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import (
"bytes"
"fmt"
"io"
"io/ioutil"
"os"
"path"
"path/filepath"
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
3 changes: 1 addition & 2 deletions pkg/util/procfs/procfs_linux_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ limitations under the License.
package procfs

import (
"io/ioutil"
"os"
"os/signal"
"path/filepath"
Expand Down Expand Up @@ -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")
}
Expand Down
3 changes: 1 addition & 2 deletions pkg/util/tail/tail.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ package tail
import (
"bytes"
"io"
"io/ioutil"
"os"
)

Expand Down Expand Up @@ -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
}

Expand Down
3 changes: 1 addition & 2 deletions pkg/util/tail/tail_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
Expand Down

0 comments on commit cc90e81

Please sign in to comment.