Skip to content

Commit

Permalink
Remove dep on github.com/caarlos0/env
Browse files Browse the repository at this point in the history
  • Loading branch information
dsh2dsh committed Oct 28, 2023
1 parent 119c113 commit 1f31c2b
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 24 deletions.
37 changes: 17 additions & 20 deletions doc_test.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
package dotenv_test

import (
"fmt"
"log"
"os"

"github.com/caarlos0/env/v9"

dotenv "github.com/dsh2dsh/expx-dotenv"
)

Expand All @@ -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.
Expand Down
21 changes: 20 additions & 1 deletion dotenv.go
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down Expand Up @@ -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 {
Expand Down
1 change: 0 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
Expand Down
2 changes: 0 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
@@ -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=
Expand Down

0 comments on commit 1f31c2b

Please sign in to comment.