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 return err
} }
directory, _ := os.Open(source) objects, err := os.ReadDir(source)
objects, err := directory.Readdir(-1)
for _, obj := range objects { for _, obj := range objects {
sourcefilepointer := source + "/" + obj.Name() sourcefilepointer := source + "/" + obj.Name()

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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