beats icon indicating copy to clipboard operation
beats copied to clipboard

refactor: replace deprecated io/ioutil package

Open blue-troy opened this issue 1 year ago • 14 comments

io/ioutil package is deprecated

Proposed commit message

As of Go 1.16, the same functionality is now provided by package io or package os, and those implementations should be preferred. Successor functionality in io or os is more performant than the deprecated functionality in io/ioutil.

Checklist

  • [x] My code follows the style guidelines of this project
  • [ ] ~~I have made corresponding changes to the documentation~~
  • [ ] ~~I have made corresponding change to the default configuration files~~
  • [ ] ~~I have added tests that prove my fix is effective or that my feature works~~
  • [ ] ~~I have added an entry in CHANGELOG.next.asciidoc or CHANGELOG-developer.next.asciidoc.~~

Related issues

  • Closes #37466

blue-troy avatar Dec 20 '23 07:12 blue-troy

💚 CLA has been signed

This pull request does not have a backport label. If this is a bug or security fix, could you label this PR @blue-troy? 🙏. For such, you'll need to label your PR with:

  • The upcoming major version of the Elastic Stack
  • The upcoming minor version of the Elastic Stack (if you're not pushing a breaking change)

To fixup this pull request, you need to add the backport labels for the needed branches, such as:

  • backport-v8./d.0 is the label to automatically backport to the 8./d branch. /d is the digit

mergify[bot] avatar Dec 20 '23 07:12 mergify[bot]

:grey_exclamation: Build Aborted

The PR is not allowed to run in the CI yet

the below badges are clickable and redirect to their specific view in the CI or DOCS Pipeline View Test View Changes Artifacts preview

Expand to view the summary

Build stats

  • Start Time: 2023-12-20T07:46:50.024+0000

  • Duration: 6 min 35 sec

Steps errors 2

Expand to view the steps failures

Load a resource file from a library
  • Took 0 min 0 sec . View more details here
  • Description: approval-list/elastic/beats.yml
Error signal
  • Took 0 min 0 sec . View more details here
  • Description: githubApiCall: The REST API call https://api.github.com/orgs/elastic/members/blue-troy return the message : java.lang.Exception: httpRequest: Failure connecting to the service https://api.github.com/orgs/elastic/members/blue-troy : httpRequest: Failure connecting to the service https://api.github.com/orgs/elastic/members/blue-troy : Code: 404Error: {"message":"User does not exist or is not a member of the organization","documentation_url":"https://docs.github.com/rest/orgs/members#check-organization-membership-for-a-user"}

:robot: GitHub comments

Expand to view the GitHub comments

To re-run your PR in the CI, just comment with:

  • /test : Re-trigger the build.

  • /package : Generate the packages and run the E2E tests.

  • /beats-tester : Run the installation tests with beats-tester.

  • run elasticsearch-ci/docs : Re-trigger the docs validation. (use unformatted text in the comment!)

elasticmachine avatar Dec 20 '23 07:12 elasticmachine

Pinging @elastic/security-external-integrations (Team:Security-External Integrations)

elasticmachine avatar Dec 20 '23 07:12 elasticmachine

There is some lint issue right now, but not caused by this pr.

blue-troy avatar Dec 20 '23 08:12 blue-troy

@blue-troy thanks for the PR!

There is some lint issue right now, but not caused by this pr.

If the lint issues are not from your code, you can ignore them.

belimawr avatar Dec 21 '23 17:12 belimawr

/test

belimawr avatar Dec 21 '23 17:12 belimawr

:green_heart: Build Succeeded

the below badges are clickable and redirect to their specific view in the CI or DOCS Pipeline View Test View Changes Artifacts preview preview

Expand to view the summary

Build stats

  • Duration: 191 min 1 sec

:grey_exclamation: Flaky test report

No test was executed to be analysed.

:robot: GitHub comments

Expand to view the GitHub comments

To re-run your PR in the CI, just comment with:

  • /test : Re-trigger the build.

  • /package : Generate the packages and run the E2E tests.

  • /beats-tester : Run the installation tests with beats-tester.

  • run elasticsearch-ci/docs : Re-trigger the docs validation. (use unformatted text in the comment!)

elasticmachine avatar Dec 21 '23 20:12 elasticmachine

It would be helpful for review to have a description of how the mutations here were performed. If it was done purely mechanically (hopefully it was), for example with a set of gofmt rewrites and a goimports cleanup, then the code used to make the changes can be included in the commit message to make use of git-generate.

For most of the case is just replace ioutil package to other package. but ioutil.ReadDir replaced with os.ReadDir returns a different struct:

before:

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

after:

	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)
	}

this pr is not generate by script for some import package issue.

blue-troy avatar Jan 15 '24 06:01 blue-troy

@blue-troy Ideally, the mechanical and manual changes would be separate. I took a look at the break down of changes and the mechanical changes touch 195 files while the manual changes only alter 15. If they are separate greater care can be applied to the review of the manual changes.

In cases where stdout and stderr are assigned io.Discard the assignment can either be removed (if first assignment) or replaced with nil since nil exec.Cmd.{Stdout,Stderr} are already the bit bucket.

efd6 avatar Jan 15 '24 20:01 efd6

This pull request is now in conflicts. Could you fix it? 🙏 To fixup this pull request, you can check out it locally. See documentation: https://help.github.com/articles/checking-out-pull-requests-locally/

git fetch upstream
git checkout -b refactor/ioutil upstream/refactor/ioutil
git merge upstream/main
git push upstream refactor/ioutil

mergify[bot] avatar Jan 25 '24 15:01 mergify[bot]

This pull request is now in conflicts. Could you fix it? 🙏 To fixup this pull request, you can check out it locally. See documentation: https://help.github.com/articles/checking-out-pull-requests-locally/

git fetch upstream
git checkout -b refactor/ioutil upstream/refactor/ioutil
git merge upstream/main
git push upstream refactor/ioutil

mergify[bot] avatar Feb 05 '24 15:02 mergify[bot]

Pinging @elastic/security-service-integrations (Team:Security-Service Integrations)

elasticmachine avatar Feb 08 '24 12:02 elasticmachine

Setting back to draft until the conflict are fixed.

pierrehilbert avatar Mar 07 '24 15:03 pierrehilbert