Skip to content

Commit 537ac21

Browse files
feat: move from using ioutils package to io and os packages (#649)
* feat: move from using ioutils package to io and os packages * feat: use WalkDir instead of Walk to improve performance Signed-off-by: Ashok Pon Kumar <[email protected]>
1 parent 1fee992 commit 537ac21

35 files changed

+97
-114
lines changed

apiresource/utils.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ package apiresource
1818

1919
import (
2020
"fmt"
21-
"io/ioutil"
2221
"os"
2322
"path/filepath"
2423
"reflect"
@@ -71,7 +70,7 @@ func writeObjects(outputPath string, objs []runtime.Object) ([]string, error) {
7170
continue
7271
}
7372
yamlPath := filepath.Join(outputPath, getFilename(obj))
74-
if err := ioutil.WriteFile(yamlPath, objYamlBytes, common.DefaultFilePermission); err != nil {
73+
if err := os.WriteFile(yamlPath, objYamlBytes, common.DefaultFilePermission); err != nil {
7574
logrus.Errorf("failed to write the yaml to file at path %s . Error: %q", yamlPath, err)
7675
continue
7776
}

common/sshkeys/sshkeys.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ import (
2222
"crypto/x509"
2323
"encoding/pem"
2424
"fmt"
25-
"io/ioutil"
25+
"os"
2626
"os/user"
2727
"path/filepath"
2828

@@ -112,7 +112,7 @@ Do you want to load the private ssh keys from [%s]?:`
112112
}
113113

114114
// Ask which keys we should consider
115-
finfos, err := ioutil.ReadDir(privateKeyDir)
115+
finfos, err := os.ReadDir(privateKeyDir)
116116
if err != nil {
117117
logrus.Errorf("Failed to read the ssh directory at path %q Error: %q", privateKeyDir, err)
118118
return
@@ -154,7 +154,7 @@ func marshalECDSAIntoPEM(key *ecdsa.PrivateKey) string {
154154

155155
func loadSSHKey(filename string) (string, error) {
156156
path := filepath.Join(privateKeyDir, filename)
157-
fileBytes, err := ioutil.ReadFile(path)
157+
fileBytes, err := os.ReadFile(path)
158158
if err != nil {
159159
logrus.Errorf("Failed to read the private key file at path %q Error: %q", path, err)
160160
return "", err

common/utils.go

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ import (
2525
"fmt"
2626
"hash/crc64"
2727
"io"
28-
"io/ioutil"
2928
"math"
3029
"math/rand"
3130
"net/url"
@@ -57,7 +56,7 @@ func GetFilesByExt(inputPath string, exts []string) ([]string, error) {
5756
} else if !info.IsDir() {
5857
logrus.Warnf("The path %q is not a directory.", inputPath)
5958
}
60-
err := filepath.Walk(inputPath, func(path string, info os.FileInfo, err error) error {
59+
err := filepath.WalkDir(inputPath, func(path string, info os.DirEntry, err error) error {
6160
if err != nil && path == inputPath { // if walk for root search path return gets error
6261
// then stop walking and return this error
6362
return err
@@ -109,7 +108,7 @@ func GetFilesByName(inputPath string, names []string, nameRegexes []string) ([]s
109108
}
110109
compiledNameRegexes = append(compiledNameRegexes, compiledNameRegex)
111110
}
112-
err := filepath.Walk(inputPath, func(path string, info os.FileInfo, err error) error {
111+
err := filepath.WalkDir(inputPath, func(path string, info os.DirEntry, err error) error {
113112
if err != nil && path == inputPath { // if walk for root search path return gets error
114113
// then stop walking and return this error
115114
return err
@@ -190,7 +189,7 @@ func GetFilesInCurrentDirectory(path string, fileNames, fileNameRegexes []string
190189

191190
//YamlAttrPresent returns YAML attributes
192191
func YamlAttrPresent(path string, attr string) (bool, interface{}) {
193-
yamlFile, err := ioutil.ReadFile(path)
192+
yamlFile, err := os.ReadFile(path)
194193
if err != nil {
195194
logrus.Warnf("Error in reading yaml file %s: %s. Skipping", path, err)
196195
return false, nil
@@ -247,12 +246,12 @@ func WriteYaml(outputPath string, data interface{}) error {
247246
logrus.Errorf("Failed to encode the object as a yaml string. Error: %q", err)
248247
return err
249248
}
250-
return ioutil.WriteFile(outputPath, yamlBytes, DefaultFilePermission)
249+
return os.WriteFile(outputPath, yamlBytes, DefaultFilePermission)
251250
}
252251

253252
// ReadYaml reads an yaml into an object
254253
func ReadYaml(file string, data interface{}) error {
255-
yamlFile, err := ioutil.ReadFile(file)
254+
yamlFile, err := os.ReadFile(file)
256255
if err != nil {
257256
logrus.Debugf("Error in reading yaml file %s: %s.", file, err)
258257
return err
@@ -282,7 +281,7 @@ func ReadYaml(file string, data interface{}) error {
282281
// It checks if apiVersion to see if the group is move2kube and also reports if the
283282
// version is different from the expected version.
284283
func ReadMove2KubeYaml(path string, out interface{}) error {
285-
yamlData, err := ioutil.ReadFile(path)
284+
yamlData, err := os.ReadFile(path)
286285
if err != nil {
287286
logrus.Errorf("Failed to read the yaml file at path %s Error: %q", path, err)
288287
return err
@@ -327,7 +326,7 @@ func ReadMove2KubeYaml(path string, out interface{}) error {
327326
// ReadMove2KubeYamlStrict is like ReadMove2KubeYaml but returns an error
328327
// when it finds unknown fields in the yaml
329328
func ReadMove2KubeYamlStrict(path string, out interface{}, kind string) error {
330-
yamlData, err := ioutil.ReadFile(path)
329+
yamlData, err := os.ReadFile(path)
331330
if err != nil {
332331
logrus.Debugf("Failed to read the yaml file at path %s Error: %q", path, err)
333332
return err
@@ -400,7 +399,7 @@ func WriteJSON(outputPath string, data interface{}) error {
400399
logrus.Error("Error while Encoding object")
401400
return err
402401
}
403-
err := ioutil.WriteFile(outputPath, b.Bytes(), DefaultFilePermission)
402+
err := os.WriteFile(outputPath, b.Bytes(), DefaultFilePermission)
404403
if err != nil {
405404
logrus.Errorf("Error writing json to file: %s", err)
406405
return err
@@ -410,7 +409,7 @@ func WriteJSON(outputPath string, data interface{}) error {
410409

411410
// ReadJSON reads an json into an object
412411
func ReadJSON(file string, data interface{}) error {
413-
jsonFile, err := ioutil.ReadFile(file)
412+
jsonFile, err := os.ReadFile(file)
414413
if err != nil {
415414
logrus.Debugf("Error in reading json file %s: %s.", file, err)
416415
return err
@@ -426,7 +425,7 @@ func ReadJSON(file string, data interface{}) error {
426425

427426
// ReadXML reads an json into an object
428427
func ReadXML(file string, data interface{}) error {
429-
xmlFile, err := ioutil.ReadFile(file)
428+
xmlFile, err := os.ReadFile(file)
430429
if err != nil {
431430
logrus.Debugf("Error in reading xml file %s: %s.", file, err)
432431
return err
@@ -758,7 +757,7 @@ func CreateAssetsData(assetsFS embed.FS, permissions map[string]int) (assetsPath
758757
}
759758

760759
// Try to create a new temporary directory for the assets.
761-
if newTempPath, err := ioutil.TempDir("", types.AppName+"*"); err != nil {
760+
if newTempPath, err := os.MkdirTemp("", types.AppName+"*"); err != nil {
762761
logrus.Errorf("Unable to create temp dir. Defaulting to local path.")
763762
} else {
764763
tempPath = newTempPath

environment/container/dockerengine.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ import (
2020
"bytes"
2121
"context"
2222
"fmt"
23-
"io/ioutil"
23+
"io"
2424

2525
"github.com/docker/docker/api/types"
2626
"github.com/docker/docker/api/types/container"
@@ -74,7 +74,7 @@ func (e *dockerEngine) pullImage(image string) bool {
7474
e.availableImages[image] = false
7575
return false
7676
}
77-
if b, err := ioutil.ReadAll(out); err == nil {
77+
if b, err := io.ReadAll(out); err == nil {
7878
logrus.Debug(cast.ToString(b))
7979
}
8080
e.availableImages[image] = true
@@ -118,11 +118,11 @@ func (e *dockerEngine) RunCmdInContainer(containerID string, cmd environmenttype
118118
return "", "", 0, e.ctx.Err()
119119
}
120120

121-
stdoutbytes, err := ioutil.ReadAll(&outBuf)
121+
stdoutbytes, err := io.ReadAll(&outBuf)
122122
if err != nil {
123123
return
124124
}
125-
stderrbytes, err := ioutil.ReadAll(&errBuf)
125+
stderrbytes, err := io.ReadAll(&errBuf)
126126
if err != nil {
127127
return
128128
}
@@ -249,7 +249,7 @@ func (e *dockerEngine) BuildImage(image, context, dockerfile string) (err error)
249249
return err
250250
}
251251
defer resp.Body.Close()
252-
response, err := ioutil.ReadAll(resp.Body)
252+
response, err := io.ReadAll(resp.Body)
253253
if err != nil {
254254
logrus.Errorf("Unable to read data from image build process : %s", err)
255255
return err
@@ -344,7 +344,7 @@ func (e *dockerEngine) RunContainer(image string, cmd environmenttypes.Command,
344344
return "", true, err
345345
}
346346
logs := ""
347-
if b, err := ioutil.ReadAll(out); err == nil {
347+
if b, err := io.ReadAll(out); err == nil {
348348
logs = cast.ToString(b)
349349
}
350350
if status.StatusCode != 0 {

environment/environment.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ package environment
1919
import (
2020
"bytes"
2121
"fmt"
22-
"io/ioutil"
2322
"net"
2423
"os"
2524
"path/filepath"
@@ -87,7 +86,7 @@ type EnvironmentInstance interface {
8786

8887
// NewEnvironment creates a new environment
8988
func NewEnvironment(envInfo EnvInfo, grpcQAReceiver net.Addr, c environmenttypes.Container) (env *Environment, err error) {
90-
tempPath, err := ioutil.TempDir(common.TempPath, "environment-"+envInfo.Name+"-*")
89+
tempPath, err := os.MkdirTemp(common.TempPath, "environment-"+envInfo.Name+"-*")
9190
if err != nil {
9291
logrus.Errorf("Unable to create temp dir : %s", err)
9392
return env, err
@@ -369,7 +368,7 @@ func (e *Environment) ProcessPathMappings(pathMappings []transformertypes.PathMa
369368
pathStr := path.String()
370369
logrus.Debugf("Output of environment template: %s\n", pathStr)
371370
if filepath.IsAbs(pathStr) {
372-
tempOutputPath, err := ioutil.TempDir(e.TempPath, "*")
371+
tempOutputPath, err := os.MkdirTemp(e.TempPath, "*")
373372
if err != nil {
374373
logrus.Errorf("Unable to create temp dir : %s", err)
375374
continue

environment/local.go

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ import (
2020
"bytes"
2121
"errors"
2222
"fmt"
23-
"io/ioutil"
2423
"net"
2524
"os"
2625
"os/exec"
@@ -48,12 +47,12 @@ func NewLocal(envInfo EnvInfo, grpcQAReceiver net.Addr) (ei EnvironmentInstance,
4847
EnvInfo: envInfo,
4948
GRPCQAReceiver: grpcQAReceiver,
5049
}
51-
local.WorkspaceContext, err = ioutil.TempDir(local.TempPath, types.AppNameShort)
50+
local.WorkspaceContext, err = os.MkdirTemp(local.TempPath, types.AppNameShort)
5251
if err != nil {
5352
logrus.Errorf("Unable to create temp dir : %s", err)
5453
return local, err
5554
}
56-
local.WorkspaceSource, err = ioutil.TempDir(local.TempPath, workspaceDir)
55+
local.WorkspaceSource, err = os.MkdirTemp(local.TempPath, workspaceDir)
5756
if err != nil {
5857
logrus.Errorf("Unable to create temp dir : %s", err)
5958
}
@@ -122,7 +121,7 @@ func (e *Local) Destroy() error {
122121

123122
// Download downloads the path to outside the environment
124123
func (e *Local) Download(path string) (string, error) {
125-
output, err := ioutil.TempDir(e.TempPath, "*")
124+
output, err := os.MkdirTemp(e.TempPath, "*")
126125
if err != nil {
127126
logrus.Errorf("Unable to create temp dir : %s", err)
128127
return path, err
@@ -145,7 +144,7 @@ func (e *Local) Download(path string) (string, error) {
145144

146145
// Upload uploads the path from outside the environment into it
147146
func (e *Local) Upload(outpath string) (envpath string, err error) {
148-
envpath, err = ioutil.TempDir(e.TempPath, "*")
147+
envpath, err = os.MkdirTemp(e.TempPath, "*")
149148
if err != nil {
150149
logrus.Errorf("Unable to create temp dir : %s", err)
151150
return outpath, err

environment/peercontainer.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@ package environment
1818

1919
import (
2020
"fmt"
21-
"io/ioutil"
2221
"net"
22+
"os"
2323
"path/filepath"
2424
"strings"
2525

@@ -139,7 +139,7 @@ func (e *PeerContainer) Destroy() error {
139139

140140
// Download downloads the path to outside the environment
141141
func (e *PeerContainer) Download(path string) (string, error) {
142-
output, err := ioutil.TempDir(e.TempPath, "*")
142+
output, err := os.MkdirTemp(e.TempPath, "*")
143143
if err != nil {
144144
logrus.Errorf("Unable to create temp dir : %s", err)
145145
return path, err

environment/processsharedcontainer.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,14 +47,14 @@ func NewProcessSharedContainer(name string, source string, context string, pid i
4747
4848
if env.Container.CID == "" {
4949
if env.Container.PID == 0 {
50-
env.OutContext, err = ioutil.TempDir(env.TempPath, types.AppNameShort)
50+
env.OutContext, err = os.MkdirTemp(env.TempPath, types.AppNameShort)
5151
if err != nil {
5252
logrus.Errorf("Unable to create temp dir : %s", err)
5353
}
5454
if err := filesystem.Replicate(env.OutContext, env.EnvContext); err != nil {
5555
logrus.Errorf("Unable to copy contents to directory %s, dp: %s", env.OutSource, env.EnvSource, err)
5656
}
57-
env.OutSource, err = ioutil.TempDir(env.TempPath, workspaceDir)
57+
env.OutSource, err = os.MkdirTemp(env.TempPath, workspaceDir)
5858
if err != nil {
5959
logrus.Errorf("Unable to create temp dir : %s", err)
6060
}

filesystem/processor.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ package filesystem
1818

1919
import (
2020
"fmt"
21-
"io/ioutil"
2221
"os"
2322
"path/filepath"
2423

@@ -88,7 +87,7 @@ func (p *processor) processFile(source, destination string) error {
8887

8988
func (p *processor) processDirectory(source, destination string) error {
9089
destEntryNames := map[string]bool{}
91-
entries, err := ioutil.ReadDir(source)
90+
entries, err := os.ReadDir(source)
9291
if err != nil {
9392
return err
9493
}
@@ -103,7 +102,7 @@ func (p *processor) processDirectory(source, destination string) error {
103102
logrus.Errorf("Error during mismatch callback for %s, %s", source, destination)
104103
}
105104
} else {
106-
destEntries, err := ioutil.ReadDir(destination)
105+
destEntries, err := os.ReadDir(destination)
107106
if err != nil {
108107
logrus.Errorf("Unable to process directory %s : %s", destination, err)
109108
} else {

filesystem/templatecopy.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ package filesystem
1818

1919
import (
2020
"bytes"
21-
"io/ioutil"
2221
"os"
2322
"path/filepath"
2423
"text/template"
@@ -151,7 +150,7 @@ func writeTemplateToFile(tpl string, config interface{}, writepath string,
151150
logrus.Warnf("Unable to transform template %q to string using the data %v", tpl, config)
152151
return err
153152
}
154-
err = ioutil.WriteFile(writepath, tplbuffer.Bytes(), filemode)
153+
err = os.WriteFile(writepath, tplbuffer.Bytes(), filemode)
155154
if err != nil {
156155
logrus.Warnf("Error writing file at %s : %s", writepath, err)
157156
return err

0 commit comments

Comments
 (0)