refactor: move from io/ioutil to io and os packages (#1417)

The io/ioutil package has been deprecated as of Go 1.16 [1]. This commit
replaces the existing io/ioutil functions with their new definitions in
io and os packages.

[1]: https://golang.org/doc/go1.16#ioutil
Signed-off-by: Eng Zer Jun <engzerjun@gmail.com>

Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
This commit is contained in:
Eng Zer Jun 2022-10-30 01:15:38 +08:00 committed by GitHub
parent a73d5063d8
commit 2f1c5a19f1
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 25 additions and 30 deletions

View file

@ -48,9 +48,7 @@ func CopyDir(source string, dest string) (err error) {
return err
}
directory, _ := os.Open(source)
objects, err := directory.Readdir(-1)
objects, err := os.ReadDir(source)
for _, obj := range objects {
sourcefilepointer := source + "/" + obj.Name()

View file

@ -5,7 +5,6 @@ import (
"errors"
"fmt"
"io"
"io/ioutil"
"os"
"path"
"path/filepath"
@ -61,7 +60,7 @@ func FindGitRevision(ctx context.Context, file string) (shortSha string, sha str
return "", "", err
}
bts, err := ioutil.ReadFile(filepath.Join(gitDir, "HEAD"))
bts, err := os.ReadFile(filepath.Join(gitDir, "HEAD"))
if err != nil {
return "", "", err
}
@ -70,7 +69,7 @@ func FindGitRevision(ctx context.Context, file string) (shortSha string, sha str
var refBuf []byte
if strings.HasPrefix(ref, "refs/") {
// load commitid ref
refBuf, err = ioutil.ReadFile(filepath.Join(gitDir, ref))
refBuf, err = os.ReadFile(filepath.Join(gitDir, ref))
if err != nil {
return "", "", err
}
@ -151,7 +150,7 @@ func findGitPrettyRef(ctx context.Context, head, root, sub string) (string, erro
return nil
}
var bts []byte
if bts, err = ioutil.ReadFile(path); err != nil {
if bts, err = os.ReadFile(path); err != nil {
return err
}
var pointsTo = strings.TrimSpace(string(bts))

View file

@ -3,7 +3,6 @@ package git
import (
"context"
"fmt"
"io/ioutil"
"os"
"os/exec"
"path/filepath"
@ -45,7 +44,7 @@ func TestFindGitSlug(t *testing.T) {
}
func testDir(t *testing.T) string {
basedir, err := ioutil.TempDir("", "act-test")
basedir, err := os.MkdirTemp("", "act-test")
require.NoError(t, err)
t.Cleanup(func() { _ = os.RemoveAll(basedir) })
return basedir
@ -53,7 +52,7 @@ func testDir(t *testing.T) string {
func cleanGitHooks(dir string) error {
hooksDir := filepath.Join(dir, ".git", "hooks")
files, err := ioutil.ReadDir(hooksDir)
files, err := os.ReadDir(hooksDir)
if err != nil {
if os.IsNotExist(err) {
return nil

View file

@ -2,7 +2,7 @@ package container
import (
"context"
"io/ioutil"
"io"
"testing"
"github.com/docker/docker/api/types"
@ -45,7 +45,7 @@ func TestImageExistsLocally(t *testing.T) {
})
assert.Nil(t, err)
defer readerDefault.Close()
_, err = ioutil.ReadAll(readerDefault)
_, err = io.ReadAll(readerDefault)
assert.Nil(t, err)
imageDefaultArchExists, err := ImageExistsLocally(ctx, "node:16-buster-slim", "linux/amd64")
@ -58,7 +58,7 @@ func TestImageExistsLocally(t *testing.T) {
})
assert.Nil(t, err)
defer readerArm64.Close()
_, err = ioutil.ReadAll(readerArm64)
_, err = io.ReadAll(readerArm64)
assert.Nil(t, err)
imageArm64Exists, err := ImageExistsLocally(ctx, "node:16-buster-slim", "linux/arm64")

View file

@ -8,7 +8,6 @@ import (
"errors"
"fmt"
"io"
"io/ioutil"
"os"
"path/filepath"
"regexp"
@ -769,7 +768,7 @@ func (cr *containerReference) waitForCommand(ctx context.Context, isTerminal boo
func (cr *containerReference) copyDir(dstPath string, srcPath string, useGitIgnore bool) common.Executor {
return func(ctx context.Context) error {
logger := common.Logger(ctx)
tarFile, err := ioutil.TempFile("", "act")
tarFile, err := os.CreateTemp("", "act")
if err != nil {
return err
}

View file

@ -3,7 +3,7 @@ package model
import (
"fmt"
"io"
"io/ioutil"
"io/fs"
"math"
"os"
"path/filepath"
@ -51,7 +51,7 @@ func (r *Run) Job() *Job {
}
type WorkflowFiles struct {
workflowFileInfo os.FileInfo
workflowDirEntry os.DirEntry
dirPath string
}
@ -74,7 +74,7 @@ func NewWorkflowPlanner(path string, noWorkflowRecurse bool) (WorkflowPlanner, e
if fi.IsDir() {
log.Debugf("Loading workflows from '%s'", path)
if noWorkflowRecurse {
files, err := ioutil.ReadDir(path)
files, err := os.ReadDir(path)
if err != nil {
return nil, err
}
@ -82,7 +82,7 @@ func NewWorkflowPlanner(path string, noWorkflowRecurse bool) (WorkflowPlanner, e
for _, v := range files {
workflows = append(workflows, WorkflowFiles{
dirPath: path,
workflowFileInfo: v,
workflowDirEntry: v,
})
}
} else {
@ -97,7 +97,7 @@ func NewWorkflowPlanner(path string, noWorkflowRecurse bool) (WorkflowPlanner, e
log.Debugf("Found workflow '%s' in '%s'", f.Name(), p)
workflows = append(workflows, WorkflowFiles{
dirPath: filepath.Dir(p),
workflowFileInfo: f,
workflowDirEntry: fs.FileInfoToDirEntry(f),
})
}
@ -112,7 +112,7 @@ func NewWorkflowPlanner(path string, noWorkflowRecurse bool) (WorkflowPlanner, e
workflows = append(workflows, WorkflowFiles{
dirPath: dirname,
workflowFileInfo: fi,
workflowDirEntry: fs.FileInfoToDirEntry(fi),
})
}
if err != nil {
@ -121,9 +121,9 @@ func NewWorkflowPlanner(path string, noWorkflowRecurse bool) (WorkflowPlanner, e
wp := new(workflowPlanner)
for _, wf := range workflows {
ext := filepath.Ext(wf.workflowFileInfo.Name())
ext := filepath.Ext(wf.workflowDirEntry.Name())
if ext == ".yml" || ext == ".yaml" {
f, err := os.Open(filepath.Join(wf.dirPath, wf.workflowFileInfo.Name()))
f, err := os.Open(filepath.Join(wf.dirPath, wf.workflowDirEntry.Name()))
if err != nil {
return nil, err
}
@ -133,19 +133,19 @@ func NewWorkflowPlanner(path string, noWorkflowRecurse bool) (WorkflowPlanner, e
if err != nil {
_ = f.Close()
if err == io.EOF {
return nil, fmt.Errorf("unable to read workflow '%s': file is empty: %w", wf.workflowFileInfo.Name(), err)
return nil, fmt.Errorf("unable to read workflow '%s': file is empty: %w", wf.workflowDirEntry.Name(), err)
}
return nil, fmt.Errorf("workflow is not valid. '%s': %w", wf.workflowFileInfo.Name(), err)
return nil, fmt.Errorf("workflow is not valid. '%s': %w", wf.workflowDirEntry.Name(), err)
}
_, err = f.Seek(0, 0)
if err != nil {
_ = f.Close()
return nil, fmt.Errorf("error occurring when resetting io pointer in '%s': %w", wf.workflowFileInfo.Name(), err)
return nil, fmt.Errorf("error occurring when resetting io pointer in '%s': %w", wf.workflowDirEntry.Name(), err)
}
workflow.File = wf.workflowFileInfo.Name()
workflow.File = wf.workflowDirEntry.Name()
if workflow.Name == "" {
workflow.Name = wf.workflowFileInfo.Name()
workflow.Name = wf.workflowDirEntry.Name()
}
jobNameRegex := regexp.MustCompile(`^([[:alpha:]_][[:alnum:]_\-]*)$`)

View file

@ -3,7 +3,7 @@ package runner
import (
"context"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"regexp"
"runtime"
@ -111,7 +111,7 @@ func New(runnerConfig *Config) (Runner, error) {
runner.eventJSON = "{}"
if runnerConfig.EventPath != "" {
log.Debugf("Reading event.json from %s", runner.config.EventPath)
eventJSONBytes, err := ioutil.ReadFile(runner.config.EventPath)
eventJSONBytes, err := os.ReadFile(runner.config.EventPath)
if err != nil {
return nil, err
}