Skip to content

Commit

Permalink
refactor: replace depricated io/ioutil package
Browse files Browse the repository at this point in the history
io/ioutil package is deprecated

Signed-off-by: blue-troy <[email protected]>
  • Loading branch information
blue-troy committed Dec 20, 2023
1 parent 50aac80 commit ccbcaed
Show file tree
Hide file tree
Showing 200 changed files with 514 additions and 592 deletions.
8 changes: 4 additions & 4 deletions dev-tools/cmd/asset/asset.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import (
"bufio"
"flag"
"fmt"
"io/ioutil"
"io"
"os"

"github.com/elastic/beats/v7/libbeat/asset"
Expand Down Expand Up @@ -67,15 +67,15 @@ func main() {
beatName = args[1]

r := bufio.NewReader(os.Stdin)
data, err = ioutil.ReadAll(r)
data, err = io.ReadAll(r)
if err != nil {
fmt.Fprintf(os.Stderr, "Error while reading from stdin: %v\n", err)
os.Exit(1)
}
} else {
file = input
beatName = args[0]
data, err = ioutil.ReadFile(input)
data, err = os.ReadFile(input)
if err != nil {
fmt.Fprintf(os.Stderr, "Invalid file path: %s\n", input)
os.Exit(1)
Expand All @@ -99,6 +99,6 @@ func main() {
if output == "-" {
os.Stdout.Write(bs)
} else {
ioutil.WriteFile(output, bs, 0640)
os.WriteFile(output, bs, 0640)
}
}
9 changes: 4 additions & 5 deletions dev-tools/cmd/license/license_generate.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 1 addition & 2 deletions dev-tools/cmd/module_fields/module_fields.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ package main
import (
"flag"
"fmt"
"io/ioutil"
"log"
"os"
"path/filepath"
Expand Down Expand Up @@ -104,7 +103,7 @@ func main() {
log.Fatalf("Error creating golang file from template: %v", err)
}

err = ioutil.WriteFile(filepath.Join(dir, module, "fields.go"), bs, 0644)
err = os.WriteFile(filepath.Join(dir, module, "fields.go"), bs, 0644)
if err != nil {
log.Fatalf("Error writing fields.go: %v", err)
}
Expand Down
3 changes: 1 addition & 2 deletions dev-tools/cmd/module_include_list/module_include_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import (
"bytes"
"flag"
"fmt"
"io/ioutil"
"log"
"os"
"path/filepath"
Expand Down Expand Up @@ -155,7 +154,7 @@ func main() {
}

// Write the output file.
if err = ioutil.WriteFile(outFile, buf.Bytes(), 0644); err != nil {
if err = os.WriteFile(outFile, buf.Bytes(), 0644); err != nil {
log.Fatalf("Failed writing output file: %v", err)
}
}
Expand Down
7 changes: 3 additions & 4 deletions dev-tools/cmd/update_go/update_go_version.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ package main
import (
"flag"
"fmt"
"io/ioutil"
"os"
"strings"
)
Expand Down Expand Up @@ -52,7 +51,7 @@ func main() {
}

func getGoVersion() string {
version, err := ioutil.ReadFile(".go-version")
version, err := os.ReadFile(".go-version")
checkErr(err)
return strings.TrimRight(string(version), "\r\n")
}
Expand All @@ -66,10 +65,10 @@ func checkErr(err error) {
func updateGoVersion(oldVersion, newVersion string) {
for _, file := range files {
fmt.Printf("Updating Go version from %s to %s in %s\n", oldVersion, newVersion, file)

Check failure on line 67 in dev-tools/cmd/update_go/update_go_version.go

View workflow job for this annotation

GitHub Actions / lint (darwin)

use of `fmt.Printf` forbidden by pattern `fmt.Print.*` (forbidigo)
content, err := ioutil.ReadFile(file)
content, err := os.ReadFile(file)
checkErr(err)
updatedContent := strings.ReplaceAll(string(content), oldVersion, newVersion)
err = ioutil.WriteFile(file, []byte(updatedContent), 0644)
err = os.WriteFile(file, []byte(updatedContent), 0644)
checkErr(err)
}
}
3 changes: 1 addition & 2 deletions dev-tools/mage/check.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"log"
"os"
"os/exec"
Expand Down Expand Up @@ -229,7 +228,7 @@ func CheckDashboardsFormat() error {

hasErrors := false
for _, file := range dashboardFiles {
d, err := ioutil.ReadFile(file)
d, err := os.ReadFile(file)
if err != nil {
return fmt.Errorf("failed to read dashboard file %s: %w", file, err)
}
Expand Down
11 changes: 5 additions & 6 deletions dev-tools/mage/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ import (
"errors"
"fmt"
"io"
"io/ioutil"
"log"
"net/http"
"os"
Expand Down Expand Up @@ -125,7 +124,7 @@ func joinMaps(args ...map[string]interface{}) map[string]interface{} {
}

func expandFile(src, dst string, args ...map[string]interface{}) error {
tmplData, err := ioutil.ReadFile(src)
tmplData, err := os.ReadFile(src)
if err != nil {
return fmt.Errorf("failed reading from template %v: %w", src, err)
}
Expand All @@ -140,7 +139,7 @@ func expandFile(src, dst string, args ...map[string]interface{}) error {
return err
}

if err = ioutil.WriteFile(createDir(dst), []byte(output), 0644); err != nil {
if err = os.WriteFile(createDir(dst), []byte(output), 0644); err != nil {
return fmt.Errorf("failed to write rendered template: %w", err)
}

Expand Down Expand Up @@ -262,13 +261,13 @@ func FindReplace(file string, re *regexp.Regexp, repl string) error {
return err
}

contents, err := ioutil.ReadFile(file)
contents, err := os.ReadFile(file)
if err != nil {
return err
}

out := re.ReplaceAllString(string(contents), repl)
return ioutil.WriteFile(file, []byte(out), info.Mode().Perm())
return os.WriteFile(file, []byte(out), info.Mode().Perm())
}

// MustFindReplace invokes FindReplace and panics if an error occurs.
Expand Down Expand Up @@ -773,7 +772,7 @@ func CreateSHA512File(file string) error {
computedHash := hex.EncodeToString(sum.Sum(nil))
out := fmt.Sprintf("%v %v", computedHash, filepath.Base(file))

return ioutil.WriteFile(file+".sha512", []byte(out), 0644)
return os.WriteFile(file+".sha512", []byte(out), 0644)
}

// Mage executes mage targets in the specified directory.
Expand Down
11 changes: 5 additions & 6 deletions dev-tools/mage/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import (
"bytes"
"errors"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"regexp"
Expand Down Expand Up @@ -196,7 +195,7 @@ func makeConfigTemplate(destination string, mode os.FileMode, confParams ConfigF
}
}

data, err := ioutil.ReadFile(confFile.Template)
data, err := os.ReadFile(confFile.Template)
if err != nil {
return fmt.Errorf("failed to read config template %q: %w", confFile.Template, err)
}
Expand Down Expand Up @@ -265,7 +264,7 @@ type moduleFieldsYmlData []struct {
}

func readModuleFieldsYml(path string) (title string, useShort bool, err error) {
data, err := ioutil.ReadFile(path)
data, err := os.ReadFile(path)
if err != nil {
return "", false, err
}
Expand Down Expand Up @@ -302,7 +301,7 @@ func moduleDashes(name string) string {
func GenerateModuleReferenceConfig(out string, moduleDirs ...string) error {
var moduleConfigs []moduleConfigTemplateData
for _, dir := range moduleDirs {
modules, err := ioutil.ReadDir(dir)
modules, err := os.ReadDir(dir)
if err != nil {
return err
}
Expand All @@ -327,7 +326,7 @@ func GenerateModuleReferenceConfig(out string, moduleDirs ...string) error {

var data []byte
for _, f := range files {
data, err = ioutil.ReadFile(f)
data, err = os.ReadFile(f)
if err != nil {
if os.IsNotExist(err) {
continue
Expand Down Expand Up @@ -365,5 +364,5 @@ func GenerateModuleReferenceConfig(out string, moduleDirs ...string) error {
"Modules": moduleConfigs,
})

return ioutil.WriteFile(createDir(out), []byte(config), 0644)
return os.WriteFile(createDir(out), []byte(config), 0644)
}
13 changes: 10 additions & 3 deletions dev-tools/mage/copy.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ package mage
import (
"fmt"
"io"
"io/ioutil"
"io/fs"
"os"
"path/filepath"
"regexp"
Expand Down Expand Up @@ -144,11 +144,18 @@ func (t *CopyTask) dirCopy(src, dest string, info os.FileInfo) error {
return fmt.Errorf("failed creating dirs: %w", err)
}

contents, err := ioutil.ReadDir(src)
contentEntries, err := os.ReadDir(src)
if err != nil {
return fmt.Errorf("failed to read dir %v: %w", src, err)
}

contents := make([]fs.FileInfo, 0, len(contentEntries))
for _, entry := range contentEntries {
content, err := entry.Info()
if err != nil {
return fmt.Errorf("failed to stat %v: %w", entry.Name(), err)
}
contents = append(contents, content)
}
for _, info := range contents {
srcFile := filepath.Join(src, info.Name())
destFile := filepath.Join(dest, info.Name())
Expand Down
5 changes: 2 additions & 3 deletions dev-tools/mage/gotest.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import (
"errors"
"fmt"
"io"
"io/ioutil"
"log"
"os"
"os/exec"
Expand Down Expand Up @@ -169,7 +168,7 @@ func DefaultTestBinaryArgs() TestBinaryArgs {
// Use MODULE=module to run only tests for `module`.
func GoTestIntegrationForModule(ctx context.Context) error {
module := EnvOr("MODULE", "")
modulesFileInfo, err := ioutil.ReadDir("./module")
modulesFileInfo, err := os.ReadDir("./module")
if err != nil {
return err
}
Expand Down Expand Up @@ -356,7 +355,7 @@ func makeCommand(ctx context.Context, env map[string]string, cmd string, args ..
for k, v := range env {
c.Env = append(c.Env, k+"="+v)
}
c.Stdout = ioutil.Discard
c.Stdout = io.Discard
if mg.Verbose() {
c.Stdout = os.Stdout
}
Expand Down
5 changes: 2 additions & 3 deletions dev-tools/mage/integtest_docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import (
"fmt"
"go/build"
"io"
"io/ioutil"
"os"
"path"
"path/filepath"
Expand Down Expand Up @@ -356,7 +355,7 @@ func StartIntegTestContainers() error {

func StopIntegTestContainers() error {
// Docker-compose rm is noisy. So only pass through stderr when in verbose.
out := ioutil.Discard
out := io.Discard
if mg.Verbose() {
out = os.Stderr
}
Expand All @@ -368,7 +367,7 @@ func StopIntegTestContainers() error {

_, err = sh.Exec(
composeEnv,
ioutil.Discard,
io.Discard,
out,
"docker-compose",
"-p", DockerComposeProjectName(),
Expand Down
10 changes: 5 additions & 5 deletions dev-tools/mage/kubernetes/kind.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ package kubernetes

import (
"fmt"
"io/ioutil"
"io"
"os"
"os/exec"
"path/filepath"
Expand Down Expand Up @@ -66,8 +66,8 @@ func (m *KindIntegrationTestStep) Setup(env map[string]string) error {
}

clusterName := kubernetesClusterName()
stdOut := ioutil.Discard
stdErr := ioutil.Discard
stdOut := io.Discard
stdErr := io.Discard
if mg.Verbose() {
stdOut = os.Stdout
stdErr = os.Stderr
Expand Down Expand Up @@ -116,8 +116,8 @@ func (m *KindIntegrationTestStep) Setup(env map[string]string) error {

// Teardown destroys the kubernetes cluster.
func (m *KindIntegrationTestStep) Teardown(env map[string]string) error {
stdOut := ioutil.Discard
stdErr := ioutil.Discard
stdOut := io.Discard
stdErr := io.Discard
if mg.Verbose() {
stdOut = os.Stdout
stdErr = os.Stderr
Expand Down
4 changes: 2 additions & 2 deletions dev-tools/mage/kubernetes/kuberemote.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,10 @@ import (
"encoding/pem"
"fmt"
"io"
"io/ioutil"
"net"
"net/http"
"net/url"
"os"
"os/exec"
"strings"
"time"
Expand Down Expand Up @@ -622,7 +622,7 @@ func podDone(event watch.Event) (bool, error) {
func createTempFile(content []byte) (string, error) {
randBytes := make([]byte, 16)
rand.Read(randBytes)
tmpfile, err := ioutil.TempFile("", hex.EncodeToString(randBytes))
tmpfile, err := os.CreateTemp("", hex.EncodeToString(randBytes))
if err != nil {
return "", err
}
Expand Down
Loading

0 comments on commit ccbcaed

Please sign in to comment.