Skip to content

Commit

Permalink
Update documentation (#18)
Browse files Browse the repository at this point in the history
- add integration documentation
- update code snippets
  • Loading branch information
ilyakaznacheev authored Dec 5, 2019
1 parent dc1dbed commit a5b75ca
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 16 deletions.
56 changes: 40 additions & 16 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,11 @@ This is a simple configuration reading tool. It just does the following:
- [Custom Value Setter](#custom-value-setter)
- [Custom Value Update](#custom-value-update)
- [Supported File Formats](#supported-file-formats)
- [Integration](#integration)
- [Flag](#flag)
- [Examples](#examples)
- [Contribution](#contribution)
- [Thanks](#thanks)

## Installation

Expand Down Expand Up @@ -60,11 +63,11 @@ You can read a configuration file and environment variables in a single function
import github.com/ilyakaznacheev/cleanenv

type ConfigDatabase struct {
Port string `yml:"port" env:"PORT" env-default:"5432"`
Host string `yml:"host" env:"HOST" env-default:"localhost"`
Name string `yml:"name" env:"NAME" env-default:"postgres"`
User string `yml:"user" env:"USER" env-default:"user"`
Password string `yml:"password" env:"PASSWORD"`
Port string `yml:"port" env:"PORT" env-default:"5432"`
Host string `yml:"host" env:"HOST" env-default:"localhost"`
Name string `yml:"name" env:"NAME" env-default:"postgres"`
User string `yml:"user" env:"USER" env-default:"user"`
Password string `yml:"password" env:"PASSWORD"`
}

var cfg ConfigDatabase
Expand All @@ -89,11 +92,11 @@ Sometimes you don't want to use configuration files at all, or you may want to u
import github.com/ilyakaznacheev/cleanenv

type ConfigDatabase struct {
Port string `env:"PORT" env-default:"5432"`
Host string `env:"HOST" env-default:"localhost"`
Name string `env:"NAME" env-default:"postgres"`
User string `env:"USER" env-default:"user"`
Password string `env:"PASSWORD"`
Port string `env:"PORT" env-default:"5432"`
Host string `env:"HOST" env-default:"localhost"`
Name string `env:"NAME" env-default:"postgres"`
User string `env:"USER" env-default:"user"`
Password string `env:"PASSWORD"`
}

var cfg ConfigDatabase
Expand Down Expand Up @@ -181,11 +184,11 @@ To make custom type allows to set the value from the environment variable, you n
type MyField string

func (f MyField) SetValue(s string) error {
if s == "" {
return fmt.Errorf("field value can't be empty")
}
f = MyField("my field is: "+ s)
return nil
if s == "" {
return fmt.Errorf("field value can't be empty")
}
f = MyField("my field is: "+ s)
return nil
}

type Config struct {
Expand All @@ -203,7 +206,7 @@ Thus, you need to implement the `Updater` interface on the structure level:

```go
type Config struct {
Field string
Field string
}

func (c *Config) Update() error {
Expand All @@ -222,6 +225,27 @@ There are several most popular config file formats supported:
- TOML
- ENV

## Integration

Package can be used with many other solutions. To make it more useful, we made some helpers.

### Flag

You can use the cleanenv help together with Golang `flag` package.

```go
// create some config structure
var cfg config

// create flag set using `flag` package
fset := flag.NewFlagSet("Example", flag.ContinueOnError)

// get config usage with wrapped flag usage
fset.Usage := cleanenv.FUsage(fset.Output(), &cfg, nil, fset.Usage)

fset.Parse(os.Args[1:])
```

## Examples

```go
Expand Down
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,5 @@ require (
github.com/joho/godotenv v1.3.0
gopkg.in/yaml.v2 v2.2.2
)

go 1.13

0 comments on commit a5b75ca

Please sign in to comment.