Skip to content

Commit

Permalink
Merge branch 'master' into onset
Browse files Browse the repository at this point in the history
  • Loading branch information
caarlos0 committed Aug 11, 2021
2 parents 8d3a039 + ed139cd commit e1996f6
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 12 deletions.
38 changes: 37 additions & 1 deletion README.md
Expand Up @@ -150,7 +150,7 @@ type config struct {
## Not Empty fields

While `required` demands the environment variable to be check, it doesn't check its value.
If you want to make sure the environment is set and not emtpy, you need to use the `notEmpty` tag option instead (`env:"SOME_ENV,notEmpty"`).
If you want to make sure the environment is set and not empty, you need to use the `notEmpty` tag option instead (`env:"SOME_ENV,notEmpty"`).

Example:

Expand Down Expand Up @@ -290,6 +290,7 @@ func main() {
}
```


### On set hooks

You might want to listen to value sets and, for example, log something or do some other kind of logic.
Expand Down Expand Up @@ -328,6 +329,41 @@ func main() {
}
```

## Making all fields to required

You can make all fields that don't have a default value be required by setting the `RequiredIfNoDef: true` in the `Options`.

For example

```go
package main

import (
"fmt"
"log"

"github.com/caarlos0/env/v6"
)

type Config struct {
Username string `env:"USERNAME" envDefault:"admin"`
Password string `env:"PASSWORD"`
}

func main() {
cfg := &Config{}
opts := &env.Options{RequiredIfNoDef: true}

// Load env vars.
if err := env.Parse(cfg, opts); err != nil {
log.Fatal(err)
}

// Print the loaded data.
fmt.Printf("%+v\n", cfg.envData)
}
```

## Stargazers over time

[![Stargazers over time](https://starchart.cc/caarlos0/env.svg)](https://starchart.cc/caarlos0/env)
6 changes: 4 additions & 2 deletions env.go
Expand Up @@ -229,19 +229,21 @@ func doParse(ref reflect.Value, funcMap map[reflect.Type]ParserFunc, opts []Opti
}

func get(field reflect.StructField, opts []Options) (val string, err error) {
var required bool = opts[0].RequiredIfNoDef
var exists bool
var isDefault bool
var loadFile bool
var unset bool
var notEmpty bool

required := opts[0].RequiredIfNoDef
expand := strings.EqualFold(field.Tag.Get("envExpand"), "true")

key, tags := parseKeyForOption(field.Tag.Get(getTagName(opts)))

for _, tag := range tags {
switch tag {
case "":
break
continue
case "file":
loadFile = true
case "required":
Expand Down
20 changes: 11 additions & 9 deletions env_test.go
Expand Up @@ -1377,21 +1377,23 @@ func TestCustomTimeParser(t *testing.T) {
}

func TestRequiredIfNoDefOption(t *testing.T) {
is := is.New(t)

type config struct {
Name string `env:"NAME"`
Genre string `env:"GENRE" envDefault:"Unknown"`
}

var cfg config
is.NoErr(Parse(&cfg))
isErrorWithMessage(t, Parse(&cfg, Options{RequiredIfNoDef: true}), `env: required environment variable "NAME" is not set`)

os.Setenv("NAME", "John")
defer os.Clearenv()
// should not trigger an error for the missing 'GENRE' env because it has a default value.
is.NoErr(Parse(&cfg, Options{RequiredIfNoDef: true}))
t.Run("missing", func(t *testing.T) {
isErrorWithMessage(t, Parse(&cfg, Options{RequiredIfNoDef: true}), `env: required environment variable "NAME" is not set`)
})

t.Run("all set", func(t *testing.T) {
os.Setenv("NAME", "John")
t.Cleanup(os.Clearenv)

// should not trigger an error for the missing 'GENRE' env because it has a default value.
is.New(t).NoErr(Parse(&cfg, Options{RequiredIfNoDef: true}))
})
}

func isErrorWithMessage(tb testing.TB, err error, msg string) {
Expand Down

0 comments on commit e1996f6

Please sign in to comment.