Skip to content

Commit

Permalink
Remove LoadTo(),
Browse files Browse the repository at this point in the history
because it can be replaced by `Load()` with callback:
```go
err := dotenv.New().Load(func() error {
	return env.Parse(&cfg)
})
```
  • Loading branch information
dsh2dsh committed Oct 28, 2023
1 parent 763cb6b commit 119c113
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 109 deletions.
22 changes: 12 additions & 10 deletions doc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,47 +5,49 @@ import (
"log"
"os"

"github.com/caarlos0/env/v9"

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

func Example() {
env := dotenv.New()
if err := env.Load(); err != nil {
if err := dotenv.New().Load(); err != nil {
log.Fatalf("error loading .env files: %v", err)
}
}

func Example_chainedCalls() {
env := dotenv.New()
if err := env.WithDepth(1).WithEnvSuffix("test").Load(); err != nil {
if err := dotenv.New().WithDepth(1).WithEnvSuffix("test").Load(); err != nil {
log.Fatalf("error loading .env files: %v", err)
}
}

func Example_withParse() {
env := dotenv.New()
cfg := struct {
SomeOpt string `env:"ENV_VAR1"`
}{
SomeOpt: "some default value, because we don't have .env file(s)",
}
if err := env.LoadTo(&cfg); err != nil {

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 := dotenv.New()
// "ENV_ROOT" environment variable contains name of current environment.
env.WithRootDir(os.Getenv("ENV_ROOT"))
dotenv.New().WithRootDir(os.Getenv("ENV_ROOT"))
}

func ExampleLoader_WithRootFiles() {
env := dotenv.New()
// stop at dir, which contains ".git"
env.WithRootFiles(".git")
dotenv.New().WithRootFiles(".git")
}

func ExampleLoader_WithRootCallback() {
Expand Down
16 changes: 0 additions & 16 deletions dotenv.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import (
"os"
"path/filepath"

"github.com/caarlos0/env/v9"
"github.com/joho/godotenv"
)

Expand Down Expand Up @@ -204,21 +203,6 @@ func (self *Loader) Load(cbs ...func() error) error {
return nil
}

// LoadTo loads all .env files like [Loader.Load] and fills every received
// struct with values from environment variables, using [env.Parse]. It expects
// zero or more args are pointers to structs. Without args it has no difference
// from [Loader.Load].
func (self *Loader) LoadTo(to ...any) error {
return self.Load(func() error {
for _, v := range to {
if err := env.Parse(v); err != nil {
return fmt.Errorf("parse .env into struct: %w", err)
}
}
return nil
})
}

// FileExistsInDir checks if file named fname exists in dir named dirName and
// returns true, if it exists, or false.
//
Expand Down
83 changes: 0 additions & 83 deletions dotenv_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -420,89 +420,6 @@ func restoreEnvVars(t *testing.T) {
}
}

func TestLoadTo(t *testing.T) {
type testStruct struct {
TestVar string `env:"TEST_VAR1"`
}

tests := []struct {
name string
chDir string
call func(env *Loader, t *testing.T) []string
expected []string
}{
{
name: "optional args",
call: func(env *Loader, t *testing.T) (parsed []string) {
require.NoError(t, env.LoadTo())
return
},
},
{
name: "loaded nothing",
call: func(env *Loader, t *testing.T) []string {
cfg := testStruct{"default value"}
require.NoError(t, env.LoadTo(&cfg))
return []string{cfg.TestVar}
},
expected: []string{"default value"},
},
{
name: "loaded one",
chDir: "testdata",
call: func(env *Loader, t *testing.T) []string {
cfg := testStruct{"default value"}
require.NoError(t, env.LoadTo(&cfg))
return []string{cfg.TestVar}
},
expected: []string{"testdata"},
},
{
name: "loaded multiple",
chDir: "testdata",
call: func(env *Loader, t *testing.T) []string {
cfg1 := testStruct{"default value 1"}
cfg2 := struct {
TestVar string `env:"TEST_VAR2"`
}{"default value 2"}
require.NoError(t, env.LoadTo(&cfg1, &cfg2))
return []string{cfg1.TestVar, cfg2.TestVar}
},
expected: []string{"testdata", "testdata2"},
},
{
name: "with err from Load",
chDir: "testdata",
call: func(env *Loader, t *testing.T) (parsed []string) {
require.Error(t, env.WithEnvSuffix("error").LoadTo())
return
},
},
{
name: "with err from Parse",
chDir: "testdata",
call: func(env *Loader, t *testing.T) (parsed []string) {
cfg := struct {
TestVar bool `env:"TEST_VAR1"`
}{}
require.Error(t, env.LoadTo(&cfg))
return
},
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if tt.chDir != "" {
changeDir(t, tt.chDir)
}
restoreEnvVars(t)
env := New()
assert.Equal(t, tt.expected, tt.call(env, t))
})
}
}

func TestLoad_errorGetwd(t *testing.T) {
tmpDir := valueNoError[string](t)(os.MkdirTemp("", "expx-dotenv-"))
t.Cleanup(func() {
Expand Down

0 comments on commit 119c113

Please sign in to comment.