diff --git a/doc_test.go b/doc_test.go index ad71c5a..e77e7be 100644 --- a/doc_test.go +++ b/doc_test.go @@ -1,12 +1,9 @@ package dotenv_test import ( - "fmt" "log" "os" - "github.com/caarlos0/env/v9" - dotenv "github.com/dsh2dsh/expx-dotenv" ) @@ -22,23 +19,23 @@ func Example_chainedCalls() { } } -func Example_withParse() { - cfg := struct { - SomeOpt string `env:"ENV_VAR1"` - }{ - SomeOpt: "some default value, because we don't have .env file(s)", - } - - err := dotenv.New().Load(func() error { - return env.Parse(&cfg) //nolint:wrapcheck - }) - if err != nil { - log.Fatalf("error loading .env files: %v", err) - } - - fmt.Println(cfg.SomeOpt) - // Output: some default value, because we don't have .env file(s) -} +// func Example_withParse() { +// cfg := struct { +// SomeOpt string `env:"ENV_VAR1"` +// }{ +// SomeOpt: "some default value, because we don't have .env file(s)", +// } + +// err := dotenv.New().Load(func() error { +// return env.Parse(&cfg) //nolint:wrapcheck +// }) +// if err != nil { +// log.Fatalf("error loading .env files: %v", err) +// } + +// fmt.Println(cfg.SomeOpt) +// // Output: some default value, because we don't have .env file(s) +// } func ExampleLoader_WithRootDir() { // "ENV_ROOT" environment variable contains name of current environment. diff --git a/dotenv.go b/dotenv.go index 8e7a4d2..adac006 100644 --- a/dotenv.go +++ b/dotenv.go @@ -33,7 +33,7 @@ func (self stdFiler) Stat(name string) (os.FileInfo, error) { // searches for .env file(s) until it reaches of the root or any parent dir // where go.mod file exists. // -// Creation time options can be change by opts. +// Creation time options can be changed by opts. func New(opts ...Option) *Loader { l := &Loader{ rootDir: string(filepath.Separator), @@ -182,6 +182,25 @@ func (self *Loader) WithRootCallback(fn func(path string) (bool, error), // variable "A" defined in .env.local file, it can't be redefined by variable // "A" from .env file. Or if env variable "A" somehow defined before calling // Load, it keeps its value and can't be redefined by .env files. +// +// After succesfull loading of .env file(s) it calls functions from cbs one by +// one. It stops calling callbacks after first error. Here an example of using +// [env] to parse env vars into a struct: +// +// cfg := struct { +// SomeOpt string `env:"ENV_VAR1"` +// }{ +// SomeOpt: "some default value, because we don't have .env file(s)", +// } +// +// err := dotenv.New().Load(func() error { +// return env.Parse(&cfg) +// }) +// if err != nil { +// log.Fatalf("error loading .env files: %v", err) +// } +// +// [env]: https://github.com/caarlos0/env func (self *Loader) Load(cbs ...func() error) error { envs, err := self.lookupEnvFiles() if err != nil { diff --git a/go.mod b/go.mod index 2701929..005fe8b 100644 --- a/go.mod +++ b/go.mod @@ -3,7 +3,6 @@ module github.com/dsh2dsh/expx-dotenv go 1.21 require ( - github.com/caarlos0/env/v9 v9.0.0 github.com/joho/godotenv v1.5.1 github.com/stretchr/testify v1.8.4 ) diff --git a/go.sum b/go.sum index 2cfc29f..6e0cb05 100644 --- a/go.sum +++ b/go.sum @@ -1,5 +1,3 @@ -github.com/caarlos0/env/v9 v9.0.0 h1:SI6JNsOA+y5gj9njpgybykATIylrRMklbs5ch6wO6pc= -github.com/caarlos0/env/v9 v9.0.0/go.mod h1:ye5mlCVMYh6tZ+vCgrs/B95sj88cg5Tlnc0XIzgZ020= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=