Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: ioutil.ReadAll() is deprecated, so removed it's dependency #202

Merged
merged 1 commit into from Feb 4, 2023
Merged

Fix: ioutil.ReadAll() is deprecated, so removed it's dependency #202

merged 1 commit into from Feb 4, 2023

Conversation

dreygur
Copy link
Contributor

@dreygur dreygur commented Feb 3, 2023

As ioutil.ReadAll() is deprecated, replaced it with buffer.

var buf bytes.Buffer
_, err := io.Copy(&buf, r)

Changes

diff --git a/godotenv.go b/godotenv.go
index 368e804..fd48c6a 100644
--- a/godotenv.go
+++ b/godotenv.go
@@ -4,20 +4,20 @@
 //
 // The TL;DR is that you make a .env file that looks something like
 //
-// 		SOME_ENV_VAR=somevalue
+//	SOME_ENV_VAR=somevalue
 //
 // and then in your go code you can call
 //
-// 		godotenv.Load()
+//	godotenv.Load()
 //
 // and all the env vars declared in .env will be available through os.Getenv("SOME_ENV_VAR")
 package godotenv
 
 import (
+	"bytes"
 	"errors"
 	"fmt"
 	"io"
-	"io/ioutil"
 	"os"
 	"os/exec"
 	"regexp"
@@ -30,12 +30,13 @@ const doubleQuoteSpecialChars = "\\\n\r\"!$`"
 
 // Parse reads an env file from io.Reader, returning a map of keys and values.
 func Parse(r io.Reader) (map[string]string, error) {
-	data, err := ioutil.ReadAll(r)
+	var buf bytes.Buffer
+	_, err := io.Copy(&buf, r)
 	if err != nil {
 		return nil, err
 	}
 
-	return UnmarshalBytes(data)
+	return UnmarshalBytes(buf.Bytes())
 }
 
 // Load will read your env file(s) and load them into ENV for this process.
@@ -46,7 +47,7 @@ func Parse(r io.Reader) (map[string]string, error) {
 //
 // You can otherwise tell it which files to load (there can be more than one) like:
 //
-//		godotenv.Load("fileone", "filetwo")
+//	godotenv.Load("fileone", "filetwo")
 //
 // It's important to note that it WILL NOT OVERRIDE an env variable that already exists - consider the .env file to set dev vars or sensible defaults.
 func Load(filenames ...string) (err error) {
@@ -69,7 +70,7 @@ func Load(filenames ...string) (err error) {
 //
 // You can otherwise tell it which files to load (there can be more than one) like:
 //
-//		godotenv.Overload("fileone", "filetwo")
+//	godotenv.Overload("fileone", "filetwo")
 //
 // It's important to note this WILL OVERRIDE an env variable that already exists - consider the .env file to forcefully set all vars.
 func Overload(filenames ...string) (err error) {

@2tef
Copy link
Contributor

2tef commented Feb 3, 2023

from ioutil.ReadAll:

Deprecated: As of Go 1.16, this function simply calls io.ReadAll.

what about using io.ReadAll?

@joho
Copy link
Owner

joho commented Feb 3, 2023

Thanks for spotting the deprecation. I agree, if io.ReadAll does the thing, let's use that.

@2tef
Copy link
Contributor

2tef commented Feb 4, 2023

there's a bit of a problem though, that'd mean bumping the go version to 1.16;
using io.Copy wouldn't affect the go version at all

@joho
Copy link
Owner

joho commented Feb 4, 2023

Ah that is also a good shout. CI only tests 1.16 and above but there's no point breaking old code bases over a couple of lines difference. I'm going to merge as-is. thanks!

@joho joho merged commit b311b26 into joho:main Feb 4, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants